filter() public method

Filter the elements in the array by a callback function
public filter ( func $callback ) : Collection
$callback func the callback function
return Collection
 /**
  * @test Collection::filter()
  */
 public function testFilter()
 {
     $this->buildEnvironment();
     $result = $this->collection->filter('prop2', '>', 2)->fetch();
     $expect = $this->source;
     unset($expect['a1']);
     $this->assertEquals($expect, $result);
     $result = $this->collection->filter('prop2', '>=', 2)->fetch();
     $expect = $this->source;
     $this->assertEquals($expect, $result);
 }
 public function testFilter()
 {
     $this->collection->filter(function ($item) {
         return $item['last_name'] == 'smith';
     });
     $this->assertEquals(2, $this->collection->count());
     $this->assertContains($this->_0, $this->collection->toArray());
     $this->assertNotContains($this->_1, $this->collection->toArray());
     $this->assertNotContains($this->_2, $this->collection->toArray());
     $this->assertContains($this->_3, $this->collection->toArray());
 }
Example #3
0
 /**
  * This method is called in case a primary key was defined using the addPrimaryKey() method.
  * It currently does something only if using SQLite.
  * If a column is an auto-increment key in SQLite, it has to be a primary key and it has to defined
  * when defining the column. Phinx takes care of that so we have to make sure columns defined as autoincrement were
  * not added with the addPrimaryKey method, otherwise, SQL queries will be wrong.
  *
  * @return void
  */
 protected function filterPrimaryKey()
 {
     if ($this->getAdapter()->getAdapterType() !== 'sqlite' || empty($this->options['primary_key'])) {
         return;
     }
     $primaryKey = $this->options['primary_key'];
     if (!is_array($primaryKey)) {
         $primaryKey = [$primaryKey];
     }
     $primaryKey = array_flip($primaryKey);
     $columnsCollection = new Collection($this->columns);
     $primaryKeyColumns = $columnsCollection->filter(function ($columnDef, $key) use($primaryKey) {
         return isset($primaryKey[$columnDef->getName()]);
     })->toArray();
     if (empty($primaryKeyColumns)) {
         return;
     }
     foreach ($primaryKeyColumns as $primaryKeyColumn) {
         if ($primaryKeyColumn->isIdentity()) {
             unset($primaryKey[$primaryKeyColumn->getName()]);
         }
     }
     $primaryKey = array_flip($primaryKey);
     if (!empty($primaryKey)) {
         $this->options['primary_key'] = $primaryKey;
     } else {
         unset($this->options['primary_key']);
     }
 }
 public function testFilter()
 {
     $collection = new Collection([new Bar('a'), new Bar('a'), new Bar('b'), new Bar('b')]);
     $result = $collection->filter(new FooFilter('a'));
     $this->assertNotSame($collection, $result);
     $this->assertInstanceOf(get_class($collection), $result);
     $this->assertCount(2, $result);
 }
Example #5
0
 /**
  * @param string $input
  */
 public function filterCategory($input)
 {
     if (empty($input) || $this->collection->isEmpty()) {
         return null;
     }
     list($operand, $value) = array_values($this->getOperandAndValue($input));
     $this->collection = $this->collection->filter(function (array $entry) use($operand, $value) {
         return $this->stringComparator($entry['category'], $value, $operand);
     });
 }
 /**
  * {@inheritdoc}
  */
 public function filter(Closure $p)
 {
     $this->initialize();
     return $this->coll->filter($p);
 }
 /**
  * @param \Collection $rowset
  * @return mixed
  */
 private function buildTree($rowset)
 {
     // extract only main categories
     $parents = $rowset->where('parent_id', null)->keyBy('id');
     // extract only children categories
     $children = $rowset->filter(function ($item) {
         return $item->parent_id != null;
     })->groupBy('parent_id');
     // we merge children with parent element
     foreach ($children as $parentId => $child) {
         $parents[$parentId]->subs = $child;
     }
     return $parents;
 }
Example #8
0
 public function filter($filterer, $op = '===')
 {
     $collection = new Collection($this->store);
     return $collection->filter($filterer, $op);
 }