1 | <?php |
||
15 | class ValueObject implements \ArrayAccess, \JsonSerializable |
||
16 | { |
||
17 | /** |
||
18 | * @var int |
||
19 | */ |
||
20 | public const KEEP_ALL = 0x00; |
||
21 | |||
22 | /** |
||
23 | * @var int |
||
24 | */ |
||
25 | public const SKIP_NULL = 0x01; |
||
26 | |||
27 | /** |
||
28 | * @var int |
||
29 | */ |
||
30 | public const SKIP_EMPTY = 0x02; |
||
31 | |||
32 | /** |
||
33 | * All of the attributes set on the container. |
||
34 | * |
||
35 | * @var array |
||
36 | */ |
||
37 | protected $attributes = []; |
||
38 | |||
39 | /** |
||
40 | * @var bool |
||
41 | */ |
||
42 | protected $skip; |
||
43 | |||
44 | /** |
||
45 | * Create a new ValueObject container instance. |
||
46 | * |
||
47 | * @param iterable $attributes |
||
48 | * @param int $skip |
||
49 | */ |
||
50 | public function __construct(iterable $attributes = [], int $skip = self::KEEP_ALL) |
||
55 | |||
56 | /** |
||
57 | * Get an attribute from the container. |
||
58 | * |
||
59 | * @param string $key |
||
60 | * @param mixed $default |
||
61 | * @return mixed |
||
62 | */ |
||
63 | public function get(string $key, $default = null) |
||
71 | |||
72 | /** |
||
73 | * Set an attribute to the container. |
||
74 | * |
||
75 | * @param string|int $key |
||
76 | * @param mixed $value |
||
77 | * @return $this |
||
78 | */ |
||
79 | public function set($key, $value): self |
||
92 | |||
93 | /** |
||
94 | * Set the attributes to the container. |
||
95 | * |
||
96 | * @param iterable $attributes |
||
97 | * @return $this |
||
98 | */ |
||
99 | public function setAttributes(iterable $attributes = []): self |
||
107 | |||
108 | /** |
||
109 | * Get the attributes from the container. |
||
110 | * |
||
111 | * @return array |
||
112 | */ |
||
113 | public function getAttributes(): array |
||
117 | |||
118 | /** |
||
119 | * Convert the ValueObject instance to an array. |
||
120 | * |
||
121 | * @return array |
||
122 | */ |
||
123 | public function toArray(): array |
||
127 | |||
128 | /** |
||
129 | * @return mixed|object |
||
130 | */ |
||
131 | public function toObject() |
||
135 | |||
136 | /** |
||
137 | * Convert the object into something JSON serializable. |
||
138 | * |
||
139 | * @return array|mixed |
||
140 | */ |
||
141 | public function jsonSerialize() |
||
153 | |||
154 | /** |
||
155 | * Convert the ValueObject instance to JSON. |
||
156 | * |
||
157 | * @param int $options |
||
158 | * @return string |
||
159 | */ |
||
160 | public function toJson(int $options = 0): string |
||
164 | |||
165 | /** |
||
166 | * Determine if the given offset exists. |
||
167 | * |
||
168 | * @param string $offset |
||
169 | * @return bool |
||
170 | */ |
||
171 | public function offsetExists($offset): bool |
||
175 | |||
176 | /** |
||
177 | * Get the value for a given offset. |
||
178 | * |
||
179 | * @param string|int $offset |
||
180 | * @return mixed |
||
181 | */ |
||
182 | public function offsetGet($offset) |
||
186 | |||
187 | /** |
||
188 | * Set the value at the given offset. |
||
189 | * |
||
190 | * @param string|int $offset |
||
191 | * @param mixed $value |
||
192 | * @return void |
||
193 | */ |
||
194 | public function offsetSet($offset, $value): void |
||
198 | |||
199 | /** |
||
200 | * Unset the value at the given offset. |
||
201 | * |
||
202 | * @param string $offset |
||
203 | * @return void |
||
204 | */ |
||
205 | public function offsetUnset($offset): void |
||
209 | |||
210 | /** |
||
211 | * Handle dynamic calls to the container to set attributes. |
||
212 | * |
||
213 | * @param string $method |
||
214 | * @param array $parameters |
||
215 | * @return $this |
||
216 | */ |
||
217 | public function __call(string $method, array $parameters = []): self |
||
223 | |||
224 | /** |
||
225 | * Dynamically retrieve the value of an attribute. |
||
226 | * |
||
227 | * @param string $key |
||
228 | * @return mixed |
||
229 | */ |
||
230 | public function __get(string $key) |
||
234 | |||
235 | /** |
||
236 | * Dynamically set the value of an attribute. |
||
237 | * |
||
238 | * @param string $key |
||
239 | * @param mixed $value |
||
240 | * @return void |
||
241 | */ |
||
242 | public function __set(string $key, $value): void |
||
246 | |||
247 | /** |
||
248 | * Dynamically check if an attribute is set. |
||
249 | * |
||
250 | * @param string $key |
||
251 | * @return bool |
||
252 | */ |
||
253 | public function __isset(string $key): bool |
||
257 | |||
258 | /** |
||
259 | * Dynamically unset an attribute. |
||
260 | * |
||
261 | * @param string $key |
||
262 | * @return void |
||
263 | */ |
||
264 | public function __unset(string $key): void |
||
268 | |||
269 | /** |
||
270 | * @return array |
||
271 | */ |
||
272 | public function __debugInfo() |
||
276 | } |
||
277 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.