|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Koded package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Mihail Binev <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* Please view the LICENSE distributed with this source code |
|
9
|
|
|
* for the full copyright and license information. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Koded\Stdlib; |
|
13
|
|
|
|
|
14
|
|
|
use RecursiveArrayIterator; |
|
15
|
|
|
use RecursiveIteratorIterator; |
|
16
|
|
|
use function array_key_exists; |
|
17
|
|
|
use function array_push; |
|
18
|
|
|
use function array_slice; |
|
19
|
|
|
use function explode; |
|
20
|
|
|
use function is_array; |
|
21
|
|
|
use function join; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Class ExtendedArguments |
|
25
|
|
|
* |
|
26
|
|
|
* - NULL key is not supported; also it doesn't make sense |
|
27
|
|
|
* - boolean is a wrong type for the key; do not use a boolean key (it's juggled into a string) |
|
28
|
|
|
* |
|
29
|
|
|
* Use string values for the keys and you'll be golden. |
|
30
|
|
|
*/ |
|
31
|
|
|
class ExtendedArguments extends Arguments |
|
32
|
|
|
{ |
|
33
|
4 |
|
public function get(string $index, mixed $default = null): mixed |
|
34
|
|
|
{ |
|
35
|
4 |
|
return $this->find($index, $default); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
1 |
|
public function set(string $index, mixed $value): static |
|
39
|
|
|
{ |
|
40
|
1 |
|
$storage = &$this->storage; |
|
41
|
1 |
|
foreach (explode('.', $index) as $i) { |
|
42
|
1 |
|
if (false === is_array($storage[$i]) || |
|
43
|
1 |
|
false === array_key_exists($i, $storage) |
|
44
|
|
|
) { |
|
45
|
1 |
|
$storage[$i] = []; |
|
46
|
|
|
} |
|
47
|
1 |
|
$storage = &$storage[$i]; |
|
48
|
|
|
} |
|
49
|
1 |
|
$storage = $value; |
|
50
|
1 |
|
return $this; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
1 |
|
public function append(string $index, mixed $value): static |
|
54
|
|
|
{ |
|
55
|
1 |
|
$partial = (array)$this->get($index); |
|
56
|
1 |
|
array_push($partial, $value); |
|
57
|
1 |
|
$this->set($index, $partial); |
|
58
|
1 |
|
return $this; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
1 |
|
public function has(mixed $index): bool |
|
62
|
|
|
{ |
|
63
|
1 |
|
$storage = & $this->storage; |
|
64
|
1 |
|
foreach (explode('.', $index) as $i) { |
|
65
|
1 |
|
if (false === is_array($storage) || |
|
66
|
1 |
|
false === array_key_exists($i, $storage) |
|
67
|
|
|
) { |
|
68
|
1 |
|
return false; |
|
69
|
|
|
} |
|
70
|
1 |
|
$storage = &$storage[$i]; |
|
71
|
|
|
} |
|
72
|
1 |
|
return true; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
1 |
|
public function delete(string $index): static |
|
76
|
|
|
{ |
|
77
|
1 |
|
$storage = &$this->storage; |
|
78
|
1 |
|
foreach (explode('.', $index) as $i) { |
|
79
|
1 |
|
if (false === is_array($storage[$i]) || |
|
80
|
1 |
|
false === array_key_exists($i, $storage) |
|
81
|
|
|
) { |
|
82
|
1 |
|
continue; |
|
83
|
|
|
} |
|
84
|
1 |
|
$storage = &$storage[$i]; |
|
85
|
|
|
} |
|
86
|
1 |
|
if (isset($i)) { |
|
87
|
1 |
|
unset($storage[$i]); |
|
88
|
|
|
} |
|
89
|
1 |
|
return $this; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
1 |
|
public function extract(array $indexes): array |
|
93
|
|
|
{ |
|
94
|
1 |
|
$found = []; |
|
95
|
1 |
|
foreach ($indexes as $index) { |
|
96
|
1 |
|
$found[$index] = $this->find($index); |
|
97
|
|
|
} |
|
98
|
1 |
|
return $found; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
2 |
|
public function flatten(): static |
|
102
|
|
|
{ |
|
103
|
2 |
|
$indexes = []; |
|
104
|
2 |
|
$flatten = []; |
|
105
|
2 |
|
$iterator = new RecursiveIteratorIterator( |
|
106
|
2 |
|
new RecursiveArrayIterator($this->storage), |
|
107
|
2 |
|
RecursiveIteratorIterator::SELF_FIRST |
|
108
|
|
|
); |
|
109
|
2 |
|
foreach ($iterator as $index => $value) { |
|
110
|
2 |
|
$indexes[$iterator->getDepth()] = $index; |
|
111
|
2 |
|
if (false === is_array($value)) { |
|
112
|
2 |
|
$_ = join('.', array_slice($indexes, 0, $iterator->getDepth() + 1)); |
|
113
|
2 |
|
$flatten[$_] = $value; |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
2 |
|
return new static($flatten); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|