public function ArrayCollection::matching

Select all elements from a selectable that match the criteria and return a new collection containing these elements.

Parameters

Criteria $criteria:

Return value

Collection

Overrides Selectable::matching

File

drupal/core/vendor/doctrine/common/lib/Doctrine/Common/Collections/ArrayCollection.php, line 470

Class

ArrayCollection
An ArrayCollection is a Collection implementation that wraps a regular PHP array.

Namespace

Doctrine\Common\Collections

Code

public function matching(Criteria $criteria) {
  $expr = $criteria
    ->getWhereExpression();
  $filtered = $this->_elements;
  if ($expr) {
    $visitor = new ClosureExpressionVisitor();
    $filter = $visitor
      ->dispatch($expr);
    $filtered = array_filter($filtered, $filter);
  }
  if ($orderings = $criteria
    ->getOrderings()) {
    $next = null;
    foreach (array_reverse($orderings) as $field => $ordering) {
      $next = ClosureExpressionVisitor::sortByField($field, $ordering == 'DESC' ? -1 : 1, $next);
    }
    usort($filtered, $next);
  }
  $offset = $criteria
    ->getFirstResult();
  $length = $criteria
    ->getMaxResults();
  if ($offset || $length) {
    $filtered = array_slice($filtered, (int) $offset, $length);
  }
  return new static($filtered);
}