Stratadox /
ImmutableCollection
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace Stratadox\ImmutableCollection; |
||
| 6 | |||
| 7 | use function array_filter; |
||
| 8 | use Closure; |
||
| 9 | use Stratadox\Collection\Collection; |
||
| 10 | use Stratadox\Collection\Filterable; |
||
| 11 | use Stratadox\Specification\Contract\Satisfiable; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * Behaviour to allow "filtering" the immutable collection. |
||
| 15 | * |
||
| 16 | * Provides access to filtering behaviour in the form of a method that |
||
| 17 | * returns a modified copy of the original (immutable) collection. |
||
| 18 | * |
||
| 19 | * @package Stratadox\Collection |
||
| 20 | * @author Stratadox |
||
| 21 | */ |
||
| 22 | trait Filtering |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * @see Filterable::that |
||
| 26 | * @param Satisfiable $condition |
||
| 27 | * @return static |
||
| 28 | */ |
||
| 29 | public function that(Satisfiable $condition) |
||
| 30 | { |
||
| 31 | return $this->newCopy(array_filter( |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 32 | $this->items(), [$condition, 'isSatisfiedBy'] |
||
| 33 | )); |
||
| 34 | } |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @see Filterable::filterWith |
||
| 38 | * @param Closure $function |
||
| 39 | * @return static |
||
| 40 | */ |
||
| 41 | public function filterWith(Closure $function) |
||
| 42 | { |
||
| 43 | return $this->newCopy(array_filter( |
||
|
0 ignored issues
–
show
|
|||
| 44 | $this->items(), $function |
||
| 45 | )); |
||
| 46 | } |
||
| 47 | |||
| 48 | /** @see Collection::items() */ |
||
| 49 | abstract public function items(): array; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @see ImmutableCollection::newCopy() |
||
| 53 | * @param array $items |
||
| 54 | * @return static |
||
| 55 | */ |
||
| 56 | abstract protected function newCopy(array $items): Collection; |
||
| 57 | } |
||
| 58 |