本文整理汇总了PHP中Collection::filter方法的典型用法代码示例。如果您正苦于以下问题:PHP Collection::filter方法的具体用法?PHP Collection::filter怎么用?PHP Collection::filter使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Collection
的用法示例。
在下文中一共展示了Collection::filter方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: testFilter
/**
* @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);
}
示例2: testFilter
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());
}
示例3: filterPrimaryKey
/**
* 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']);
}
}
示例4: testFilter
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);
}
示例5: filterCategory
/**
* @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);
});
}
示例6: filter
/**
* {@inheritdoc}
*/
public function filter(Closure $p)
{
$this->initialize();
return $this->coll->filter($p);
}
示例7: buildTree
/**
* @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;
}
示例8: filter
public function filter($filterer, $op = '===')
{
$collection = new Collection($this->store);
return $collection->filter($filterer, $op);
}