Optional.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. */
  22. public function __construct($value)
  23. {
  24. $this->value = $value;
  25. }
  26. /**
  27. * Dynamically access a property on the underlying object.
  28. *
  29. * @param string $key
  30. * @return mixed
  31. */
  32. public function __get($key)
  33. {
  34. if (is_object($this->value)) {
  35. return $this->value->{$key} ?? null;
  36. }
  37. }
  38. /**
  39. * Dynamically check a property exists on the underlying object.
  40. *
  41. * @param mixed $name
  42. * @return bool
  43. */
  44. public function __isset($name)
  45. {
  46. if (is_object($this->value)) {
  47. return isset($this->value->{$name});
  48. }
  49. if (is_array($this->value) || $this->value instanceof ArrayObject) {
  50. return isset($this->value[$name]);
  51. }
  52. return false;
  53. }
  54. /**
  55. * Determine if an item exists at an offset.
  56. *
  57. * @param mixed $key
  58. * @return bool
  59. */
  60. public function offsetExists($key): bool
  61. {
  62. return Arr::accessible($this->value) && Arr::exists($this->value, $key);
  63. }
  64. /**
  65. * Get an item at a given offset.
  66. *
  67. * @param mixed $key
  68. * @return mixed
  69. */
  70. public function offsetGet($key): mixed
  71. {
  72. return Arr::get($this->value, $key);
  73. }
  74. /**
  75. * Set the item at a given offset.
  76. *
  77. * @param mixed $key
  78. * @param mixed $value
  79. * @return void
  80. */
  81. public function offsetSet($key, $value): void
  82. {
  83. if (Arr::accessible($this->value)) {
  84. $this->value[$key] = $value;
  85. }
  86. }
  87. /**
  88. * Unset the item at a given offset.
  89. *
  90. * @param string $key
  91. * @return void
  92. */
  93. public function offsetUnset($key): void
  94. {
  95. if (Arr::accessible($this->value)) {
  96. unset($this->value[$key]);
  97. }
  98. }
  99. /**
  100. * Dynamically pass a method to the underlying object.
  101. *
  102. * @param string $method
  103. * @param array $parameters
  104. * @return mixed
  105. */
  106. public function __call($method, $parameters)
  107. {
  108. if (static::hasMacro($method)) {
  109. return $this->macroCall($method, $parameters);
  110. }
  111. if (is_object($this->value)) {
  112. return $this->value->{$method}(...$parameters);
  113. }
  114. }
  115. }