123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- <?php
- declare(strict_types=1);
- namespace Hyperf\Support;
- use ArrayAccess;
- use Hyperf\Collection\Arr;
- use Hyperf\Macroable\Macroable;
- class Optional implements ArrayAccess
- {
- use Macroable {
- __call as macroCall;
- }
-
- public function __construct(protected $value)
- {
- }
-
- public function __get($key)
- {
- if (is_object($this->value)) {
- return $this->value->{$key} ?? null;
- }
- return null;
- }
-
- public function __isset($name)
- {
- if (is_object($this->value)) {
- return isset($this->value->{$name});
- }
- return false;
- }
-
- public function __call($method, $parameters)
- {
- if (static::hasMacro($method)) {
- return $this->macroCall($method, $parameters);
- }
- if (is_object($this->value)) {
- return $this->value->{$method}(...$parameters);
- }
- return null;
- }
-
- public function offsetExists(mixed $offset): bool
- {
- return Arr::accessible($this->value) && Arr::exists($this->value, $offset);
- }
-
- public function offsetGet(mixed $offset): mixed
- {
- return Arr::get($this->value, $offset);
- }
-
- public function offsetSet(mixed $offset, mixed $value): void
- {
- if (Arr::accessible($this->value)) {
- $this->value[$offset] = $value;
- }
- }
-
- public function offsetUnset(mixed $offset): void
- {
- if (Arr::accessible($this->value)) {
- unset($this->value[$offset]);
- }
- }
- }
|