本文整理汇总了PHP中Collection::each方法的典型用法代码示例。如果您正苦于以下问题:PHP Collection::each方法的具体用法?PHP Collection::each怎么用?PHP Collection::each使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Collection
的用法示例。
在下文中一共展示了Collection::each方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: addPermissions
/**
* Filter collection by permissions and add attributes canBeEdited and canBeRemoved
* @param Collection $collection
* @return Collection
*/
public function addPermissions($collection)
{
$newCollection = collect([]);
$customActions = $this->crudeSetup->getCustomActions();
$collection->each(function ($model) use($newCollection, $customActions) {
if ($this->permissionView($model)) {
$model->canBeEdited = $this->permissionUpdate($model);
$model->canBeRemoved = $this->permissionDelete($model);
if (!empty($customActions)) {
foreach ($customActions as $action => $value) {
$permission = $action . 'CustomActionPermission';
$param = $action . 'CustomActionAvailable';
$model->{$param} = method_exists($this, $permission) ? $this->{$permission}($model) : true;
}
}
$newCollection->push($model);
}
});
return $newCollection;
}
示例2: testEach
public function testEach()
{
$arr = [];
$this->collection->each(function ($item, $key) use(&$arr) {
$arr[$key] = $item;
});
$this->assertEquals([$this->_0, $this->_1, $this->_2, $this->_3], $arr);
}
示例3: testEach
public function testEach()
{
$this->collection->each(function ($item) {
$this->assertArrayHasKey('first_name', $item);
$this->assertArrayHasKey('last_name', $item);
$this->assertArrayHasKey('age', $item);
});
}
示例4: test6_Mapping
public function test6_Mapping()
{
# mapping
static::$collection->append('part2', ['books' => ['No name', 'Perpetual change']]);
$result = [];
static::$collection->each(function ($key) use(&$result) {
$result[] = $key;
});
}
示例5: groupBy
/**
* Splits the values into every possible response returned by the callback.
*
* For example, if the callback juste return the value:
* ['a', 'b', 'c']
* will become:
* ['a' => ['a'], 'b' => ['b'], 'c' => ['c']]
*
* If the callback return a boolean, it will be return an array with two collections.
* [0 => $nonMatchedCollection, 1 => $matchedCollection]
* This seems to be quite the same as Doctrine's ARrayCollection. But be careful because keys are inverted.
*
* @param callable $callback
* @return $this[]
*/
public function groupBy(callable $callback)
{
$results = [];
$collection = new Collection($this);
foreach ($collection->each($callback) as $key => $result) {
if (!isset($results[$result])) {
$results[$result] = new $this();
}
$results[$result][$key] = $this[$key];
}
return $results;
}
示例6: testEachWithReturn
public function testEachWithReturn()
{
$collection = new Collection(['a']);
$result = $collection->each(function ($bar) {
return strtoupper($bar);
});
$this->assertNotSame($collection, $result);
$this->assertInstanceOf(get_class($collection), $result);
$this->assertCount(1, $result);
foreach ($result as $bar) {
$this->assertSame('A', $bar);
}
}