This closure can be used with {@Collection #filter()} and is used internally by {@ArrayCollection #select()}.
Since: 2.3
Author: Benjamin Eberlei (kontakt@beberlei.de)
Inheritance: extends Doctrine\Common\Collections\Expr\ExpressionVisitor
 /**
  * @param object $object
  * @param string $field
  *
  * @return mixed
  */
 public static function getObjectFieldValue($object, $field)
 {
     $propertyAccess = PropertyAccess::createPropertyAccessorBuilder()->enableMagicCall()->enableExceptionOnInvalidIndex()->getPropertyAccessor();
     try {
         return $propertyAccess->getValue($object, $field);
     } catch (NoSuchPropertyException $e) {
         return parent::getObjectFieldValue($object, $field);
     }
 }
Example #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);
 }
 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());
 }
 public function testArrayComparison()
 {
     $closure = $this->visitor->walkComparison($this->builder->eq("foo", 42));
     $this->assertTrue($closure(array('foo' => 42)));
 }
 /**
  * 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);
 }