You are here

public function Query::execute in Apigee Edge 8

Execute the query.

Return value

int|array Returns an integer for count queries or an array of ids. The values of the array are always entity ids. The keys will be revision ids if the entity supports revision and entity ids if not.

Overrides QueryInterface::execute

File

src/Entity/Query/Query.php, line 78

Class

Query
Defines the entity query for Apigee Edge entities.

Namespace

Drupal\apigee_edge\Entity\Query

Code

public function execute() {

  // We have to allow getFromStorage() to remove unnecessary query conditions
  // so we have to run it before compile(). Example: DeveloperAppQuery
  // can load only apps of a specific developer by developerId or email.
  // If it does that by email then the email condition should be removed
  // because developer apps do not have email property only developerId.
  // Basically, DeveloperAppQuery already applies a condition on the returned
  // result because this function gets called.
  $all_records = $this
    ->getFromStorage();
  $filter = $this->condition
    ->compile($this);
  $result = array_filter($all_records, $filter);
  if ($this->count) {
    return count($result);
  }
  if ($this->sort) {
    uasort($result, function (EntityInterface $entity0, EntityInterface $entity1) : int {
      foreach ($this->sort as $sort) {
        $value0 = Condition::getProperty($entity0, $sort['field']);
        $value1 = Condition::getProperty($entity1, $sort['field']);
        $cmp = $value0 <=> $value1;
        if ($cmp === 0) {
          continue;
        }
        if ($sort['direction'] === 'DESC') {
          $cmp *= -1;
        }
        return $cmp;
      }
      return 0;
    });
  }
  $this
    ->initializePager();
  if ($this->range) {
    $result = array_slice($result, $this->range['start'], $this->range['length']);
  }
  return array_map(function (EntityInterface $entity) : string {
    return (string) $entity
      ->id();
  }, $result);
}