@@ 110-118 (lines=9) @@ | ||
107 | * |
|
108 | * @return \Enzyme\Collection\Collection |
|
109 | */ |
|
110 | public function map(Closure $fn) |
|
111 | { |
|
112 | $results = []; |
|
113 | foreach ($this->items as $key => $value) { |
|
114 | $results[] = $fn($value, $key); |
|
115 | } |
|
116 | ||
117 | return new static($results); |
|
118 | } |
|
119 | ||
120 | /** |
|
121 | * Execute the given callback function for each element in this collection |
|
@@ 349-362 (lines=14) @@ | ||
346 | * |
|
347 | * @return \Enzyme\Collection\Collection |
|
348 | */ |
|
349 | public function filter(Closure $fn) |
|
350 | { |
|
351 | $results = []; |
|
352 | foreach ($this->items as $key => $value) { |
|
353 | if (true === $fn($value, $key)) { |
|
354 | $results[$key] = $value; |
|
355 | } |
|
356 | } |
|
357 | ||
358 | // Pushing this new array will normalize numeric keys if they exist. |
|
359 | // After filtering, they may not start at zero and sequentially |
|
360 | // go upwards, which is generally not expected. |
|
361 | return (new static())->pushArray($results); |
|
362 | } |
|
363 | ||
364 | /** |
|
365 | * Sort the collection using the provided callback function. Same expected |