cct-marketing /
collections
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace CCT\Component\Collections; |
||
| 6 | |||
| 7 | use CCT\Component\Collections\Interactors\ArrayAggregator; |
||
| 8 | use CCT\Component\Collections\Interactors\ArrayInspector; |
||
| 9 | use CCT\Component\Collections\Interactors\ArraySegregator; |
||
| 10 | use CCT\Component\Collections\Interactors\ArraySorter; |
||
| 11 | use CCT\Component\Collections\Interactors\InteractorInterface; |
||
| 12 | |||
| 13 | class CollectionProxy |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * @var CollectionInterface |
||
| 17 | */ |
||
| 18 | protected $collection; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var object |
||
| 22 | */ |
||
| 23 | protected $proxy; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var array |
||
| 27 | */ |
||
| 28 | public static $proxies = [ |
||
| 29 | 'sorter' => ArraySorter::class, |
||
| 30 | 'inspector' => ArrayInspector::class, |
||
| 31 | 'aggregator' => ArrayAggregator::class, |
||
| 32 | 'segregator' => ArraySegregator::class, |
||
| 33 | ]; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * ArrayProxy constructor. |
||
| 37 | * |
||
| 38 | * @param CollectionInterface $collection |
||
| 39 | * @param string $proxy |
||
| 40 | */ |
||
| 41 | 15 | public function __construct(CollectionInterface $collection, string $proxy) |
|
| 42 | { |
||
| 43 | 15 | $this->collection = $collection; |
|
| 44 | 15 | $this->proxy = new static::$proxies[$proxy]($collection->all()); |
|
| 45 | 15 | } |
|
| 46 | |||
| 47 | /** |
||
| 48 | * Proxy a method call onto the specified Interactor. |
||
| 49 | * |
||
| 50 | * @param string $method |
||
| 51 | * @param array $parameters |
||
| 52 | * |
||
| 53 | * @return mixed |
||
| 54 | */ |
||
| 55 | 15 | public function __call($method, $parameters) |
|
| 56 | { |
||
| 57 | 15 | $result = $this->proxy->{$method}(...$parameters); |
|
| 58 | |||
| 59 | 13 | if (is_array($result)) { |
|
| 60 | 9 | $this->collection->override($result); |
|
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 61 | |||
| 62 | 9 | return $this->collection; |
|
| 63 | } |
||
| 64 | |||
| 65 | 4 | if ($result instanceof InteractorInterface || null === $result) { |
|
| 66 | 1 | $this->collection->override($this->proxy->all()); |
|
| 67 | |||
| 68 | 1 | return $this->collection; |
|
| 69 | } |
||
| 70 | |||
| 71 | 3 | return $result; |
|
| 72 | } |
||
| 73 | } |
||
| 74 |