HigherOrderCollectionProxy.php 1.4KB

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