-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathArraydotify.php
More file actions
308 lines (261 loc) · 6.68 KB
/
Copy pathArraydotify.php
File metadata and controls
308 lines (261 loc) · 6.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
<?php
declare(strict_types=1);
namespace Bow\Support;
use ArrayAccess;
class Arraydotify implements ArrayAccess
{
/**
* The array collection in dot notation
*
* @var array
*/
private array $items = [];
/**
* The original array structure
*
* @var array
*/
private array $origin = [];
/**
* Arraydotify constructor.
*
* @param array $items
*/
public function __construct(array $items = [])
{
$this->origin = $items;
$this->items = $this->dotify($items);
}
/**
* Convert a multi-dimensional array to dot notation
*
* @param array $items
* @param string $prepend
* @return array
*/
private function dotify(array $items, string $prepend = ''): array
{
$dot = [];
foreach ($items as $key => $value) {
$dotKey = $prepend . $key;
if (is_array($value) || is_object($value)) {
$dot = array_merge(
$dot,
$this->dotify((array) $value, $dotKey . '.')
);
} else {
$dot[$dotKey] = $value;
}
}
return $dot;
}
/**
* Make array dotify (static factory method)
*
* @param array $items
* @return Arraydotify
*/
public static function make(array $items = []): Arraydotify
{
return new self($items);
}
/**
* Get the original array
*
* @return array
*/
public function toArray(): array
{
return $this->origin;
}
/**
* Get a value from the array using dot notation
*
* @param mixed $offset
* @return mixed
*/
public function &offsetGet(mixed $offset): mixed
{
// Try to get from dotified items first
if (isset($this->items[$offset])) {
$value = $this->items[$offset];
return $value;
}
// Try to find nested array in origin and return by reference
$value = $this->findByReference($this->origin, $offset);
return $value;
}
/**
* Check if a key exists in the array using dot notation
*
* @param mixed $offset
* @return bool
*/
public function offsetExists(mixed $offset): bool
{
if (isset($this->items[$offset])) {
return true;
}
$value = $this->find($this->origin, $offset);
return $value !== null;
}
/**
* Get the dotified array
*
* @return array
*/
public function getDotified(): array
{
return $this->items;
}
/**
* Check if the array has a key using dot notation
*
* @param string $key
* @return bool
*/
public function has(string $key): bool
{
return $this->offsetExists($key);
}
/**
* Get a value using dot notation with a default fallback
*
* @param string $key
* @param mixed $default
* @return mixed
*/
public function get(string $key, mixed $default = null): mixed
{
$value = $this->offsetGet($key);
return $value ?? $default;
}
/**
* Set a value using dot notation
*
* @param string $key
* @param mixed $value
* @return void
*/
public function set(string $key, mixed $value): void
{
$this->offsetSet($key, $value);
}
/**
* Find a value in the original array using dot notation
*
* @param array $array
* @param string $key
* @return mixed
*/
private function find(array $array, string $key): mixed
{
if (empty($key)) {
return null;
}
$keys = explode('.', $key);
foreach ($keys as $segment) {
if (!is_array($array) || !array_key_exists($segment, $array)) {
return null;
}
$array = $array[$segment];
}
return $array;
}
/**
* Find a value in the original array by reference using dot notation
*
* @param array $array
* @param string $key
* @return mixed
*/
private function &findByReference(array &$array, string $key): mixed
{
if (empty($key)) {
$null = null;
return $null;
}
$keys = explode('.', $key);
$current = &$array;
foreach ($keys as $segment) {
if (!is_array($current) || !array_key_exists($segment, $current)) {
$null = null;
return $null;
}
$current = &$current[$segment];
}
return $current;
}
/**
* Set a value in the array using dot notation
*
* @param mixed $offset
* @param mixed $value
* @return void
*/
public function offsetSet(mixed $offset, mixed $value): void
{
if (is_null($offset)) {
$this->origin[] = $value;
} else {
$this->dataSet($this->origin, $offset, $value);
}
// Rebuild dotified array
$this->items = $this->dotify($this->origin);
}
/**
* Unset a value from the array using dot notation
*
* @param mixed $offset
* @return void
*/
public function offsetUnset(mixed $offset): void
{
if (isset($this->items[$offset])) {
unset($this->items[$offset]);
}
$this->dataUnset($this->origin, $offset);
// Rebuild dotified array
$this->items = $this->dotify($this->origin);
}
/**
* Set a value in an array using dot notation
*
* @param array $array
* @param string $key
* @param mixed $value
* @return void
*/
private function dataSet(array &$array, string $key, mixed $value): void
{
$keys = explode('.', $key);
while (count($keys) > 1) {
$segment = array_shift($keys);
// Create nested array if it doesn't exist or isn't an array
if (!isset($array[$segment]) || !is_array($array[$segment])) {
$array[$segment] = [];
}
$array = &$array[$segment];
}
$array[array_shift($keys)] = $value;
}
/**
* Unset a value from an array using dot notation
*
* @param array $array
* @param string $key
* @return void
*/
private function dataUnset(array &$array, string $key): void
{
$keys = explode('.', $key);
while (count($keys) > 1) {
$segment = array_shift($keys);
if (!isset($array[$segment]) || !is_array($array[$segment])) {
return;
}
$array = &$array[$segment];
}
unset($array[array_shift($keys)]);
}
}