HigherOrderWhenProxy.php 1.3 KB

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