Filter a collection for entries matching the expression.
public static filter ( array | ArrayAcces\ArrayAccess | Traversabl\Traversable $collection, Webmozart\Expression\Expression $expr ) : array | ArrayAcces\ArrayAccess | Traversabl\Traversable | ||
$collection | array | ArrayAcces\ArrayAccess | Traversabl\Traversable | An array or an object implementing Traversable and ArrayAccess. |
$expr | Webmozart\Expression\Expression | The expression to evaluate for each entry. |
Результат | array | ArrayAcces\ArrayAccess | Traversabl\Traversable | The filtered collection. |
public function testDomainExpressions() { $c1 = new Customer(); $c1->setPremium(true); $c2 = new Customer(); $c2->setBookings(array('booking1', 'booking2')); $c3 = new Customer(); $c3->setPremium(true); $c3->setBookings(array('booking1')); $customers = array($c1, $c2, $c3); $this->assertEquals(array($c1, 2 => $c3), Expr::filter($customers, new IsPremium())); $this->assertEquals(array(1 => $c2, 2 => $c3), Expr::filter($customers, new HasPreviousBookings())); $this->assertEquals(array(2 => $c3), Expr::filter($customers, Expr::andX(array(new HasPreviousBookings(), new IsPremium())))); }
/** * {@inheritdoc} */ public function findBindings($typeName, Expression $expr = null) { Assert::stringNotEmpty($typeName, 'The type class must be a non-empty string. Got: %s'); if (!isset($this->keysByTypeName[$typeName])) { return array(); } $key = $this->keysByTypeName[$typeName]; if (!isset($this->bindingsByKey[$key])) { $this->loadBindingsForKey($key); } $bindings = $this->bindingsByKey[$key]; if (null !== $expr) { $bindings = Expr::filter($bindings, $expr); } return $bindings; }
public function testFilterCollection() { $input = new ArrayObject(range(1, 10)); $output = new ArrayObject(array_filter(range(1, 10), function ($i) { return $i > 4; })); $this->assertEquals($output, Expr::filter($input, Expr::greaterThan(4))); }