Optional.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace Illuminate\Support;
  3. use ArrayAccess;
  4. use ArrayObject;
  5. use Illuminate\Support\Traits\Macroable;
  6. class Optional implements ArrayAccess
  7. {
  8. use Macroable {
  9. __call as macroCall;
  10. }
  11. /**
  12. * The underlying object.
  13. *
  14. * @var mixed
  15. */
  16. protected $value;
  17. /**
  18. * Create a new optional instance.
  19. *
  20. * @param mixed $value
  21. * @return void
  22. */
  23. public function __construct($value)
  24. {
  25. $this->value = $value;
  26. }
  27. /**
  28. * Dynamically access a property on the underlying object.
  29. *
  30. * @param string $key
  31. * @return mixed
  32. */
  33. public function __get($key)
  34. {
  35. if (is_object($this->value)) {
  36. return $this->value->{$key} ?? null;
  37. }
  38. }
  39. /**
  40. * Dynamically check a property exists on the underlying object.
  41. *
  42. * @param mixed $name
  43. * @return bool
  44. */
  45. public function __isset($name)
  46. {
  47. if (is_object($this->value)) {
  48. return isset($this->value->{$name});
  49. }
  50. if (is_array($this->value) || $this->value instanceof ArrayObject) {
  51. return isset($this->value[$name]);
  52. }
  53. return false;
  54. }
  55. /**
  56. * Determine if an item exists at an offset.
  57. *
  58. * @param mixed $key
  59. * @return bool
  60. */
  61. #[\ReturnTypeWillChange]
  62. public function offsetExists($key)
  63. {
  64. return Arr::accessible($this->value) && Arr::exists($this->value, $key);
  65. }
  66. /**
  67. * Get an item at a given offset.
  68. *
  69. * @param mixed $key
  70. * @return mixed
  71. */
  72. #[\ReturnTypeWillChange]
  73. public function offsetGet($key)
  74. {
  75. return Arr::get($this->value, $key);
  76. }
  77. /**
  78. * Set the item at a given offset.
  79. *
  80. * @param mixed $key
  81. * @param mixed $value
  82. * @return void
  83. */
  84. #[\ReturnTypeWillChange]
  85. public function offsetSet($key, $value)
  86. {
  87. if (Arr::accessible($this->value)) {
  88. $this->value[$key] = $value;
  89. }
  90. }
  91. /**
  92. * Unset the item at a given offset.
  93. *
  94. * @param string $key
  95. * @return void
  96. */
  97. #[\ReturnTypeWillChange]
  98. public function offsetUnset($key)
  99. {
  100. if (Arr::accessible($this->value)) {
  101. unset($this->value[$key]);
  102. }
  103. }
  104. /**
  105. * Dynamically pass a method to the underlying object.
  106. *
  107. * @param string $method
  108. * @param array $parameters
  109. * @return mixed
  110. */
  111. public function __call($method, $parameters)
  112. {
  113. if (static::hasMacro($method)) {
  114. return $this->macroCall($method, $parameters);
  115. }
  116. if (is_object($this->value)) {
  117. return $this->value->{$method}(...$parameters);
  118. }
  119. }
  120. }