| @@ 97-105 (lines=9) @@ | ||
| 94 | * |
|
| 95 | * @return \Enzyme\Collection\Collection |
|
| 96 | */ |
|
| 97 | public function map(Closure $fn) |
|
| 98 | { |
|
| 99 | $results = []; |
|
| 100 | foreach ($this->items as $key => $value) { |
|
| 101 | $results[] = $fn($value, $key); |
|
| 102 | } |
|
| 103 | ||
| 104 | return new static($results); |
|
| 105 | } |
|
| 106 | ||
| 107 | /** |
|
| 108 | * Pluck out all values in this collection which have the specified key. |
|
| @@ 307-320 (lines=14) @@ | ||
| 304 | * |
|
| 305 | * @return \Enzyme\Collection\Collection |
|
| 306 | */ |
|
| 307 | public function filter(Closure $fn) |
|
| 308 | { |
|
| 309 | $results = []; |
|
| 310 | foreach ($this->items as $key => $value) { |
|
| 311 | if (true === $fn($value, $key)) { |
|
| 312 | $results[$key] = $value; |
|
| 313 | } |
|
| 314 | } |
|
| 315 | ||
| 316 | // Pushing this new array will normalize numeric keys if they exist. |
|
| 317 | // After filtering, they may not start at zero and sequentially |
|
| 318 | // go upwards, which is generally not expected. |
|
| 319 | return (new static())->pushArray($results); |
|
| 320 | } |
|
| 321 | ||
| 322 | /** |
|
| 323 | * Sort the collection using the provided callback function. Same expected |
|