/** * 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; }
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); }
public function testEach() { $this->collection->each(function ($item) { $this->assertArrayHasKey('first_name', $item); $this->assertArrayHasKey('last_name', $item); $this->assertArrayHasKey('age', $item); }); }
public function test6_Mapping() { # mapping static::$collection->append('part2', ['books' => ['No name', 'Perpetual change']]); $result = []; static::$collection->each(function ($key) use(&$result) { $result[] = $key; }); }
/** * 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; }
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); } }