Optional.php 2.6 KB

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