Optional.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. public function offsetExists($key): bool
  62. {
  63. return Arr::accessible($this->value) && Arr::exists($this->value, $key);
  64. }
  65. /**
  66. * Get an item at a given offset.
  67. *
  68. * @param mixed $key
  69. * @return mixed
  70. */
  71. public function offsetGet($key): mixed
  72. {
  73. return Arr::get($this->value, $key);
  74. }
  75. /**
  76. * Set the item at a given offset.
  77. *
  78. * @param mixed $key
  79. * @param mixed $value
  80. * @return void
  81. */
  82. public function offsetSet($key, $value): void
  83. {
  84. if (Arr::accessible($this->value)) {
  85. $this->value[$key] = $value;
  86. }
  87. }
  88. /**
  89. * Unset the item at a given offset.
  90. *
  91. * @param string $key
  92. * @return void
  93. */
  94. public function offsetUnset($key): void
  95. {
  96. if (Arr::accessible($this->value)) {
  97. unset($this->value[$key]);
  98. }
  99. }
  100. /**
  101. * Dynamically pass a method to the underlying object.
  102. *
  103. * @param string $method
  104. * @param array $parameters
  105. * @return mixed
  106. */
  107. public function __call($method, $parameters)
  108. {
  109. if (static::hasMacro($method)) {
  110. return $this->macroCall($method, $parameters);
  111. }
  112. if (is_object($this->value)) {
  113. return $this->value->{$method}(...$parameters);
  114. }
  115. }
  116. }