Stratadox /
ImmutableCollection
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace Stratadox\ImmutableCollection; |
||
| 6 | |||
| 7 | use function in_array; |
||
| 8 | use Stratadox\Collection\Collection; |
||
| 9 | use Stratadox\Collection\Differentiable; |
||
| 10 | |||
| 11 | /** |
||
| 12 | * Behaviour to find the difference between multiple collections. |
||
| 13 | * |
||
| 14 | * Provides access to differentiating behaviour in the form of a method that |
||
| 15 | * returns a modified copy of the original (immutable) collection with all |
||
| 16 | * values omitted that also appear in one of the other collections. |
||
| 17 | * |
||
| 18 | * @package Stratadox\Collection |
||
| 19 | * @author Stratadox |
||
| 20 | */ |
||
| 21 | trait Differentiating |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * @see Differentiable::differenceBetween() |
||
| 25 | * @param Collection[] ...$others |
||
| 26 | * @return static |
||
| 27 | */ |
||
| 28 | public function differenceBetween(Collection ...$others) |
||
| 29 | { |
||
| 30 | return $this->newCopy(array_filter( |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 31 | $this->items(), |
||
| 32 | function ($item) use ($others) : bool { |
||
| 33 | foreach ($others as $otherCollection) { |
||
| 34 | if (in_array($item, $otherCollection->items(), false)) { |
||
| 35 | return false; |
||
| 36 | } |
||
| 37 | } |
||
| 38 | return true; |
||
| 39 | } |
||
| 40 | )); |
||
| 41 | } |
||
| 42 | |||
| 43 | /** @see Collection::items() */ |
||
| 44 | abstract public function items(): array; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @see ImmutableCollection::newCopy() |
||
| 48 | * @param array $items |
||
| 49 | * @return static |
||
| 50 | */ |
||
| 51 | abstract protected function newCopy(array $items): Collection; |
||
| 52 | } |