1 | <?php |
||
2 | /** |
||
3 | * This file is part of the O2System Framework package. |
||
4 | * |
||
5 | * For the full copyright and license information, please view the LICENSE |
||
6 | * file that was distributed with this source code. |
||
7 | * |
||
8 | * @author Steeve Andrian Salim |
||
9 | * @copyright Copyright (c) Steeve Andrian Salim |
||
10 | */ |
||
11 | |||
12 | // ------------------------------------------------------------------------ |
||
13 | |||
14 | namespace O2System\Spl\DataStructures; |
||
15 | |||
16 | // ------------------------------------------------------------------------ |
||
17 | |||
18 | use O2System\Spl\Iterators\ArrayIterator; |
||
19 | use Traversable; |
||
20 | |||
21 | class SplArrayStorage implements |
||
22 | \Countable, |
||
23 | \IteratorAggregate, |
||
24 | \Serializable, |
||
25 | \JsonSerializable, |
||
26 | \ArrayAccess |
||
27 | { |
||
28 | use Traits\ArrayConversionTrait; |
||
29 | use Traits\ArrayFunctionsTrait; |
||
30 | |||
31 | /** |
||
32 | * SplArrayStorage |
||
33 | * |
||
34 | * @var array |
||
35 | */ |
||
36 | protected $storage = []; |
||
37 | |||
38 | // ------------------------------------------------------------------------ |
||
39 | |||
40 | /** |
||
41 | * SplArrayStorage::__isset |
||
42 | * |
||
43 | * @param mixed $offset <p> |
||
44 | * An offset to check for. |
||
45 | * </p> |
||
46 | * |
||
47 | * @return boolean true on success or false on failure. |
||
48 | * </p> |
||
49 | * <p> |
||
50 | * The return value will be casted to boolean if non-boolean was returned. |
||
51 | */ |
||
52 | public function __isset($offset) |
||
53 | { |
||
54 | return isset($this->storage[ $offset ]); |
||
55 | } |
||
56 | |||
57 | // ------------------------------------------------------------------------ |
||
58 | |||
59 | /** |
||
60 | * SplArrayStorage::__unset |
||
61 | * |
||
62 | * Unset a given offset. |
||
63 | * |
||
64 | * @param mixed $offset <p> |
||
65 | * The offset to unset. |
||
66 | * </p> |
||
67 | * |
||
68 | * @return void |
||
69 | */ |
||
70 | public function __unset($offset) |
||
71 | { |
||
72 | unset($this->storage[ $offset ]); |
||
73 | } |
||
74 | |||
75 | // ------------------------------------------------------------------------ |
||
76 | |||
77 | /** |
||
78 | * SplArrayStorage::__get |
||
79 | * |
||
80 | * Offset to retrieve |
||
81 | * |
||
82 | * @param mixed $offset <p> |
||
83 | * An offset to check for. |
||
84 | * </p> |
||
85 | * |
||
86 | * @return mixed Can return all value types. |
||
87 | */ |
||
88 | public function &__get($offset) |
||
89 | { |
||
90 | return $this->offsetGet($offset); |
||
91 | } |
||
92 | |||
93 | // ------------------------------------------------------------------------ |
||
94 | |||
95 | /** |
||
96 | * SplArrayStorage::__set |
||
97 | * |
||
98 | * @param mixed $offset <p> |
||
99 | * The offset to assign the value to. |
||
100 | * </p> |
||
101 | * @param mixed $value <p> |
||
102 | * The value to set. |
||
103 | * </p> |
||
104 | */ |
||
105 | public function __set($offset, $value) |
||
106 | { |
||
107 | $this->offsetSet($offset, $value); |
||
108 | } |
||
109 | |||
110 | // ------------------------------------------------------------------------ |
||
111 | |||
112 | /** |
||
113 | * SplArrayStorage::offsetGet |
||
114 | * |
||
115 | * Offset to retrieve |
||
116 | * |
||
117 | * @link http://php.net/manual/en/arrayaccess.offsetget.php |
||
118 | * |
||
119 | * @param mixed $offset <p> |
||
120 | * The offset to retrieve. |
||
121 | * </p> |
||
122 | * |
||
123 | * @return mixed Can return all value types. |
||
124 | * @since 5.0.0 |
||
125 | */ |
||
126 | public function offsetGet($offset) |
||
127 | { |
||
128 | if ($this->offsetExists($offset)) { |
||
129 | return $this->storage[ $offset ]; |
||
130 | } else { |
||
131 | $this->storage[ $offset ] = []; |
||
132 | |||
133 | return $this->storage[ $offset ]; |
||
134 | } |
||
135 | } |
||
136 | |||
137 | // ------------------------------------------------------------------------ |
||
138 | |||
139 | /** |
||
140 | * SplArrayStorage::offsetExists |
||
141 | * |
||
142 | * Whether a offset exists |
||
143 | * |
||
144 | * @link http://php.net/manual/en/arrayaccess.offsetexists.php |
||
145 | * |
||
146 | * @param mixed $offset <p> |
||
147 | * An offset to check for. |
||
148 | * </p> |
||
149 | * |
||
150 | * @return boolean true on success or false on failure. |
||
151 | * </p> |
||
152 | * <p> |
||
153 | * The return value will be casted to boolean if non-boolean was returned. |
||
154 | * @since 5.0.0 |
||
155 | */ |
||
156 | public function offsetExists($offset) |
||
157 | { |
||
158 | return (bool)isset($this->storage[ $offset ]); |
||
159 | } |
||
160 | |||
161 | // ------------------------------------------------------------------------ |
||
162 | |||
163 | /** |
||
164 | * SplArrayStorage::offsetSet |
||
165 | * |
||
166 | * Offset to set |
||
167 | * |
||
168 | * @link http://php.net/manual/en/arrayaccess.offsetset.php |
||
169 | * |
||
170 | * @param mixed $offset <p> |
||
171 | * The offset to assign the value to. |
||
172 | * </p> |
||
173 | * @param mixed $value <p> |
||
174 | * The value to set. |
||
175 | * </p> |
||
176 | * |
||
177 | * @return void |
||
178 | * @since 5.0.0 |
||
179 | */ |
||
180 | public function offsetSet($offset, $value) |
||
181 | { |
||
182 | $this->storage[ $offset ] = $value; |
||
183 | } |
||
184 | |||
185 | // ------------------------------------------------------------------------ |
||
186 | |||
187 | /** |
||
188 | * SplArrayStorage::offsetUnset |
||
189 | * |
||
190 | * Offset to unset |
||
191 | * |
||
192 | * @link http://php.net/manual/en/arrayaccess.offsetunset.php |
||
193 | * |
||
194 | * @param mixed $offset <p> |
||
195 | * The offset to unset. |
||
196 | * </p> |
||
197 | * |
||
198 | * @return void |
||
199 | * @since 5.0.0 |
||
200 | */ |
||
201 | public function offsetUnset($offset) |
||
202 | { |
||
203 | if (isset($this->storage[ $offset ])) { |
||
204 | unset($this->storage[ $offset ]); |
||
205 | } |
||
206 | } |
||
207 | |||
208 | // ------------------------------------------------------------------------ |
||
209 | |||
210 | /** |
||
211 | * SplArrayStorage::offsetGetFilter |
||
212 | * |
||
213 | * Get filtered array storage value |
||
214 | * |
||
215 | * @param $offset |
||
216 | * @param null $filter |
||
0 ignored issues
–
show
Documentation
Bug
introduced
by
![]() |
|||
217 | * |
||
218 | * @return mixed|null |
||
219 | */ |
||
220 | public function offsetGetFilter($offset, $filter = null) |
||
221 | { |
||
222 | if ($this->offsetExists($offset)) { |
||
223 | $storage = $this->offsetGet($offset); |
||
224 | |||
225 | if (is_array($storage) AND is_array($filter)) { |
||
0 ignored issues
–
show
|
|||
226 | return filter_var_array($offset, $filter); |
||
227 | } elseif (is_array($storage) AND isset($filter)) { |
||
228 | foreach ($storage as $key => $value) { |
||
229 | $storage[ $key ] = filter_var($value, $filter); |
||
230 | } |
||
231 | } elseif (isset($filter)) { |
||
232 | return filter_var($storage, $filter); |
||
233 | } |
||
234 | |||
235 | return $storage; |
||
236 | } |
||
237 | |||
238 | return false; |
||
239 | } |
||
240 | |||
241 | // ------------------------------------------------------------------------ |
||
242 | |||
243 | /** |
||
244 | * SplArrayStorage::append |
||
245 | * |
||
246 | * append array of values into the storage |
||
247 | * |
||
248 | * @param array $values Variable list of arrays to merge. |
||
249 | * |
||
250 | * @return array The array merged copy of the resulting array |
||
251 | */ |
||
252 | public function append(array $values) |
||
253 | { |
||
254 | $this->storage = array_merge($this->storage, $values); |
||
255 | } |
||
256 | |||
257 | // ------------------------------------------------------------------------ |
||
258 | |||
259 | /** |
||
260 | * SplArrayStorage::merge |
||
261 | * |
||
262 | * Merge array of values into the storage |
||
263 | * |
||
264 | * @param array $values Variable list of arrays to merge. |
||
265 | * |
||
266 | * @return array The array merged copy of the resulting array |
||
267 | */ |
||
268 | public function merge(array $values) |
||
269 | { |
||
270 | $storage = $this->getArrayCopy(); |
||
271 | $storage = array_merge($storage, $values); |
||
272 | |||
273 | $this->exchangeArray($storage); |
||
274 | |||
275 | return $storage; |
||
276 | } |
||
277 | |||
278 | // ------------------------------------------------------------------------ |
||
279 | |||
280 | /** |
||
281 | * SplArrayStorage::getArrayCopy |
||
282 | * |
||
283 | * Creates a copy of the storage. |
||
284 | * |
||
285 | * @return array A copy of the storage. |
||
286 | */ |
||
287 | public function getArrayCopy() |
||
288 | { |
||
289 | return $this->storage; |
||
290 | } |
||
291 | |||
292 | // ------------------------------------------------------------------------ |
||
293 | |||
294 | /** |
||
295 | * SplArrayStorage::exchangeArray |
||
296 | * |
||
297 | * Exchange the array for another one. |
||
298 | * |
||
299 | * @link http://php.net/manual/en/arrayobject.exchangearray.php |
||
300 | * |
||
301 | * @param array $values <p> |
||
302 | * The new array or object to exchange with the current array. |
||
303 | * </p> |
||
304 | * |
||
305 | * @return array the old array. |
||
306 | * @since 5.1.0 |
||
307 | */ |
||
308 | public function exchangeArray(array $values) |
||
309 | { |
||
310 | $oldStorage = $this->storage; |
||
311 | $this->storage = $values; |
||
312 | |||
313 | return $oldStorage; |
||
314 | } |
||
315 | |||
316 | // ------------------------------------------------------------------------ |
||
317 | |||
318 | /** |
||
319 | * SplArrayStorage::asort |
||
320 | * |
||
321 | * Sort the entries by value |
||
322 | * |
||
323 | * @link http://php.net/manual/en/arrayobject.asort.php |
||
324 | * |
||
325 | * @param int $sortFlags |
||
326 | * |
||
327 | * @return void |
||
328 | * @since 5.2.0 |
||
329 | */ |
||
330 | public function asort($sortFlags = SORT_REGULAR) |
||
331 | { |
||
332 | asort($this->storage, $sortFlags); |
||
333 | } |
||
334 | |||
335 | // ------------------------------------------------------------------------ |
||
336 | |||
337 | /** |
||
338 | * SplArrayStorage::ksort |
||
339 | * |
||
340 | * Sort the entries by key |
||
341 | * |
||
342 | * @link http://php.net/manual/en/arrayobject.ksort.php |
||
343 | * |
||
344 | * @param int $sortFlags |
||
345 | * |
||
346 | * @return void |
||
347 | * @since 5.2.0 |
||
348 | */ |
||
349 | public function ksort($sortFlags = SORT_REGULAR) |
||
350 | { |
||
351 | ksort($this->storage, $sortFlags); |
||
352 | } |
||
353 | |||
354 | // ------------------------------------------------------------------------ |
||
355 | |||
356 | /** |
||
357 | * SplArrayStorage::uasort |
||
358 | * |
||
359 | * Sort the entries with a user-defined comparison function and maintain key association |
||
360 | * |
||
361 | * @link http://php.net/manual/en/arrayobject.uasort.php |
||
362 | * |
||
363 | * @param callback $comparisonFunction <p> |
||
364 | * Function <i>comparisonFunction</i> should accept two |
||
365 | * parameters which will be filled by pairs of entries. |
||
366 | * The comparison function must return an integer less than, equal |
||
367 | * to, or greater than zero if the first argument is considered to |
||
368 | * be respectively less than, equal to, or greater than the |
||
369 | * second. |
||
370 | * </p> |
||
371 | * |
||
372 | * @return void |
||
373 | * @since 5.2.0 |
||
374 | */ |
||
375 | public function uasort($comparisonFunction) |
||
376 | { |
||
377 | uasort($this->storage, $comparisonFunction); |
||
378 | } |
||
379 | |||
380 | // ------------------------------------------------------------------------ |
||
381 | |||
382 | /** |
||
383 | * SplArrayStorage::uksort |
||
384 | * |
||
385 | * Sort the entries by keys using a user-defined comparison function |
||
386 | * |
||
387 | * @link http://php.net/manual/en/arrayobject.uksort.php |
||
388 | * |
||
389 | * @param callback $comparisonFunction <p> |
||
390 | * The callback comparison function. |
||
391 | * </p> |
||
392 | * <p> |
||
393 | * Function <i>comparisonFunction</i> should accept two |
||
394 | * parameters which will be filled by pairs of entry keys. |
||
395 | * The comparison function must return an integer less than, equal |
||
396 | * to, or greater than zero if the first argument is considered to |
||
397 | * be respectively less than, equal to, or greater than the |
||
398 | * second. |
||
399 | * </p> |
||
400 | * |
||
401 | * @return void |
||
402 | * @since 5.2.0 |
||
403 | */ |
||
404 | public function uksort($comparisonFunction) |
||
405 | { |
||
406 | uksort($this->storage, $comparisonFunction); |
||
407 | } |
||
408 | |||
409 | // ------------------------------------------------------------------------ |
||
410 | |||
411 | /** |
||
412 | * SplArrayStorage::natsort |
||
413 | * |
||
414 | * Sort entries using a "natural order" algorithm |
||
415 | * |
||
416 | * @link http://php.net/manual/en/arrayobject.natsort.php |
||
417 | * @return void |
||
418 | * @since 5.2.0 |
||
419 | */ |
||
420 | public function natsort() |
||
421 | { |
||
422 | natsort($this->storage); |
||
423 | } |
||
424 | |||
425 | // ------------------------------------------------------------------------ |
||
426 | |||
427 | /** |
||
428 | * SplArrayStorage::natcasesort |
||
429 | * |
||
430 | * Sort an array using a case insensitive "natural order" algorithm |
||
431 | * |
||
432 | * @link http://php.net/manual/en/arrayobject.natcasesort.php |
||
433 | * @return void |
||
434 | * @since 5.2.0 |
||
435 | */ |
||
436 | public function natcasesort() |
||
437 | { |
||
438 | natcasesort($this->storage); |
||
439 | } |
||
440 | |||
441 | // ------------------------------------------------------------------------ |
||
442 | |||
443 | /** |
||
444 | * SplArrayStorage::isEmpty |
||
445 | * |
||
446 | * Checks if the array storage is empty. |
||
447 | * |
||
448 | * @return bool |
||
449 | */ |
||
450 | public function isEmpty() |
||
451 | { |
||
452 | return (bool)empty($this->storage); |
||
453 | } |
||
454 | |||
455 | // ------------------------------------------------------------------------ |
||
456 | |||
457 | /** |
||
458 | * SplArrayStorage::has |
||
459 | * |
||
460 | * Checks if a value exists in the storage. |
||
461 | * |
||
462 | * @param mixed $needle The searched value. |
||
463 | * @param bool $strict If the third parameter strict is set to TRUE then the in_array() function will also check |
||
464 | * the types of the needle in the haystack. |
||
465 | * |
||
466 | * @return bool |
||
467 | */ |
||
468 | public function has($needle, $strict = false) |
||
469 | { |
||
470 | return in_array($needle, $this->getArrayCopy(), $strict); |
||
471 | } |
||
472 | |||
473 | // ------------------------------------------------------------------------ |
||
474 | |||
475 | /** |
||
476 | * SplArrayStorage::serialize |
||
477 | * |
||
478 | * String representation of object |
||
479 | * |
||
480 | * @link http://php.net/manual/en/serializable.serialize.php |
||
481 | * @return string the string representation of the object or null |
||
482 | * @since 5.1.0 |
||
483 | */ |
||
484 | public function serialize() |
||
485 | { |
||
486 | return serialize($this->storage); |
||
487 | } |
||
488 | |||
489 | // ------------------------------------------------------------------------ |
||
490 | |||
491 | /** |
||
492 | * SplArrayStorage::unserialize |
||
493 | * |
||
494 | * Constructs the storage |
||
495 | * |
||
496 | * @link http://php.net/manual/en/serializable.unserialize.php |
||
497 | * |
||
498 | * @param string $serialized <p> |
||
499 | * The string representation of the object. |
||
500 | * </p> |
||
501 | * |
||
502 | * @return void |
||
503 | * @since 5.1.0 |
||
504 | */ |
||
505 | public function unserialize($serialized) |
||
506 | { |
||
507 | $this->storage = unserialize($serialized); |
||
508 | } |
||
509 | |||
510 | // ------------------------------------------------------------------------ |
||
511 | |||
512 | /** |
||
513 | * SplArrayStorage::count |
||
514 | * |
||
515 | * Count elements of an object |
||
516 | * |
||
517 | * @link http://php.net/manual/en/countable.count.php |
||
518 | * @return int The custom count as an integer. |
||
519 | * </p> |
||
520 | * <p> |
||
521 | * The return value is cast to an integer. |
||
522 | * @since 5.1.0 |
||
523 | */ |
||
524 | public function count() |
||
525 | { |
||
526 | return count($this->storage); |
||
527 | } |
||
528 | |||
529 | // ------------------------------------------------------------------------ |
||
530 | |||
531 | /** |
||
532 | * SplArrayStorage::jsonSerialize |
||
533 | * |
||
534 | * Specify data which should be serialized to JSON |
||
535 | * |
||
536 | * @link http://php.net/manual/en/jsonserializable.jsonserialize.php |
||
537 | * @return mixed data which can be serialized by <b>json_encode</b>, |
||
538 | * which is a value of any type other than a resource. |
||
539 | * @since 5.4.0 |
||
540 | */ |
||
541 | public function jsonSerialize() |
||
542 | { |
||
543 | return $this->storage; |
||
544 | } |
||
545 | |||
546 | // ------------------------------------------------------------------------ |
||
547 | |||
548 | /** |
||
549 | * SplArrayStorage::getIterator |
||
550 | * |
||
551 | * Retrieve an external iterator |
||
552 | * |
||
553 | * @link http://php.net/manual/en/iteratoraggregate.getiterator.php |
||
554 | * @return Traversable An instance of an object implementing <b>Iterator</b> or |
||
555 | * <b>Traversable</b> |
||
556 | * @since 5.0.0 |
||
557 | */ |
||
558 | public function getIterator() |
||
559 | { |
||
560 | return new ArrayIterator($this->storage); |
||
561 | } |
||
562 | } |