beautify code, ajout méthode pluck et toArray à l'objet collection
This commit is contained in:
@@ -57,8 +57,7 @@ class Collection implements IteratorAggregate, Countable
|
||||
|
||||
public function map(callable $fn): Collection
|
||||
{
|
||||
$tmp = array_map($fn, $this->elements);
|
||||
return new Collection($tmp);
|
||||
return new Collection(array_map($fn, $this->elements));
|
||||
}
|
||||
|
||||
public function each(callable $fn): void
|
||||
@@ -68,9 +67,24 @@ class Collection implements IteratorAggregate, Countable
|
||||
|
||||
public function filter(callable $fn): Collection
|
||||
{
|
||||
$tmp = array_filter($this->elements, $fn, ARRAY_FILTER_USE_BOTH);
|
||||
$collect = new Collection($tmp);
|
||||
return $collect;
|
||||
return new Collection(array_filter($this->elements, $fn, ARRAY_FILTER_USE_BOTH));
|
||||
}
|
||||
|
||||
public function pluck(string $key): Collection
|
||||
{
|
||||
return new Collection(array_map(function ($item) use ($key) {
|
||||
if (is_object($item)) {
|
||||
return isset($item->$key) ? $item->$key : null;
|
||||
}
|
||||
if (is_array($item)) {
|
||||
return isset($item[$key]) ? $item[$key] : null;
|
||||
}
|
||||
}, $this->elements));
|
||||
}
|
||||
|
||||
public function toArray(): array
|
||||
{
|
||||
return $this->elements;
|
||||
}
|
||||
|
||||
public function empty(): bool
|
||||
|
||||
Reference in New Issue
Block a user