|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Arrays; |
|
6
|
|
|
|
|
7
|
|
|
use Closure; |
|
8
|
|
|
use InvalidArgumentException; |
|
9
|
|
|
use Throwable; |
|
10
|
|
|
use Yiisoft\Strings\NumericHelper; |
|
11
|
|
|
use Yiisoft\Strings\StringHelper; |
|
12
|
|
|
|
|
13
|
|
|
use function array_key_exists; |
|
14
|
|
|
use function count; |
|
15
|
|
|
use function gettype; |
|
16
|
|
|
use function in_array; |
|
17
|
|
|
use function is_array; |
|
18
|
|
|
use function is_float; |
|
19
|
|
|
use function is_int; |
|
20
|
|
|
use function is_object; |
|
21
|
|
|
use function is_string; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Yii array helper provides static methods allowing you to deal with arrays more efficiently. |
|
25
|
|
|
* |
|
26
|
|
|
* @psalm-type ArrayKey = float|int|string|array<array-key,float|int|string> |
|
27
|
|
|
* @psalm-type ArrayPath = float|int|string|array<array-key,float|int|string|array<array-key,float|int|string>> |
|
28
|
|
|
*/ |
|
29
|
|
|
final class ArrayHelper |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* Converts an object or an array of objects into an array. |
|
33
|
|
|
* |
|
34
|
|
|
* For example: |
|
35
|
|
|
* |
|
36
|
|
|
* ```php |
|
37
|
|
|
* [ |
|
38
|
|
|
* Post::class => [ |
|
39
|
|
|
* 'id', |
|
40
|
|
|
* 'title', |
|
41
|
|
|
* 'createTime' => 'created_at', |
|
42
|
|
|
* 'length' => function ($post) { |
|
43
|
|
|
* return strlen($post->content); |
|
44
|
|
|
* }, |
|
45
|
|
|
* ], |
|
46
|
|
|
* ] |
|
47
|
|
|
* ``` |
|
48
|
|
|
* |
|
49
|
|
|
* The result of `ArrayHelper::toArray($post, $properties)` could be like the following: |
|
50
|
|
|
* |
|
51
|
|
|
* ```php |
|
52
|
|
|
* [ |
|
53
|
|
|
* 'id' => 123, |
|
54
|
|
|
* 'title' => 'test', |
|
55
|
|
|
* 'createTime' => '2013-01-01 12:00AM', |
|
56
|
|
|
* 'length' => 301, |
|
57
|
|
|
* ] |
|
58
|
|
|
* ``` |
|
59
|
|
|
* |
|
60
|
|
|
* @param mixed $object The object to be converted into an array. |
|
61
|
|
|
* |
|
62
|
|
|
* It is possible to provide default way of converting object to array for a specific class by implementing |
|
63
|
|
|
* {@see \Yiisoft\Arrays\ArrayableInterface} in its class. |
|
64
|
|
|
* @param array $properties A mapping from object class names to the properties that need to put into |
|
65
|
|
|
* the resulting arrays. The properties specified for each class is an array of the following format: |
|
66
|
|
|
* |
|
67
|
|
|
* - A field name to include as is. |
|
68
|
|
|
* - A key-value pair of desired array key name and model column name to take value from. |
|
69
|
|
|
* - A key-value pair of desired array key name and a callback which returns value. |
|
70
|
|
|
* @param bool $recursive Whether to recursively converts properties which are objects into arrays. |
|
71
|
|
|
* |
|
72
|
|
|
* @return array The array representation of the object. |
|
73
|
|
|
*/ |
|
74
|
6 |
|
public static function toArray(mixed $object, array $properties = [], bool $recursive = true): array |
|
75
|
|
|
{ |
|
76
|
6 |
|
if (is_array($object)) { |
|
77
|
5 |
|
if ($recursive) { |
|
78
|
4 |
|
foreach ($object as $key => $value) { |
|
79
|
4 |
|
if (is_array($value) || is_object($value)) { |
|
80
|
4 |
|
$object[$key] = self::toArray($value, $properties); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
5 |
|
return $object; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
5 |
|
if (is_object($object)) { |
|
89
|
5 |
|
if (!empty($properties)) { |
|
90
|
1 |
|
$className = $object::class; |
|
91
|
1 |
|
if (!empty($properties[$className])) { |
|
92
|
1 |
|
$result = []; |
|
93
|
|
|
/** |
|
94
|
|
|
* @var int|string $key |
|
95
|
|
|
* @var string $name |
|
96
|
|
|
*/ |
|
97
|
1 |
|
foreach ($properties[$className] as $key => $name) { |
|
98
|
1 |
|
if (is_int($key)) { |
|
99
|
1 |
|
$result[$name] = $object->$name; |
|
100
|
|
|
} else { |
|
101
|
1 |
|
$result[$key] = self::getValue($object, $name); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
1 |
|
return $recursive ? self::toArray($result, $properties) : $result; |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
5 |
|
if ($object instanceof ArrayableInterface) { |
|
109
|
4 |
|
$result = $object->toArray([], [], $recursive); |
|
110
|
|
|
} else { |
|
111
|
4 |
|
$result = []; |
|
112
|
|
|
/** |
|
113
|
|
|
* @var string $key |
|
114
|
|
|
*/ |
|
115
|
4 |
|
foreach ($object as $key => $value) { |
|
116
|
4 |
|
$result[$key] = $value; |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
5 |
|
return $recursive ? self::toArray($result, $properties) : $result; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
1 |
|
return [$object]; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Merges two or more arrays into one recursively. If each array has an element with the same string key value, |
|
128
|
|
|
* the latter will overwrite the former (different from {@see array_merge_recursive()}). Recursive merging will be |
|
129
|
|
|
* conducted if both arrays have an element of array type and are having the same key. For integer-keyed elements, |
|
130
|
|
|
* the elements from the latter array will be appended to the former array. |
|
131
|
|
|
* |
|
132
|
|
|
* @param array ...$arrays Arrays to be merged. |
|
133
|
|
|
* |
|
134
|
|
|
* @return array The merged array (the original arrays are not changed). |
|
135
|
|
|
*/ |
|
136
|
4 |
|
public static function merge(...$arrays): array |
|
137
|
|
|
{ |
|
138
|
4 |
|
return self::doMerge($arrays, null); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Merges two or more arrays into one recursively with specified depth. If each array has an element with the same |
|
143
|
|
|
* string key value, the latter will overwrite the former (different from {@see array_merge_recursive()}). |
|
144
|
|
|
* Recursive merging will be conducted if both arrays have an element of array type and are having the same key. |
|
145
|
|
|
* For integer-keyed elements, the elements from the latter array will be appended to the former array. |
|
146
|
|
|
* |
|
147
|
|
|
* @param array[] $arrays Arrays to be merged. |
|
148
|
|
|
* @param int|null $depth The maximum depth that merging is recursively. `Null` means unlimited depth. |
|
149
|
|
|
* |
|
150
|
|
|
* @return array The merged array (the original arrays are not changed). |
|
151
|
|
|
*/ |
|
152
|
5 |
|
public static function parametrizedMerge(array $arrays, ?int $depth): array |
|
153
|
|
|
{ |
|
154
|
5 |
|
return self::doMerge($arrays, $depth); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Retrieves the value of an array element or object property with the given key or property name. |
|
159
|
|
|
* If the key does not exist in the array or object, the default value will be returned instead. |
|
160
|
|
|
* |
|
161
|
|
|
* Below are some usage examples, |
|
162
|
|
|
* |
|
163
|
|
|
* ```php |
|
164
|
|
|
* // Working with array: |
|
165
|
|
|
* $username = \Yiisoft\Arrays\ArrayHelper::getValue($_POST, 'username'); |
|
166
|
|
|
* |
|
167
|
|
|
* // Working with object: |
|
168
|
|
|
* $username = \Yiisoft\Arrays\ArrayHelper::getValue($user, 'username'); |
|
169
|
|
|
* |
|
170
|
|
|
* // Retrieve the value by matcher function: |
|
171
|
|
|
* $firstActiveMember = \Yiisoft\Arrays\ArrayHelper::getValue($users, function ($user, $key) { |
|
172
|
|
|
* return $user->type === 'member' && $user->isActive; |
|
173
|
|
|
* }); |
|
174
|
|
|
* |
|
175
|
|
|
* // Using an array of keys to retrieve the value: |
|
176
|
|
|
* $value = \Yiisoft\Arrays\ArrayHelper::getValue($versions, ['1.0', 'date']); |
|
177
|
|
|
* ``` |
|
178
|
|
|
* |
|
179
|
|
|
* @param array|object $array Array or object to extract value from. |
|
180
|
|
|
* @param array|Closure|float|int|string $key Key name of the array element, |
|
181
|
|
|
* an array of keys, object property name, object method like `getName()` or a callable. The callable function signature should be: |
|
182
|
|
|
* `function($value, $key)`. |
|
183
|
|
|
* @param mixed $default The default value to be returned if the specified array key does not exist. Not used when |
|
184
|
|
|
* getting value from an object. |
|
185
|
|
|
* |
|
186
|
|
|
* @psalm-param ArrayKey|Closure $key |
|
187
|
|
|
* |
|
188
|
|
|
* @return mixed The value of the element if found or the return value of callable is truthy, default value otherwise. |
|
189
|
|
|
*/ |
|
190
|
|
|
public static function getValue( |
|
191
|
95 |
|
array|object $array, |
|
192
|
|
|
array|Closure|float|int|string $key, |
|
193
|
|
|
mixed $default = null |
|
194
|
|
|
): mixed { |
|
195
|
|
|
if ($key instanceof Closure) { |
|
|
|
|
|
|
196
|
95 |
|
return self::getValueByMatcher($array, $key, $default); |
|
197
|
16 |
|
} |
|
198
|
|
|
|
|
199
|
|
|
if (is_array($key)) { |
|
|
|
|
|
|
200
|
87 |
|
/** @psalm-var array<mixed,string|int> $key */ |
|
201
|
|
|
$lastKey = array_pop($key); |
|
202
|
42 |
|
foreach ($key as $keyPart) { |
|
203
|
42 |
|
$array = self::getRootValue($array, $keyPart, null); |
|
204
|
39 |
|
if (!is_array($array) && !is_object($array)) { |
|
205
|
39 |
|
return $default; |
|
206
|
10 |
|
} |
|
207
|
|
|
} |
|
208
|
|
|
return self::getRootValue($array, $lastKey, $default); |
|
209
|
33 |
|
} |
|
210
|
|
|
|
|
211
|
|
|
return self::getRootValue($array, $key, $default); |
|
212
|
47 |
|
} |
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* @param mixed $array Array or object to extract value from, otherwise method will return $default. |
|
216
|
|
|
* @param float|int|string $key Key name of the array element, object property name or object method like `getValue()`. |
|
217
|
|
|
* @param mixed $default The default value to be returned if the specified array key does not exist. Not used when |
|
218
|
|
|
* getting value from an object. |
|
219
|
|
|
* |
|
220
|
|
|
* @return mixed The value of the element if found, default value otherwise. |
|
221
|
|
|
*/ |
|
222
|
|
|
private static function getRootValue(mixed $array, float|int|string $key, mixed $default): mixed |
|
223
|
114 |
|
{ |
|
224
|
|
|
if (is_array($array)) { |
|
225
|
114 |
|
$key = self::normalizeArrayKey($key); |
|
226
|
101 |
|
return array_key_exists($key, $array) ? $array[$key] : $default; |
|
227
|
101 |
|
} |
|
228
|
|
|
|
|
229
|
|
|
if (is_object($array)) { |
|
230
|
15 |
|
$key = (string) $key; |
|
231
|
15 |
|
|
|
232
|
|
|
if (str_ends_with($key, '()')) { |
|
233
|
15 |
|
$method = substr($key, 0, -2); |
|
234
|
1 |
|
/** @psalm-suppress MixedMethodCall */ |
|
235
|
|
|
return $array->$method(); |
|
236
|
1 |
|
} |
|
237
|
|
|
|
|
238
|
|
|
try { |
|
239
|
|
|
/** @psalm-suppress MixedPropertyFetch */ |
|
240
|
|
|
return $array::$$key; |
|
241
|
14 |
|
} catch (Throwable) { |
|
|
|
|
|
|
242
|
14 |
|
/** |
|
243
|
|
|
* This is expected to fail if the property does not exist, or __get() is not implemented. |
|
244
|
|
|
* It is not reliably possible to check whether a property is accessible beforehand. |
|
245
|
|
|
* |
|
246
|
|
|
* @psalm-suppress MixedPropertyFetch |
|
247
|
|
|
*/ |
|
248
|
|
|
return $array->$key; |
|
249
|
14 |
|
} |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
return $default; |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
private static function getValueByMatcher( |
|
256
|
|
|
array|object $array, |
|
257
|
|
|
Closure $match, |
|
258
|
|
|
mixed $default = null |
|
259
|
|
|
): mixed |
|
260
|
|
|
{ |
|
261
|
|
|
if (is_object($array)) { |
|
|
|
|
|
|
262
|
|
|
$array = self::getObjectVars($array); |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
foreach ($array as $key => $value) { |
|
266
|
|
|
if ($match($value, $key)) { |
|
267
|
|
|
return $value; |
|
268
|
|
|
} |
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
|
|
return $default; |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
/** |
|
275
|
|
|
* Retrieves the value of an array element or object property with the given key or property name. |
|
276
|
|
|
* If the key does not exist in the array or object, the default value will be returned instead. |
|
277
|
|
|
* |
|
278
|
|
|
* The key may be specified in a dot-separated format to retrieve the value of a sub-array or the property |
|
279
|
|
|
* of an embedded object. In particular, if the key is `x.y.z`, then the returned value would |
|
280
|
|
|
* be `$array['x']['y']['z']` or `$array->x->y->z` (if `$array` is an object). If `$array['x']` |
|
281
|
|
|
* or `$array->x` is neither an array nor an object, the default value will be returned. |
|
282
|
|
|
* Note that if the array already has an element `x.y.z`, then its value will be returned |
|
283
|
|
|
* instead of going through the sub-arrays. So it is better to be done specifying an array of key names |
|
284
|
|
|
* like `['x', 'y', 'z']`. |
|
285
|
|
|
* |
|
286
|
|
|
* Below are some usage examples, |
|
287
|
|
|
* |
|
288
|
|
|
* ```php |
|
289
|
|
|
* // Using separated format to retrieve the property of embedded object: |
|
290
|
|
|
* $street = \Yiisoft\Arrays\ArrayHelper::getValue($users, 'address.street'); |
|
291
|
37 |
|
* |
|
292
|
|
|
* // Using an array of keys to retrieve the value: |
|
293
|
|
|
* $value = \Yiisoft\Arrays\ArrayHelper::getValue($versions, ['1.0', 'date']); |
|
294
|
|
|
* ``` |
|
295
|
|
|
* |
|
296
|
|
|
* @param array|object $array Array or object to extract value from. |
|
297
|
37 |
|
* @param array|Closure|float|int|string $path Key name of the array element, an array of keys or property name |
|
298
|
37 |
|
* of the object, or an anonymous function returning the value. The anonymous function signature should be: |
|
299
|
37 |
|
* `function($array, $defaultValue)`. |
|
300
|
37 |
|
* @param mixed $default The default value to be returned if the specified array key does not exist. Not used when |
|
301
|
37 |
|
* getting value from an object. |
|
302
|
|
|
* @param string $delimiter A separator, used to parse string $key for embedded object property retrieving. Defaults |
|
303
|
|
|
* to "." (dot). |
|
304
|
|
|
* |
|
305
|
|
|
* @psalm-param ArrayPath|Closure $path |
|
306
|
|
|
* |
|
307
|
|
|
* @return mixed The value of the element if found, default value otherwise. |
|
308
|
|
|
*/ |
|
309
|
|
|
public static function getValueByPath( |
|
310
|
|
|
array|object $array, |
|
311
|
|
|
array|Closure|float|int|string $path, |
|
312
|
|
|
mixed $default = null, |
|
313
|
|
|
string $delimiter = '.' |
|
314
|
|
|
): mixed { |
|
315
|
|
|
return self::getValue( |
|
316
|
|
|
$array, |
|
317
|
|
|
$path instanceof Closure ? $path : self::parseMixedPath($path, $delimiter), |
|
|
|
|
|
|
318
|
|
|
$default |
|
319
|
|
|
); |
|
320
|
|
|
} |
|
321
|
|
|
|
|
322
|
|
|
/** |
|
323
|
|
|
* Writes a value into an associative array at the key path specified. |
|
324
|
|
|
* If there is no such key path yet, it will be created recursively. |
|
325
|
|
|
* If the key exists, it will be overwritten. |
|
326
|
|
|
* |
|
327
|
|
|
* ```php |
|
328
|
|
|
* $array = [ |
|
329
|
|
|
* 'key' => [ |
|
330
|
|
|
* 'in' => [ |
|
331
|
|
|
* 'val1', |
|
332
|
|
|
* 'key' => 'val' |
|
333
|
|
|
* ] |
|
334
|
|
|
* ] |
|
335
|
|
|
* ]; |
|
336
|
|
|
* ``` |
|
337
|
|
|
* |
|
338
|
|
|
* The result of `ArrayHelper::setValue($array, ['key', 'in'], ['arr' => 'val']);` |
|
339
|
|
|
* will be the following: |
|
340
|
|
|
* |
|
341
|
29 |
|
* ```php |
|
342
|
|
|
* [ |
|
343
|
29 |
|
* 'key' => [ |
|
344
|
2 |
|
* 'in' => [ |
|
345
|
2 |
|
* 'arr' => 'val' |
|
346
|
|
|
* ] |
|
347
|
|
|
* ] |
|
348
|
27 |
|
* ] |
|
349
|
|
|
* ``` |
|
350
|
27 |
|
* |
|
351
|
15 |
|
* @param array $array The array to write the value to. |
|
352
|
15 |
|
* @param array|float|int|string|null $key The path of where do you want to write a value to `$array` |
|
353
|
8 |
|
* the path can be described by an array of keys. If the path is null then `$array` will be assigned the `$value`. |
|
354
|
|
|
* |
|
355
|
15 |
|
* @psalm-param ArrayKey|null $key |
|
356
|
2 |
|
* |
|
357
|
|
|
* @param mixed $value The value to be written. |
|
358
|
15 |
|
*/ |
|
359
|
|
|
public static function setValue(array &$array, array|float|int|string|null $key, mixed $value): void |
|
360
|
|
|
{ |
|
361
|
|
|
if ($key === null) { |
|
|
|
|
|
|
362
|
27 |
|
$array = $value; |
|
363
|
|
|
return; |
|
364
|
|
|
} |
|
365
|
|
|
|
|
366
|
|
|
$keys = is_array($key) ? $key : [$key]; |
|
|
|
|
|
|
367
|
|
|
|
|
368
|
|
|
while (count($keys) > 1) { |
|
369
|
|
|
$k = self::normalizeArrayKey(array_shift($keys)); |
|
370
|
|
|
if (!isset($array[$k])) { |
|
371
|
|
|
$array[$k] = []; |
|
372
|
|
|
} |
|
373
|
|
|
if (!is_array($array[$k])) { |
|
374
|
|
|
$array[$k] = [$array[$k]]; |
|
375
|
|
|
} |
|
376
|
|
|
$array = &$array[$k]; |
|
377
|
|
|
/** @var array $array */ |
|
378
|
|
|
} |
|
379
|
|
|
|
|
380
|
|
|
$array[self::normalizeArrayKey(array_shift($keys))] = $value; |
|
381
|
|
|
} |
|
382
|
|
|
|
|
383
|
|
|
/** |
|
384
|
|
|
* Find array value in array at the key path specified and add passed value to him. |
|
385
|
|
|
* |
|
386
|
|
|
* If there is no such key path yet, it will be created recursively and an empty array will be initialized. |
|
387
|
|
|
* |
|
388
|
|
|
* ```php |
|
389
|
|
|
* $array = ['key' => []]; |
|
390
|
|
|
* |
|
391
|
|
|
* ArrayHelper::addValue($array, ['key', 'in'], 'variable1'); |
|
392
|
|
|
* ArrayHelper::addValue($array, ['key', 'in'], 'variable2'); |
|
393
|
|
|
* |
|
394
|
|
|
* // Result: ['key' => ['in' => ['variable1', 'variable2']]] |
|
395
|
|
|
* ``` |
|
396
|
|
|
* |
|
397
|
31 |
|
* If the value exists, it will become the first element of the array. |
|
398
|
|
|
* |
|
399
|
31 |
|
* ```php |
|
400
|
2 |
|
* $array = ['key' => 'in']; |
|
401
|
2 |
|
* |
|
402
|
|
|
* ArrayHelper::addValue($array, ['key'], 'variable1'); |
|
403
|
|
|
* |
|
404
|
29 |
|
* // Result: ['key' => ['in', 'variable1']] |
|
405
|
|
|
* ``` |
|
406
|
29 |
|
* |
|
407
|
29 |
|
* @param array $array The array to append the value to. |
|
408
|
|
|
* @param array|float|int|string|null $key The path of where do you want to append a value to `$array`. The path can |
|
409
|
29 |
|
* be described by an array of keys. If the path is null then `$value` will be appended to the `$array`. |
|
410
|
20 |
|
* |
|
411
|
14 |
|
* @psalm-param ArrayKey|null $key |
|
412
|
9 |
|
* |
|
413
|
|
|
* @param mixed $value The value to be appended. |
|
414
|
|
|
*/ |
|
415
|
29 |
|
public static function addValue(array &$array, array|float|int|string|null $key, mixed $value): void |
|
416
|
|
|
{ |
|
417
|
|
|
if ($key === null) { |
|
|
|
|
|
|
418
|
|
|
$array[] = $value; |
|
419
|
29 |
|
return; |
|
420
|
|
|
} |
|
421
|
|
|
|
|
422
|
|
|
$keys = is_array($key) ? $key : [$key]; |
|
|
|
|
|
|
423
|
|
|
|
|
424
|
|
|
while (count($keys) > 0) { |
|
425
|
|
|
$k = self::normalizeArrayKey(array_shift($keys)); |
|
426
|
|
|
|
|
427
|
|
|
if (!array_key_exists($k, $array)) { |
|
428
|
|
|
$array[$k] = []; |
|
429
|
|
|
} elseif (!is_array($array[$k])) { |
|
430
|
|
|
$array[$k] = [$array[$k]]; |
|
431
|
|
|
} |
|
432
|
|
|
|
|
433
|
|
|
$array = &$array[$k]; |
|
434
|
|
|
/** @var array $array */ |
|
435
|
|
|
} |
|
436
|
|
|
|
|
437
|
20 |
|
$array[] = $value; |
|
438
|
|
|
} |
|
439
|
|
|
|
|
440
|
|
|
/** |
|
441
|
|
|
* Find array value in array at the key path specified and add passed value to him. |
|
442
|
|
|
* |
|
443
|
20 |
|
* @see addValue |
|
444
|
|
|
* |
|
445
|
|
|
* @param array $array The array to append the value to. |
|
446
|
|
|
* @param array|float|int|string|null $path The path of where do you want to append a value to `$array`. The path |
|
447
|
|
|
* can be described by a string when each key should be separated by a dot. You can also describe the path as |
|
448
|
|
|
* an array of keys. If the path is null then `$value` will be appended to the `$array`. |
|
449
|
|
|
* @param mixed $value The value to be added. |
|
450
|
|
|
* @param string $delimiter A separator, used to parse string $key for embedded object property retrieving. Defaults |
|
451
|
|
|
* to "." (dot). |
|
452
|
|
|
* |
|
453
|
|
|
* @psalm-param ArrayPath|null $path |
|
454
|
|
|
*/ |
|
455
|
|
|
public static function addValueByPath( |
|
456
|
|
|
array &$array, |
|
457
|
|
|
array|float|int|string|null $path, |
|
458
|
|
|
mixed $value, |
|
459
|
|
|
string $delimiter = '.' |
|
460
|
|
|
): void { |
|
461
|
|
|
self::addValue($array, $path === null ? null : self::parseMixedPath($path, $delimiter), $value); |
|
|
|
|
|
|
462
|
|
|
} |
|
463
|
|
|
|
|
464
|
|
|
/** |
|
465
|
|
|
* Writes a value into an associative array at the key path specified. |
|
466
|
|
|
* If there is no such key path yet, it will be created recursively. |
|
467
|
|
|
* If the key exists, it will be overwritten. |
|
468
|
|
|
* |
|
469
|
|
|
* ```php |
|
470
|
|
|
* $array = [ |
|
471
|
|
|
* 'key' => [ |
|
472
|
|
|
* 'in' => [ |
|
473
|
|
|
* 'val1', |
|
474
|
|
|
* 'key' => 'val' |
|
475
|
|
|
* ] |
|
476
|
|
|
* ] |
|
477
|
|
|
* ]; |
|
478
|
|
|
* ``` |
|
479
|
|
|
* |
|
480
|
|
|
* The result of `ArrayHelper::setValue($array, 'key.in.0', ['arr' => 'val']);` will be the following: |
|
481
|
|
|
* |
|
482
|
|
|
* ```php |
|
483
|
|
|
* [ |
|
484
|
|
|
* 'key' => [ |
|
485
|
|
|
* 'in' => [ |
|
486
|
|
|
* ['arr' => 'val'], |
|
487
|
|
|
* 'key' => 'val' |
|
488
|
|
|
* ] |
|
489
|
|
|
* ] |
|
490
|
|
|
* ] |
|
491
|
|
|
* |
|
492
|
|
|
* ``` |
|
493
|
|
|
* |
|
494
|
|
|
* The result of |
|
495
|
|
|
* `ArrayHelper::setValue($array, 'key.in', ['arr' => 'val']);` or |
|
496
|
|
|
* `ArrayHelper::setValue($array, ['key', 'in'], ['arr' => 'val']);` |
|
497
|
|
|
* will be the following: |
|
498
|
|
|
* |
|
499
|
|
|
* ```php |
|
500
|
|
|
* [ |
|
501
|
|
|
* 'key' => [ |
|
502
|
21 |
|
* 'in' => [ |
|
503
|
|
|
* 'arr' => 'val' |
|
504
|
|
|
* ] |
|
505
|
|
|
* ] |
|
506
|
|
|
* ] |
|
507
|
|
|
* ``` |
|
508
|
21 |
|
* |
|
509
|
|
|
* @param array $array The array to write the value to. |
|
510
|
|
|
* @param array|float|int|string|null $path The path of where do you want to write a value to `$array`. |
|
511
|
|
|
* The path can be described by a string when each key should be separated by a dot. |
|
512
|
|
|
* You can also describe the path as an array of keys. If the path is null then `$array` will be assigned |
|
513
|
|
|
* the `$value`. |
|
514
|
|
|
* @param mixed $value The value to be written. |
|
515
|
|
|
* @param string $delimiter A separator, used to parse string $key for embedded object property retrieving. Defaults |
|
516
|
|
|
* to "." (dot). |
|
517
|
|
|
* |
|
518
|
|
|
* @psalm-param ArrayPath|null $path |
|
519
|
|
|
*/ |
|
520
|
|
|
public static function setValueByPath( |
|
521
|
|
|
array &$array, |
|
522
|
|
|
array|float|int|string|null $path, |
|
523
|
|
|
mixed $value, |
|
524
|
|
|
string $delimiter = '.' |
|
525
|
|
|
): void { |
|
526
|
|
|
self::setValue($array, $path === null ? null : self::parseMixedPath($path, $delimiter), $value); |
|
|
|
|
|
|
527
|
|
|
} |
|
528
|
|
|
|
|
529
|
|
|
/** |
|
530
|
|
|
* Removes an item from an array and returns the value. If the key does not exist in the array, the default value |
|
531
|
|
|
* will be returned instead. |
|
532
|
|
|
* |
|
533
|
|
|
* Usage examples, |
|
534
|
|
|
* |
|
535
|
13 |
|
* ```php |
|
536
|
|
|
* // $array = ['type' => 'A', 'options' => [1, 2]]; |
|
537
|
13 |
|
* |
|
538
|
|
|
* // Working with array: |
|
539
|
13 |
|
* $type = \Yiisoft\Arrays\ArrayHelper::remove($array, 'type'); |
|
540
|
7 |
|
* |
|
541
|
7 |
|
* // $array content |
|
542
|
1 |
|
* // $array = ['options' => [1, 2]]; |
|
543
|
|
|
* ``` |
|
544
|
6 |
|
* |
|
545
|
|
|
* @param array $array The array to extract value from. |
|
546
|
|
|
* @param array|float|int|string $key Key name of the array element or associative array at the key path specified. |
|
547
|
12 |
|
* @param mixed $default The default value to be returned if the specified key does not exist. |
|
548
|
12 |
|
* |
|
549
|
11 |
|
* @psalm-param ArrayKey $key |
|
550
|
11 |
|
* |
|
551
|
11 |
|
* @return mixed The value of the element if found, default value otherwise. |
|
552
|
|
|
*/ |
|
553
|
|
|
public static function remove(array &$array, array|float|int|string $key, mixed $default = null): mixed |
|
554
|
1 |
|
{ |
|
555
|
|
|
$keys = is_array($key) ? $key : [$key]; |
|
|
|
|
|
|
556
|
|
|
|
|
557
|
|
|
while (count($keys) > 1) { |
|
558
|
|
|
$key = self::normalizeArrayKey(array_shift($keys)); |
|
559
|
|
|
if (!isset($array[$key]) || !is_array($array[$key])) { |
|
560
|
|
|
return $default; |
|
561
|
|
|
} |
|
562
|
|
|
$array = &$array[$key]; |
|
563
|
|
|
} |
|
564
|
|
|
|
|
565
|
|
|
$key = self::normalizeArrayKey(array_shift($keys)); |
|
566
|
|
|
if (array_key_exists($key, $array)) { |
|
567
|
|
|
$value = $array[$key]; |
|
568
|
|
|
unset($array[$key]); |
|
569
|
|
|
return $value; |
|
570
|
|
|
} |
|
571
|
|
|
|
|
572
|
|
|
return $default; |
|
573
|
|
|
} |
|
574
|
|
|
|
|
575
|
|
|
/** |
|
576
|
|
|
* Removes an item from an array and returns the value. If the key does not exist in the array, the default value |
|
577
|
|
|
* will be returned instead. |
|
578
|
|
|
* |
|
579
|
|
|
* Usage examples, |
|
580
|
|
|
* |
|
581
|
|
|
* ```php |
|
582
|
|
|
* // $array = ['type' => 'A', 'options' => [1, 2]]; |
|
583
|
|
|
* |
|
584
|
5 |
|
* // Working with array: |
|
585
|
|
|
* $type = \Yiisoft\Arrays\ArrayHelper::remove($array, 'type'); |
|
586
|
|
|
* |
|
587
|
|
|
* // $array content |
|
588
|
|
|
* // $array = ['options' => [1, 2]]; |
|
589
|
|
|
* ``` |
|
590
|
5 |
|
* |
|
591
|
|
|
* @param array $array The array to extract value from. |
|
592
|
|
|
* @param array|float|int|string $path Key name of the array element or associative array at the key path specified. |
|
593
|
|
|
* The path can be described by a string when each key should be separated by a delimiter (default is dot). |
|
594
|
|
|
* @param mixed $default The default value to be returned if the specified key does not exist. |
|
595
|
|
|
* @param string $delimiter A separator, used to parse string $key for embedded object property retrieving. Defaults |
|
596
|
|
|
* to "." (dot). |
|
597
|
|
|
* |
|
598
|
|
|
* @psalm-param ArrayPath $path |
|
599
|
|
|
* |
|
600
|
|
|
* @return mixed The value of the element if found, default value otherwise. |
|
601
|
|
|
*/ |
|
602
|
|
|
public static function removeByPath( |
|
603
|
|
|
array &$array, |
|
604
|
|
|
array|float|int|string $path, |
|
605
|
|
|
mixed $default = null, |
|
606
|
|
|
string $delimiter = '.' |
|
607
|
|
|
): mixed { |
|
608
|
|
|
return self::remove($array, self::parseMixedPath($path, $delimiter), $default); |
|
609
|
|
|
} |
|
610
|
|
|
|
|
611
|
2 |
|
/** |
|
612
|
|
|
* Removes items with matching values from the array and returns the removed items. |
|
613
|
2 |
|
* |
|
614
|
2 |
|
* Example, |
|
615
|
2 |
|
* |
|
616
|
1 |
|
* ```php |
|
617
|
1 |
|
* $array = ['Bob' => 'Dylan', 'Michael' => 'Jackson', 'Mick' => 'Jagger', 'Janet' => 'Jackson']; |
|
618
|
|
|
* $removed = \Yiisoft\Arrays\ArrayHelper::removeValue($array, 'Jackson'); |
|
619
|
|
|
* // result: |
|
620
|
|
|
* // $array = ['Bob' => 'Dylan', 'Mick' => 'Jagger']; |
|
621
|
2 |
|
* // $removed = ['Michael' => 'Jackson', 'Janet' => 'Jackson']; |
|
622
|
|
|
* ``` |
|
623
|
|
|
* |
|
624
|
|
|
* @param array $array The array where to look the value from. |
|
625
|
|
|
* @param mixed $value The value to remove from the array. |
|
626
|
|
|
* |
|
627
|
|
|
* @return array The items that were removed from the array. |
|
628
|
|
|
*/ |
|
629
|
|
|
public static function removeValue(array &$array, mixed $value): array |
|
630
|
|
|
{ |
|
631
|
|
|
$result = []; |
|
632
|
|
|
foreach ($array as $key => $val) { |
|
633
|
|
|
if ($val === $value) { |
|
634
|
|
|
$result[$key] = $val; |
|
635
|
|
|
unset($array[$key]); |
|
636
|
|
|
} |
|
637
|
|
|
} |
|
638
|
|
|
|
|
639
|
|
|
return $result; |
|
640
|
|
|
} |
|
641
|
|
|
|
|
642
|
|
|
/** |
|
643
|
|
|
* Indexes and/or groups the array according to a specified key. |
|
644
|
|
|
* The input should be either multidimensional array or an array of objects. |
|
645
|
|
|
* |
|
646
|
|
|
* The `$key` can be either a key name of the sub-array, a property name of object, or an anonymous |
|
647
|
|
|
* function that must return the value that will be used as a key. |
|
648
|
|
|
* |
|
649
|
|
|
* `$groups` is an array of keys, that will be used to group the input array into one or more sub-arrays based |
|
650
|
|
|
* on keys specified. |
|
651
|
|
|
* |
|
652
|
|
|
* If the `$key` is specified as `null` or a value of an element corresponding to the key is `null` in addition |
|
653
|
|
|
* to `$groups` not specified then the element is discarded. |
|
654
|
|
|
* |
|
655
|
|
|
* For example: |
|
656
|
|
|
* |
|
657
|
|
|
* ```php |
|
658
|
|
|
* $array = [ |
|
659
|
|
|
* ['id' => '123', 'data' => 'abc', 'device' => 'laptop'], |
|
660
|
|
|
* ['id' => '345', 'data' => 'def', 'device' => 'tablet'], |
|
661
|
|
|
* ['id' => '345', 'data' => 'hgi', 'device' => 'smartphone'], |
|
662
|
|
|
* ]; |
|
663
|
|
|
* $result = ArrayHelper::index($array, 'id'); |
|
664
|
|
|
* ``` |
|
665
|
|
|
* |
|
666
|
|
|
* The result will be an associative array, where the key is the value of `id` attribute |
|
667
|
|
|
* |
|
668
|
|
|
* ```php |
|
669
|
|
|
* [ |
|
670
|
|
|
* '123' => ['id' => '123', 'data' => 'abc', 'device' => 'laptop'], |
|
671
|
|
|
* '345' => ['id' => '345', 'data' => 'hgi', 'device' => 'smartphone'] |
|
672
|
|
|
* // The second element of an original array is overwritten by the last element because of the same id |
|
673
|
|
|
* ] |
|
674
|
|
|
* ``` |
|
675
|
|
|
* |
|
676
|
|
|
* An anonymous function can be used in the grouping array as well. |
|
677
|
|
|
* |
|
678
|
|
|
* ```php |
|
679
|
|
|
* $result = ArrayHelper::index($array, function ($element) { |
|
680
|
|
|
* return $element['id']; |
|
681
|
|
|
* }); |
|
682
|
|
|
* ``` |
|
683
|
|
|
* |
|
684
|
|
|
* Passing `id` as a third argument will group `$array` by `id`: |
|
685
|
|
|
* |
|
686
|
|
|
* ```php |
|
687
|
|
|
* $result = ArrayHelper::index($array, null, 'id'); |
|
688
|
|
|
* ``` |
|
689
|
|
|
* |
|
690
|
|
|
* The result will be a multidimensional array grouped by `id` on the first level, by `device` on the second level |
|
691
|
|
|
* and indexed by `data` on the third level: |
|
692
|
|
|
* |
|
693
|
|
|
* ```php |
|
694
|
|
|
* [ |
|
695
|
|
|
* '123' => [ |
|
696
|
|
|
* ['id' => '123', 'data' => 'abc', 'device' => 'laptop'] |
|
697
|
|
|
* ], |
|
698
|
|
|
* '345' => [ // all elements with this index are present in the result array |
|
699
|
|
|
* ['id' => '345', 'data' => 'def', 'device' => 'tablet'], |
|
700
|
|
|
* ['id' => '345', 'data' => 'hgi', 'device' => 'smartphone'], |
|
701
|
|
|
* ] |
|
702
|
|
|
* ] |
|
703
|
|
|
* ``` |
|
704
|
|
|
* |
|
705
|
|
|
* The anonymous function can be used in the array of grouping keys as well: |
|
706
|
|
|
* |
|
707
|
|
|
* ```php |
|
708
|
|
|
* $result = ArrayHelper::index($array, 'data', [function ($element) { |
|
709
|
|
|
* return $element['id']; |
|
710
|
|
|
* }, 'device']); |
|
711
|
|
|
* ``` |
|
712
|
|
|
* |
|
713
|
|
|
* The result will be a multidimensional array grouped by `id` on the first level, by the `device` on the second one |
|
714
|
|
|
* and indexed by the `data` on the third level: |
|
715
|
|
|
* |
|
716
|
|
|
* ```php |
|
717
|
|
|
* [ |
|
718
|
|
|
* '123' => [ |
|
719
|
|
|
* 'laptop' => [ |
|
720
|
|
|
* 'abc' => ['id' => '123', 'data' => 'abc', 'device' => 'laptop'] |
|
721
|
|
|
* ] |
|
722
|
|
|
* ], |
|
723
|
|
|
* '345' => [ |
|
724
|
|
|
* 'tablet' => [ |
|
725
|
|
|
* 'def' => ['id' => '345', 'data' => 'def', 'device' => 'tablet'] |
|
726
|
|
|
* ], |
|
727
|
|
|
* 'smartphone' => [ |
|
728
|
24 |
|
* 'hgi' => ['id' => '345', 'data' => 'hgi', 'device' => 'smartphone'] |
|
729
|
|
|
* ] |
|
730
|
|
|
* ] |
|
731
|
|
|
* ] |
|
732
|
|
|
* ``` |
|
733
|
24 |
|
* |
|
734
|
24 |
|
* @param iterable $array The array or iterable object that needs to be indexed or grouped. |
|
735
|
|
|
* @param Closure|string|null $key The column name or anonymous function which result will be used |
|
736
|
|
|
* to index the array. |
|
737
|
24 |
|
* @param Closure[]|string|string[]|null $groups The array of keys, that will be used to group the input |
|
738
|
24 |
|
* array by one or more keys. If the `$key` attribute or its value for the particular element is null and `$groups` |
|
739
|
8 |
|
* is not defined, the array element will be discarded. Otherwise, if `$groups` is specified, array element will be |
|
740
|
8 |
|
* added to the result array without any key. |
|
741
|
8 |
|
* |
|
742
|
8 |
|
* @psalm-param iterable<mixed, array|object> $array |
|
743
|
|
|
* |
|
744
|
|
|
* @return array The indexed and/or grouped array. |
|
745
|
20 |
|
*/ |
|
746
|
|
|
public static function index( |
|
747
|
20 |
|
iterable $array, |
|
748
|
9 |
|
Closure|string|null $key, |
|
749
|
9 |
|
array|string|null $groups = [] |
|
750
|
9 |
|
): array { |
|
751
|
9 |
|
$result = []; |
|
752
|
9 |
|
$groups = (array)$groups; |
|
753
|
|
|
|
|
754
|
|
|
/** @var mixed $element */ |
|
755
|
9 |
|
foreach ($array as $element) { |
|
756
|
|
|
if (!is_array($element) && !is_object($element)) { |
|
757
|
|
|
throw new InvalidArgumentException( |
|
758
|
|
|
'index() can not get value from ' . gettype($element) . |
|
759
|
20 |
|
'. The $array should be either multidimensional array or an array of objects.' |
|
760
|
7 |
|
); |
|
761
|
7 |
|
} |
|
762
|
|
|
|
|
763
|
|
|
$lastArray = &$result; |
|
764
|
13 |
|
|
|
765
|
13 |
|
foreach ($groups as $group) { |
|
766
|
12 |
|
$value = self::normalizeArrayKey( |
|
767
|
|
|
self::getValue($element, $group) |
|
768
|
|
|
); |
|
769
|
20 |
|
if (!array_key_exists($value, $lastArray)) { |
|
770
|
|
|
$lastArray[$value] = []; |
|
771
|
|
|
} |
|
772
|
16 |
|
/** @psalm-suppress MixedAssignment */ |
|
773
|
|
|
$lastArray = &$lastArray[$value]; |
|
774
|
|
|
/** @var array $lastArray */ |
|
775
|
|
|
} |
|
776
|
|
|
|
|
777
|
|
|
if ($key === null) { |
|
778
|
|
|
if (!empty($groups)) { |
|
779
|
|
|
$lastArray[] = $element; |
|
780
|
|
|
} |
|
781
|
|
|
} else { |
|
782
|
|
|
$value = self::getValue($element, $key); |
|
783
|
|
|
if ($value !== null) { |
|
784
|
|
|
$lastArray[self::normalizeArrayKey($value)] = $element; |
|
785
|
|
|
} |
|
786
|
|
|
} |
|
787
|
1 |
|
unset($lastArray); |
|
788
|
|
|
} |
|
789
|
1 |
|
|
|
790
|
|
|
return $result; |
|
791
|
|
|
} |
|
792
|
|
|
|
|
793
|
|
|
/** |
|
794
|
|
|
* Groups the array according to a specified key. |
|
795
|
|
|
* This is just an alias for indexing by groups |
|
796
|
|
|
* |
|
797
|
|
|
* @param iterable $array The array or iterable object that needs to be grouped. |
|
798
|
|
|
* @param Closure[]|string|string[] $groups The array of keys, that will be used to group the input array |
|
799
|
|
|
* by one or more keys. |
|
800
|
|
|
* |
|
801
|
|
|
* @psalm-param iterable<mixed, array|object> $array |
|
802
|
|
|
* |
|
803
|
|
|
* @return array The grouped array. |
|
804
|
|
|
*/ |
|
805
|
|
|
public static function group(iterable $array, array|string $groups): array |
|
806
|
|
|
{ |
|
807
|
|
|
return self::index($array, null, $groups); |
|
808
|
|
|
} |
|
809
|
|
|
|
|
810
|
|
|
/** |
|
811
|
|
|
* Returns the values of a specified column in an array. |
|
812
|
|
|
* The input array should be multidimensional or an array of objects. |
|
813
|
|
|
* |
|
814
|
|
|
* For example, |
|
815
|
|
|
* |
|
816
|
|
|
* ```php |
|
817
|
|
|
* $array = [ |
|
818
|
|
|
* ['id' => '123', 'data' => 'abc'], |
|
819
|
|
|
* ['id' => '345', 'data' => 'def'], |
|
820
|
|
|
* ]; |
|
821
|
8 |
|
* $result = ArrayHelper::getColumn($array, 'id'); |
|
822
|
|
|
* // the result is: ['123', '345'] |
|
823
|
8 |
|
* |
|
824
|
8 |
|
* // using anonymous function |
|
825
|
6 |
|
* $result = ArrayHelper::getColumn($array, function ($element) { |
|
826
|
6 |
|
* return $element['id']; |
|
827
|
|
|
* }); |
|
828
|
|
|
* ``` |
|
829
|
2 |
|
* |
|
830
|
2 |
|
* @param iterable $array The array or iterable object to get column from. |
|
831
|
|
|
* @param Closure|string $name Column name or a closure returning column name. |
|
832
|
|
|
* @param bool $keepKeys Whether to maintain the array keys. If false, the resulting array |
|
833
|
|
|
* will be re-indexed with integers. |
|
834
|
8 |
|
* |
|
835
|
|
|
* @psalm-param iterable<array-key, array|object> $array |
|
836
|
|
|
* |
|
837
|
|
|
* @return array The list of column values. |
|
838
|
|
|
*/ |
|
839
|
|
|
public static function getColumn(iterable $array, Closure|string $name, bool $keepKeys = true): array |
|
840
|
|
|
{ |
|
841
|
|
|
$result = []; |
|
842
|
|
|
if ($keepKeys) { |
|
843
|
|
|
foreach ($array as $k => $element) { |
|
844
|
|
|
$result[$k] = self::getValue($element, $name); |
|
845
|
|
|
} |
|
846
|
|
|
} else { |
|
847
|
|
|
foreach ($array as $element) { |
|
848
|
|
|
$result[] = self::getValue($element, $name); |
|
849
|
|
|
} |
|
850
|
|
|
} |
|
851
|
|
|
|
|
852
|
|
|
return $result; |
|
853
|
|
|
} |
|
854
|
|
|
|
|
855
|
|
|
/** |
|
856
|
|
|
* Builds a map (key-value pairs) from a multidimensional array or an array of objects. |
|
857
|
|
|
* The `$from` and `$to` parameters specify the key names or property names to set up the map. |
|
858
|
|
|
* Optionally, one can further group the map according to a grouping field `$group`. |
|
859
|
|
|
* |
|
860
|
|
|
* For example, |
|
861
|
|
|
* |
|
862
|
|
|
* ```php |
|
863
|
|
|
* $array = [ |
|
864
|
|
|
* ['id' => '123', 'name' => 'aaa', 'class' => 'x'], |
|
865
|
|
|
* ['id' => '124', 'name' => 'bbb', 'class' => 'x'], |
|
866
|
|
|
* ['id' => '345', 'name' => 'ccc', 'class' => 'y'], |
|
867
|
|
|
* ]; |
|
868
|
|
|
* |
|
869
|
|
|
* $result = ArrayHelper::map($array, 'id', 'name'); |
|
870
|
|
|
* // the result is: |
|
871
|
|
|
* // [ |
|
872
|
|
|
* // '123' => 'aaa', |
|
873
|
|
|
* // '124' => 'bbb', |
|
874
|
|
|
* // '345' => 'ccc', |
|
875
|
|
|
* // ] |
|
876
|
|
|
* |
|
877
|
|
|
* $result = ArrayHelper::map($array, 'id', 'name', 'class'); |
|
878
|
|
|
* // the result is: |
|
879
|
|
|
* // [ |
|
880
|
|
|
* // 'x' => [ |
|
881
|
9 |
|
* // '123' => 'aaa', |
|
882
|
|
|
* // '124' => 'bbb', |
|
883
|
|
|
* // ], |
|
884
|
|
|
* // 'y' => [ |
|
885
|
|
|
* // '345' => 'ccc', |
|
886
|
|
|
* // ], |
|
887
|
9 |
|
* // ] |
|
888
|
4 |
|
* ``` |
|
889
|
4 |
|
* |
|
890
|
4 |
|
* @param iterable $array Array or iterable object to build map from. |
|
891
|
4 |
|
* @param Closure|string $from Key or property name to map from. |
|
892
|
4 |
|
* @param Closure|string $to Key or property name to map to. |
|
893
|
|
|
* @param Closure|string|null $group Key or property to group the map. |
|
894
|
|
|
* |
|
895
|
4 |
|
* @psalm-param iterable<mixed, array|object> $array |
|
896
|
|
|
* |
|
897
|
|
|
* @return array Resulting map. |
|
898
|
2 |
|
*/ |
|
899
|
|
|
public static function map( |
|
900
|
|
|
iterable $array, |
|
901
|
5 |
|
Closure|string $from, |
|
902
|
5 |
|
Closure|string $to, |
|
903
|
5 |
|
Closure|string|null $group = null |
|
904
|
5 |
|
): array { |
|
905
|
5 |
|
if ($group === null) { |
|
906
|
|
|
if ($from instanceof Closure || $to instanceof Closure || !is_array($array)) { |
|
907
|
|
|
$result = []; |
|
908
|
5 |
|
foreach ($array as $element) { |
|
909
|
|
|
$key = (string)self::getValue($element, $from); |
|
910
|
|
|
$result[$key] = self::getValue($element, $to); |
|
911
|
|
|
} |
|
912
|
|
|
|
|
913
|
|
|
return $result; |
|
914
|
|
|
} |
|
915
|
|
|
|
|
916
|
|
|
return array_column($array, $to, $from); |
|
917
|
|
|
} |
|
918
|
|
|
|
|
919
|
|
|
$result = []; |
|
920
|
|
|
foreach ($array as $element) { |
|
921
|
|
|
$groupKey = (string)self::getValue($element, $group); |
|
922
|
|
|
$key = (string)self::getValue($element, $from); |
|
923
|
|
|
$result[$groupKey][$key] = self::getValue($element, $to); |
|
924
|
41 |
|
} |
|
925
|
|
|
|
|
926
|
41 |
|
return $result; |
|
927
|
31 |
|
} |
|
928
|
25 |
|
|
|
929
|
|
|
/** |
|
930
|
|
|
* Checks if the given array contains the specified key. |
|
931
|
27 |
|
* This method enhances the `array_key_exists()` function by supporting case-insensitive |
|
932
|
27 |
|
* key comparison. |
|
933
|
27 |
|
* |
|
934
|
14 |
|
* @param array $array The array with keys to check. |
|
935
|
|
|
* @param array|float|int|string $key The key to check. |
|
936
|
|
|
* @param bool $caseSensitive Whether the key comparison should be case-sensitive. |
|
937
|
|
|
* |
|
938
|
13 |
|
* @psalm-param ArrayKey $key |
|
939
|
|
|
* |
|
940
|
|
|
* @return bool Whether the array contains the specified key. |
|
941
|
10 |
|
*/ |
|
942
|
|
|
public static function keyExists(array $array, array|float|int|string $key, bool $caseSensitive = true): bool |
|
943
|
|
|
{ |
|
944
|
35 |
|
if (is_array($key)) { |
|
|
|
|
|
|
945
|
|
|
if (count($key) === 1) { |
|
946
|
35 |
|
return self::rootKeyExists($array, end($key), $caseSensitive); |
|
947
|
|
|
} |
|
948
|
35 |
|
|
|
949
|
29 |
|
foreach (self::getExistsKeys($array, array_shift($key), $caseSensitive) as $existKey) { |
|
950
|
|
|
$array = self::getRootValue($array, $existKey, null); |
|
951
|
|
|
if (is_array($array) && self::keyExists($array, $key, $caseSensitive)) { |
|
952
|
6 |
|
return true; |
|
953
|
6 |
|
} |
|
954
|
5 |
|
} |
|
955
|
|
|
|
|
956
|
|
|
return false; |
|
957
|
|
|
} |
|
958
|
1 |
|
|
|
959
|
|
|
return self::rootKeyExists($array, $key, $caseSensitive); |
|
960
|
|
|
} |
|
961
|
|
|
|
|
962
|
|
|
private static function rootKeyExists(array $array, float|int|string $key, bool $caseSensitive): bool |
|
963
|
|
|
{ |
|
964
|
27 |
|
$key = (string)$key; |
|
965
|
|
|
|
|
966
|
27 |
|
if ($caseSensitive) { |
|
967
|
|
|
return array_key_exists($key, $array); |
|
968
|
27 |
|
} |
|
969
|
22 |
|
|
|
970
|
|
|
foreach (array_keys($array) as $k) { |
|
971
|
|
|
if (strcasecmp($key, (string)$k) === 0) { |
|
972
|
5 |
|
return true; |
|
973
|
5 |
|
} |
|
974
|
5 |
|
} |
|
975
|
5 |
|
|
|
976
|
|
|
return false; |
|
977
|
|
|
} |
|
978
|
|
|
|
|
979
|
|
|
/** |
|
980
|
|
|
* @return array<int, array-key> |
|
|
|
|
|
|
981
|
|
|
*/ |
|
982
|
|
|
private static function getExistsKeys(array $array, float|int|string $key, bool $caseSensitive): array |
|
983
|
|
|
{ |
|
984
|
|
|
$key = (string)$key; |
|
985
|
|
|
|
|
986
|
|
|
if ($caseSensitive) { |
|
987
|
|
|
return [$key]; |
|
988
|
|
|
} |
|
989
|
|
|
|
|
990
|
|
|
return array_filter( |
|
991
|
|
|
array_keys($array), |
|
992
|
|
|
static fn ($k) => strcasecmp($key, (string)$k) === 0 |
|
993
|
|
|
); |
|
994
|
26 |
|
} |
|
995
|
|
|
|
|
996
|
|
|
/** |
|
997
|
|
|
* Checks if the given array contains the specified key. The key may be specified in a dot format. |
|
998
|
|
|
* In particular, if the key is `x.y.z`, then key would be `$array['x']['y']['z']`. |
|
999
|
|
|
* |
|
1000
|
26 |
|
* This method enhances the `array_key_exists()` function by supporting case-insensitive |
|
1001
|
|
|
* key comparison. |
|
1002
|
|
|
* |
|
1003
|
|
|
* @param array $array The array to check path in. |
|
1004
|
|
|
* @param array|float|int|string $path The key path. Can be described by a string when each key should be separated |
|
1005
|
|
|
* by delimiter. You can also describe the path as an array of keys. |
|
1006
|
|
|
* @param bool $caseSensitive Whether the key comparison should be case-sensitive. |
|
1007
|
|
|
* @param string $delimiter A separator, used to parse string $key for embedded object property retrieving. Defaults |
|
1008
|
|
|
* to "." (dot). |
|
1009
|
|
|
* |
|
1010
|
|
|
* @psalm-param ArrayPath $path |
|
1011
|
|
|
*/ |
|
1012
|
|
|
public static function pathExists( |
|
1013
|
|
|
array $array, |
|
1014
|
|
|
array|float|int|string $path, |
|
1015
|
|
|
bool $caseSensitive = true, |
|
1016
|
|
|
string $delimiter = '.' |
|
1017
|
|
|
): bool { |
|
1018
|
|
|
return self::keyExists($array, self::parseMixedPath($path, $delimiter), $caseSensitive); |
|
1019
|
|
|
} |
|
1020
|
2 |
|
|
|
1021
|
|
|
/** |
|
1022
|
2 |
|
* Encodes special characters in an array of strings into HTML entities. |
|
1023
|
2 |
|
* Only array values will be encoded by default. |
|
1024
|
2 |
|
* If a value is an array, this method will also encode it recursively. |
|
1025
|
2 |
|
* Only string values will be encoded. |
|
1026
|
|
|
* |
|
1027
|
2 |
|
* @param iterable $data Data to be encoded. |
|
1028
|
1 |
|
* @param bool $valuesOnly Whether to encode array values only. If false, |
|
1029
|
|
|
* both the array keys and array values will be encoded. |
|
1030
|
2 |
|
* @param string|null $encoding The encoding to use, defaults to `ini_get('default_charset')`. |
|
1031
|
2 |
|
* |
|
1032
|
2 |
|
* @psalm-param iterable<mixed, mixed> $data |
|
1033
|
2 |
|
* |
|
1034
|
|
|
* @return array The encoded data. |
|
1035
|
2 |
|
* |
|
1036
|
|
|
* @link https://www.php.net/manual/en/function.htmlspecialchars.php |
|
1037
|
|
|
*/ |
|
1038
|
|
|
public static function htmlEncode(iterable $data, bool $valuesOnly = true, string $encoding = null): array |
|
1039
|
2 |
|
{ |
|
1040
|
|
|
$d = []; |
|
1041
|
|
|
foreach ($data as $key => $value) { |
|
1042
|
|
|
if (!is_int($key)) { |
|
1043
|
|
|
$key = (string)$key; |
|
1044
|
|
|
} |
|
1045
|
|
|
if (!$valuesOnly && is_string($key)) { |
|
1046
|
|
|
$key = htmlspecialchars($key, ENT_QUOTES | ENT_SUBSTITUTE, $encoding, true); |
|
1047
|
|
|
} |
|
1048
|
|
|
if (is_string($value)) { |
|
1049
|
|
|
$d[$key] = htmlspecialchars($value, ENT_QUOTES | ENT_SUBSTITUTE, $encoding, true); |
|
1050
|
|
|
} elseif (is_array($value)) { |
|
1051
|
|
|
$d[$key] = self::htmlEncode($value, $valuesOnly, $encoding); |
|
1052
|
|
|
} else { |
|
1053
|
|
|
$d[$key] = $value; |
|
1054
|
|
|
} |
|
1055
|
|
|
} |
|
1056
|
|
|
|
|
1057
|
|
|
return $d; |
|
1058
|
2 |
|
} |
|
1059
|
|
|
|
|
1060
|
2 |
|
/** |
|
1061
|
2 |
|
* Decodes HTML entities into the corresponding characters in an array of strings. |
|
1062
|
2 |
|
* Only array values will be decoded by default. |
|
1063
|
2 |
|
* If a value is an array, this method will also decode it recursively. |
|
1064
|
|
|
* Only string values will be decoded. |
|
1065
|
2 |
|
* |
|
1066
|
1 |
|
* @param iterable $data Data to be decoded. |
|
1067
|
|
|
* @param bool $valuesOnly Whether to decode array values only. If false, |
|
1068
|
2 |
|
* both the array keys and array values will be decoded. |
|
1069
|
2 |
|
* |
|
1070
|
2 |
|
* @psalm-param iterable<mixed, mixed> $data |
|
1071
|
2 |
|
* |
|
1072
|
|
|
* @return array The decoded data. |
|
1073
|
2 |
|
* |
|
1074
|
|
|
* @link https://www.php.net/manual/en/function.htmlspecialchars-decode.php |
|
1075
|
|
|
*/ |
|
1076
|
|
|
public static function htmlDecode(iterable $data, bool $valuesOnly = true): array |
|
1077
|
2 |
|
{ |
|
1078
|
|
|
$decoded = []; |
|
1079
|
|
|
foreach ($data as $key => $value) { |
|
1080
|
|
|
if (!is_int($key)) { |
|
1081
|
|
|
$key = (string)$key; |
|
1082
|
|
|
} |
|
1083
|
|
|
if (!$valuesOnly && is_string($key)) { |
|
1084
|
|
|
$key = htmlspecialchars_decode($key, ENT_QUOTES); |
|
1085
|
|
|
} |
|
1086
|
|
|
if (is_string($value)) { |
|
1087
|
|
|
$decoded[$key] = htmlspecialchars_decode($value, ENT_QUOTES); |
|
1088
|
|
|
} elseif (is_array($value)) { |
|
1089
|
|
|
$decoded[$key] = self::htmlDecode($value); |
|
1090
|
|
|
} else { |
|
1091
|
|
|
$decoded[$key] = $value; |
|
1092
|
|
|
} |
|
1093
|
|
|
} |
|
1094
|
1 |
|
|
|
1095
|
|
|
return $decoded; |
|
1096
|
1 |
|
} |
|
1097
|
1 |
|
|
|
1098
|
|
|
/** |
|
1099
|
|
|
* Returns a value indicating whether the given array is an associative array. |
|
1100
|
1 |
|
* |
|
1101
|
1 |
|
* An array is associative if all its keys are strings. If `$allStrings` is false, |
|
1102
|
1 |
|
* then an array will be treated as associative if at least one of its keys is a string. |
|
1103
|
1 |
|
* |
|
1104
|
|
|
* Note that an empty array will NOT be considered associative. |
|
1105
|
|
|
* |
|
1106
|
|
|
* @param array $array The array being checked. |
|
1107
|
1 |
|
* @param bool $allStrings Whether the array keys must be all strings in order for |
|
1108
|
|
|
* the array to be treated as associative. |
|
1109
|
|
|
* |
|
1110
|
1 |
|
* @return bool Whether the array is associative. |
|
1111
|
1 |
|
*/ |
|
1112
|
1 |
|
public static function isAssociative(array $array, bool $allStrings = true): bool |
|
1113
|
|
|
{ |
|
1114
|
|
|
if ($array === []) { |
|
1115
|
|
|
return false; |
|
1116
|
1 |
|
} |
|
1117
|
|
|
|
|
1118
|
|
|
if ($allStrings) { |
|
1119
|
|
|
foreach ($array as $key => $_value) { |
|
1120
|
|
|
if (!is_string($key)) { |
|
1121
|
|
|
return false; |
|
1122
|
|
|
} |
|
1123
|
|
|
} |
|
1124
|
|
|
|
|
1125
|
|
|
return true; |
|
1126
|
|
|
} |
|
1127
|
|
|
|
|
1128
|
|
|
foreach ($array as $key => $_value) { |
|
1129
|
|
|
if (is_string($key)) { |
|
1130
|
|
|
return true; |
|
1131
|
|
|
} |
|
1132
|
|
|
} |
|
1133
|
1 |
|
|
|
1134
|
|
|
return false; |
|
1135
|
1 |
|
} |
|
1136
|
1 |
|
|
|
1137
|
|
|
/** |
|
1138
|
|
|
* Returns a value indicating whether the given array is an indexed array. |
|
1139
|
1 |
|
* |
|
1140
|
1 |
|
* An array is indexed if all its keys are integers. If `$consecutive` is true, |
|
1141
|
|
|
* then the array keys must be a consecutive sequence starting from 0. |
|
1142
|
|
|
* |
|
1143
|
1 |
|
* Note that an empty array will be considered indexed. |
|
1144
|
1 |
|
* |
|
1145
|
1 |
|
* @param array $array The array being checked. |
|
1146
|
|
|
* @param bool $consecutive Whether the array keys must be a consecutive sequence |
|
1147
|
|
|
* in order for the array to be treated as indexed. |
|
1148
|
|
|
* |
|
1149
|
1 |
|
* @return bool Whether the array is indexed. |
|
1150
|
|
|
*/ |
|
1151
|
|
|
public static function isIndexed(array $array, bool $consecutive = false): bool |
|
1152
|
|
|
{ |
|
1153
|
|
|
if ($array === []) { |
|
1154
|
|
|
return true; |
|
1155
|
|
|
} |
|
1156
|
|
|
|
|
1157
|
|
|
if ($consecutive) { |
|
1158
|
|
|
return array_keys($array) === range(0, count($array) - 1); |
|
1159
|
|
|
} |
|
1160
|
|
|
|
|
1161
|
|
|
foreach ($array as $key => $_value) { |
|
1162
|
|
|
if (!is_int($key)) { |
|
1163
|
|
|
return false; |
|
1164
|
|
|
} |
|
1165
|
|
|
} |
|
1166
|
|
|
|
|
1167
|
|
|
return true; |
|
1168
|
3 |
|
} |
|
1169
|
|
|
|
|
1170
|
3 |
|
/** |
|
1171
|
3 |
|
* Check whether an array or `\Traversable` contains an element. |
|
1172
|
|
|
* |
|
1173
|
|
|
* This method does the same as the PHP function {@see in_array()} |
|
1174
|
3 |
|
* but additionally works for objects that implement the {@see \Traversable} interface. |
|
1175
|
3 |
|
* |
|
1176
|
3 |
|
* @param mixed $needle The value to look for. |
|
1177
|
|
|
* @param iterable $haystack The set of values to search. |
|
1178
|
|
|
* @param bool $strict Whether to enable strict (`===`) comparison. |
|
1179
|
|
|
* |
|
1180
|
3 |
|
* @throws InvalidArgumentException if `$haystack` is neither traversable nor an array. |
|
1181
|
|
|
* |
|
1182
|
|
|
* @return bool `true` if `$needle` was found in `$haystack`, `false` otherwise. |
|
1183
|
|
|
* |
|
1184
|
|
|
* @link https://php.net/manual/en/function.in-array.php |
|
1185
|
|
|
*/ |
|
1186
|
|
|
public static function isIn(mixed $needle, iterable $haystack, bool $strict = false): bool |
|
1187
|
|
|
{ |
|
1188
|
|
|
if (is_array($haystack)) { |
|
1189
|
|
|
return in_array($needle, $haystack, $strict); |
|
1190
|
|
|
} |
|
1191
|
|
|
|
|
1192
|
|
|
foreach ($haystack as $value) { |
|
1193
|
|
|
if ($needle == $value && (!$strict || $needle === $value)) { |
|
1194
|
|
|
return true; |
|
1195
|
|
|
} |
|
1196
|
|
|
} |
|
1197
|
1 |
|
|
|
1198
|
|
|
return false; |
|
1199
|
1 |
|
} |
|
1200
|
1 |
|
|
|
1201
|
1 |
|
/** |
|
1202
|
|
|
* Checks whether an array or {@see \Traversable} is a subset of another array or {@see \Traversable}. |
|
1203
|
|
|
* |
|
1204
|
|
|
* This method will return `true`, if all elements of `$needles` are contained in |
|
1205
|
1 |
|
* `$haystack`. If at least one element is missing, `false` will be returned. |
|
1206
|
|
|
* |
|
1207
|
|
|
* @param iterable $needles The values that must **all** be in `$haystack`. |
|
1208
|
|
|
* @param iterable $haystack The set of value to search. |
|
1209
|
|
|
* @param bool $strict Whether to enable strict (`===`) comparison. |
|
1210
|
|
|
* |
|
1211
|
|
|
* @throws InvalidArgumentException if `$haystack` or `$needles` is neither traversable nor an array. |
|
1212
|
|
|
* |
|
1213
|
|
|
* @return bool `true` if `$needles` is a subset of `$haystack`, `false` otherwise. |
|
1214
|
|
|
*/ |
|
1215
|
|
|
public static function isSubset(iterable $needles, iterable $haystack, bool $strict = false): bool |
|
1216
|
|
|
{ |
|
1217
|
|
|
foreach ($needles as $needle) { |
|
1218
|
|
|
if (!self::isIn($needle, $haystack, $strict)) { |
|
1219
|
|
|
return false; |
|
1220
|
|
|
} |
|
1221
|
|
|
} |
|
1222
|
|
|
|
|
1223
|
|
|
return true; |
|
1224
|
|
|
} |
|
1225
|
|
|
|
|
1226
|
|
|
/** |
|
1227
|
|
|
* Filters array according to rules specified. |
|
1228
|
|
|
* |
|
1229
|
|
|
* For example: |
|
1230
|
|
|
* |
|
1231
|
|
|
* ```php |
|
1232
|
|
|
* $array = [ |
|
1233
|
|
|
* 'A' => [1, 2], |
|
1234
|
|
|
* 'B' => [ |
|
1235
|
|
|
* 'C' => 1, |
|
1236
|
|
|
* 'D' => 2, |
|
1237
|
|
|
* ], |
|
1238
|
|
|
* 'E' => 1, |
|
1239
|
|
|
* ]; |
|
1240
|
|
|
* |
|
1241
|
|
|
* $result = \Yiisoft\Arrays\ArrayHelper::filter($array, ['A']); |
|
1242
|
|
|
* // $result will be: |
|
1243
|
|
|
* // [ |
|
1244
|
|
|
* // 'A' => [1, 2], |
|
1245
|
|
|
* // ] |
|
1246
|
|
|
* |
|
1247
|
|
|
* $result = \Yiisoft\Arrays\ArrayHelper::filter($array, ['A', 'B.C']); |
|
1248
|
|
|
* // $result will be: |
|
1249
|
|
|
* // [ |
|
1250
|
|
|
* // 'A' => [1, 2], |
|
1251
|
|
|
* // 'B' => ['C' => 1], |
|
1252
|
17 |
|
* // ] |
|
1253
|
|
|
* |
|
1254
|
17 |
|
* $result = \Yiisoft\Arrays\ArrayHelper::filter($array, ['B', '!B.C']); |
|
1255
|
17 |
|
* // $result will be: |
|
1256
|
|
|
* // [ |
|
1257
|
17 |
|
* // 'B' => ['D' => 2], |
|
1258
|
17 |
|
* // ] |
|
1259
|
6 |
|
* ``` |
|
1260
|
6 |
|
* |
|
1261
|
|
|
* @param array $array Source array. |
|
1262
|
|
|
* @param list<string> $filters Rules that define array keys which should be left or removed from results. |
|
|
|
|
|
|
1263
|
17 |
|
* Each rule is: |
|
1264
|
17 |
|
* - `var` - `$array['var']` will be left in result. |
|
1265
|
17 |
|
* - `var.key` = only `$array['var']['key']` will be left in result. |
|
1266
|
17 |
|
* - `!var.key` = `$array['var']['key']` will be removed from result. |
|
1267
|
4 |
|
* |
|
1268
|
|
|
* @return array Filtered array. |
|
1269
|
15 |
|
*/ |
|
1270
|
|
|
public static function filter(array $array, array $filters): array |
|
1271
|
|
|
{ |
|
1272
|
|
|
$result = []; |
|
1273
|
13 |
|
$excludeFilters = []; |
|
1274
|
13 |
|
|
|
1275
|
13 |
|
foreach ($filters as $filter) { |
|
1276
|
13 |
|
if ($filter[0] === '!') { |
|
1277
|
|
|
$excludeFilters[] = substr($filter, 1); |
|
1278
|
|
|
continue; |
|
1279
|
13 |
|
} |
|
1280
|
|
|
|
|
1281
|
|
|
$nodeValue = $array; // Set $array as root node. |
|
1282
|
|
|
$keys = explode('.', $filter); |
|
1283
|
13 |
|
foreach ($keys as $key) { |
|
1284
|
|
|
if (!is_array($nodeValue) || !array_key_exists($key, $nodeValue)) { |
|
1285
|
|
|
continue 2; // Jump to next filter. |
|
1286
|
|
|
} |
|
1287
|
|
|
$nodeValue = $nodeValue[$key]; |
|
1288
|
|
|
} |
|
1289
|
|
|
|
|
1290
|
|
|
// We've found a value now let's insert it. |
|
1291
|
|
|
$resultNode = &$result; |
|
1292
|
17 |
|
foreach ($keys as $key) { |
|
1293
|
6 |
|
if (!array_key_exists($key, $resultNode)) { |
|
1294
|
6 |
|
$resultNode[$key] = []; |
|
1295
|
6 |
|
} |
|
1296
|
6 |
|
/** @psalm-suppress MixedAssignment */ |
|
1297
|
6 |
|
$resultNode = &$resultNode[$key]; |
|
1298
|
2 |
|
/** @var array $resultNode */ |
|
1299
|
|
|
} |
|
1300
|
|
|
/** @var array */ |
|
1301
|
5 |
|
$resultNode = $nodeValue; |
|
1302
|
|
|
} |
|
1303
|
5 |
|
|
|
1304
|
|
|
/** |
|
1305
|
4 |
|
* @psalm-suppress UnnecessaryVarAnnotation |
|
1306
|
4 |
|
* |
|
1307
|
|
|
* @var array $result |
|
1308
|
|
|
*/ |
|
1309
|
|
|
|
|
1310
|
|
|
foreach ($excludeFilters as $filter) { |
|
1311
|
|
|
$excludeNode = &$result; |
|
1312
|
|
|
$keys = explode('.', $filter); |
|
1313
|
17 |
|
$numNestedKeys = count($keys) - 1; |
|
1314
|
|
|
foreach ($keys as $i => $key) { |
|
1315
|
|
|
if (!is_array($excludeNode) || !array_key_exists($key, $excludeNode)) { |
|
1316
|
|
|
continue 2; // Jump to next filter. |
|
1317
|
|
|
} |
|
1318
|
|
|
|
|
1319
|
|
|
if ($i < $numNestedKeys) { |
|
1320
|
|
|
/** @psalm-suppress MixedAssignment */ |
|
1321
|
|
|
$excludeNode = &$excludeNode[$key]; |
|
1322
|
|
|
} else { |
|
1323
|
|
|
unset($excludeNode[$key]); |
|
1324
|
|
|
break; |
|
1325
|
|
|
} |
|
1326
|
|
|
} |
|
1327
|
|
|
} |
|
1328
|
4 |
|
|
|
1329
|
|
|
/** @var array $result */ |
|
1330
|
4 |
|
|
|
1331
|
|
|
return $result; |
|
1332
|
|
|
} |
|
1333
|
|
|
|
|
1334
|
|
|
/** |
|
1335
|
|
|
* Returns the public member variables of an object. |
|
1336
|
9 |
|
* |
|
1337
|
|
|
* This method is provided such that we can get the public member variables of an object, because a direct call of |
|
1338
|
9 |
|
* {@see get_object_vars()} (within the object itself) will return only private and protected variables. |
|
1339
|
9 |
|
* |
|
1340
|
8 |
|
* @param object $object The object to be handled. |
|
1341
|
8 |
|
* |
|
1342
|
6 |
|
* @return array The public member variables of the object. |
|
1343
|
6 |
|
* |
|
1344
|
6 |
|
* @link https://www.php.net/manual/en/function.get-object-vars.php |
|
1345
|
|
|
*/ |
|
1346
|
|
|
public static function getObjectVars(object $object): array |
|
1347
|
6 |
|
{ |
|
1348
|
|
|
return get_object_vars($object); |
|
1349
|
|
|
} |
|
1350
|
6 |
|
|
|
1351
|
6 |
|
/** |
|
1352
|
6 |
|
* @param array[] $arrays |
|
1353
|
6 |
|
*/ |
|
1354
|
|
|
private static function doMerge(array $arrays, ?int $depth, int $currentDepth = 0): array |
|
1355
|
5 |
|
{ |
|
1356
|
|
|
$result = array_shift($arrays) ?: []; |
|
1357
|
6 |
|
while (!empty($arrays)) { |
|
1358
|
|
|
foreach (array_shift($arrays) as $key => $value) { |
|
1359
|
|
|
if (is_int($key)) { |
|
1360
|
|
|
if (array_key_exists($key, $result)) { |
|
1361
|
9 |
|
if ($result[$key] !== $value) { |
|
1362
|
|
|
$result[] = $value; |
|
1363
|
|
|
} |
|
1364
|
171 |
|
} else { |
|
1365
|
|
|
$result[$key] = $value; |
|
1366
|
171 |
|
} |
|
1367
|
|
|
} elseif ( |
|
1368
|
|
|
isset($result[$key]) |
|
1369
|
|
|
&& ($depth === null || $currentDepth < $depth) |
|
1370
|
|
|
&& is_array($value) |
|
1371
|
|
|
&& is_array($result[$key]) |
|
1372
|
|
|
) { |
|
1373
|
|
|
$result[$key] = self::doMerge([$result[$key], $value], $depth, $currentDepth + 1); |
|
1374
|
105 |
|
} else { |
|
1375
|
|
|
$result[$key] = $value; |
|
1376
|
105 |
|
} |
|
1377
|
19 |
|
} |
|
1378
|
19 |
|
} |
|
1379
|
19 |
|
return $result; |
|
1380
|
19 |
|
} |
|
1381
|
19 |
|
|
|
1382
|
19 |
|
private static function normalizeArrayKey(mixed $key): string |
|
1383
|
|
|
{ |
|
1384
|
|
|
return is_float($key) ? NumericHelper::normalize($key) : (string)$key; |
|
1385
|
9 |
|
} |
|
1386
|
|
|
|
|
1387
|
5 |
|
/** |
|
1388
|
5 |
|
* @psalm-param ArrayPath $path |
|
1389
|
5 |
|
* |
|
1390
|
|
|
* @psalm-return ArrayKey |
|
1391
|
|
|
*/ |
|
1392
|
4 |
|
private static function parseMixedPath(array|float|int|string $path, string $delimiter): array|float|int|string |
|
1393
|
|
|
{ |
|
1394
|
19 |
|
if (is_array($path)) { |
|
|
|
|
|
|
1395
|
|
|
$newPath = []; |
|
1396
|
|
|
foreach ($path as $key) { |
|
1397
|
87 |
|
if (is_string($key)) { |
|
1398
|
|
|
$parsedPath = StringHelper::parsePath($key, $delimiter); |
|
1399
|
|
|
$newPath = array_merge($newPath, $parsedPath); |
|
1400
|
|
|
continue; |
|
1401
|
|
|
} |
|
1402
|
|
|
|
|
1403
|
|
|
if (is_array($key)) { |
|
1404
|
|
|
/** @var list<float|int|string> $parsedPath */ |
|
1405
|
|
|
$parsedPath = self::parseMixedPath($key, $delimiter); |
|
1406
|
|
|
$newPath = array_merge($newPath, $parsedPath); |
|
|
|
|
|
|
1407
|
|
|
continue; |
|
1408
|
|
|
} |
|
1409
|
|
|
|
|
1410
|
|
|
$newPath[] = $key; |
|
1411
|
|
|
} |
|
1412
|
|
|
return $newPath; |
|
1413
|
|
|
} |
|
1414
|
|
|
|
|
1415
|
|
|
return is_string($path) ? StringHelper::parsePath($path, $delimiter) : $path; |
|
1416
|
|
|
} |
|
1417
|
|
|
} |
|
1418
|
|
|
|