Traits
1 month ago
Arr.php
1 month ago
Collection.php
1 month ago
Enumerable.php
1 month ago
HigherOrderCollectionProxy.php
2 years ago
ItemNotFoundException.php
2 years ago
LazyCollection.php
1 month ago
MultipleItemsFoundException.php
1 month ago
helpers.php
1 month ago
HigherOrderCollectionProxy.php
61 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWPSCOPED\Illuminate\Support; |
| 4 | |
| 5 | /** |
| 6 | * @mixin \Illuminate\Support\Enumerable |
| 7 | * @internal |
| 8 | */ |
| 9 | class HigherOrderCollectionProxy |
| 10 | { |
| 11 | /** |
| 12 | * The collection being operated on. |
| 13 | * |
| 14 | * @var \Illuminate\Support\Enumerable |
| 15 | */ |
| 16 | protected $collection; |
| 17 | /** |
| 18 | * The method being proxied. |
| 19 | * |
| 20 | * @var string |
| 21 | */ |
| 22 | protected $method; |
| 23 | /** |
| 24 | * Create a new proxy instance. |
| 25 | * |
| 26 | * @param \Illuminate\Support\Enumerable $collection |
| 27 | * @param string $method |
| 28 | * @return void |
| 29 | */ |
| 30 | public function __construct(Enumerable $collection, $method) |
| 31 | { |
| 32 | $this->method = $method; |
| 33 | $this->collection = $collection; |
| 34 | } |
| 35 | /** |
| 36 | * Proxy accessing an attribute onto the collection items. |
| 37 | * |
| 38 | * @param string $key |
| 39 | * @return mixed |
| 40 | */ |
| 41 | public function __get($key) |
| 42 | { |
| 43 | return $this->collection->{$this->method}(function ($value) use($key) { |
| 44 | return \is_array($value) ? $value[$key] : $value->{$key}; |
| 45 | }); |
| 46 | } |
| 47 | /** |
| 48 | * Proxy a method call onto the collection items. |
| 49 | * |
| 50 | * @param string $method |
| 51 | * @param array $parameters |
| 52 | * @return mixed |
| 53 | */ |
| 54 | public function __call($method, $parameters) |
| 55 | { |
| 56 | return $this->collection->{$this->method}(function ($value) use($method, $parameters) { |
| 57 | return $value->{$method}(...$parameters); |
| 58 | }); |
| 59 | } |
| 60 | } |
| 61 |