1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Stratadox\ImmutableCollection; |
||
6 | |||
7 | use function array_map; |
||
8 | use Closure; |
||
9 | use Stratadox\Collection\Collection; |
||
10 | use Stratadox\Collection\Mappable; |
||
11 | |||
12 | /** |
||
13 | * Behaviour to map the collection according to a closure. |
||
14 | * |
||
15 | * Provides access to mapping behaviour in the form of a method that maps the |
||
16 | * collection according to an anonymous function. |
||
17 | * |
||
18 | * @package Stratadox\Collection |
||
19 | * @author Stratadox |
||
20 | */ |
||
21 | trait Mapping |
||
22 | { |
||
23 | /** |
||
24 | * @see Mappable::map() |
||
25 | * @param Closure $function |
||
26 | * @return static |
||
27 | */ |
||
28 | public function map(Closure $function) |
||
29 | { |
||
30 | return $this->newCopy(array_map($function, $this->items())); |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
31 | } |
||
32 | |||
33 | /** @see Collection::items() */ |
||
34 | abstract public function items(): array; |
||
35 | |||
36 | /** |
||
37 | * @see ImmutableCollection::newCopy() |
||
38 | * @param array $items |
||
39 | * @return Collection|static |
||
40 | */ |
||
41 | abstract protected function newCopy(array $items): Collection; |
||
42 | } |
||
43 |