sortByField() публичный статический Метод

Helper for sorting arrays of objects based on multiple fields + orientations.
public static sortByField ( string $name, integer $orientation = 1, Closure $next = null ) : Closure
$name string
$orientation integer
$next Closure
Результат Closure
 public function testSortDelegate()
 {
     $objects = array(new TestObject("a", "c"), new TestObject("a", "b"), new TestObject("a", "a"));
     $sort = ClosureExpressionVisitor::sortByField("bar", 1);
     $sort = ClosureExpressionVisitor::sortByField("foo", 1, $sort);
     usort($objects, $sort);
     $this->assertEquals("a", $objects[0]->getBar());
     $this->assertEquals("b", $objects[1]->getBar());
     $this->assertEquals("c", $objects[2]->getBar());
 }
Пример #2
0
 /**
  * {@inheritDoc}
  */
 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()) {
         foreach (array_reverse($orderings) as $field => $ordering) {
             $next = ClosureExpressionVisitor::sortByField($field, $ordering == Criteria::DESC ? -1 : 1);
         }
         usort($filtered, $next);
     }
     $offset = $criteria->getFirstResult();
     $length = $criteria->getMaxResults();
     if ($offset || $length) {
         $filtered = array_slice($filtered, (int) $offset, $length);
     }
     return new static($filtered);
 }
 /**
  * Select all elements from a selectable that match the criteria and
  * return a new collection containing these elements.
  *
  * @param  Criteria $criteria
  * @return Collection2
  */
 public function matching(Criteria $criteria)
 {
     $this->initialize();
     $expr = $criteria->getWhereExpression();
     $filtered = $this->entities;
     if ($expr) {
         $visitor = new ClosureExpressionVisitor();
         $filter = $visitor->dispatch($expr);
         $filtered = array_filter($filtered, $filter);
     }
     if (null !== ($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 ArrayCollection($filtered);
 }