Debian Codegraph @

lib/3rdParty/composer/illuminate/support/Optional.php

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