1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Stratadox\ImmutableCollection; |
||
6 | |||
7 | use Stratadox\Collection\Alterable; |
||
8 | use Stratadox\Collection\Collection; |
||
9 | |||
10 | /** |
||
11 | * Behaviour to allow "altering" the immutable collection. |
||
12 | * |
||
13 | * Provides access to update and delete behaviour in the form of methods that |
||
14 | * return a modified copy of the original (immutable) collection. |
||
15 | * |
||
16 | * @package Stratadox\Collection |
||
17 | * @author Stratadox |
||
18 | */ |
||
19 | trait Altering |
||
20 | { |
||
21 | /** |
||
22 | * @see Alterable::change() |
||
23 | * @param int $index |
||
24 | * @param mixed $newItem |
||
25 | * @return static |
||
26 | */ |
||
27 | public function change(int $index, $newItem) |
||
28 | { |
||
29 | $items = $this->items(); |
||
30 | $items[$index] = $newItem; |
||
31 | return $this->newCopy($items); |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
32 | } |
||
33 | |||
34 | /** @see Collection::items() */ |
||
35 | abstract public function items(): array; |
||
36 | |||
37 | /** |
||
38 | * @see ImmutableCollection::newCopy() |
||
39 | * @param array $items |
||
40 | * @return static |
||
41 | */ |
||
42 | abstract protected function newCopy(array $items): Collection; |
||
43 | } |
||
44 |