1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @link https://www.yiiframework.com/ |
5
|
|
|
* @copyright Copyright (c) 2008 Yii Software LLC |
6
|
|
|
* @license https://www.yiiframework.com/license/ |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace yii\helpers; |
10
|
|
|
|
11
|
|
|
use Yii; |
12
|
|
|
use ArrayAccess; |
13
|
|
|
use Traversable; |
14
|
|
|
use yii\base\Arrayable; |
15
|
|
|
use yii\base\InvalidArgumentException; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* BaseArrayHelper provides concrete implementation for [[ArrayHelper]]. |
19
|
|
|
* |
20
|
|
|
* Do not use BaseArrayHelper. Use [[ArrayHelper]] instead. |
21
|
|
|
* |
22
|
|
|
* @author Qiang Xue <[email protected]> |
23
|
|
|
* @since 2.0 |
24
|
|
|
*/ |
25
|
|
|
class BaseArrayHelper |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* Converts an object or an array of objects into an array. |
29
|
|
|
* @param object|array|string $object the object to be converted into an array |
30
|
|
|
* @param array $properties a mapping from object class names to the properties that need to put into the resulting arrays. |
31
|
|
|
* The properties specified for each class is an array of the following format: |
32
|
|
|
* |
33
|
|
|
* ```php |
34
|
|
|
* [ |
35
|
|
|
* 'app\models\Post' => [ |
36
|
|
|
* 'id', |
37
|
|
|
* 'title', |
38
|
|
|
* // the key name in array result => property name |
39
|
|
|
* 'createTime' => 'created_at', |
40
|
|
|
* // the key name in array result => anonymous function |
41
|
|
|
* 'length' => function ($post) { |
42
|
|
|
* return strlen($post->content); |
43
|
|
|
* }, |
44
|
|
|
* ], |
45
|
|
|
* ] |
46
|
|
|
* ``` |
47
|
|
|
* |
48
|
|
|
* The result of `ArrayHelper::toArray($post, $properties)` could be like the following: |
49
|
|
|
* |
50
|
|
|
* ```php |
51
|
|
|
* [ |
52
|
|
|
* 'id' => 123, |
53
|
|
|
* 'title' => 'test', |
54
|
|
|
* 'createTime' => '2013-01-01 12:00AM', |
55
|
|
|
* 'length' => 301, |
56
|
|
|
* ] |
57
|
|
|
* ``` |
58
|
|
|
* |
59
|
|
|
* @param bool $recursive whether to recursively converts properties which are objects into arrays. |
60
|
|
|
* @return array the array representation of the object |
61
|
|
|
*/ |
62
|
35 |
|
public static function toArray($object, $properties = [], $recursive = true) |
63
|
|
|
{ |
64
|
35 |
|
if (is_array($object)) { |
65
|
35 |
|
if ($recursive) { |
66
|
35 |
|
foreach ($object as $key => $value) { |
67
|
35 |
|
if ($value instanceof \UnitEnum) { |
|
|
|
|
68
|
|
|
$object[$key] = $value->value; |
69
|
35 |
|
} elseif (is_array($value) || is_object($value)) { |
70
|
4 |
|
$object[$key] = static::toArray($value, $properties, true); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
35 |
|
return $object; |
76
|
8 |
|
} elseif ($object instanceof \DateTimeInterface) { |
77
|
1 |
|
return (array)$object; |
78
|
8 |
|
} elseif (is_object($object)) { |
79
|
8 |
|
if (!empty($properties)) { |
80
|
1 |
|
$className = get_class($object); |
81
|
1 |
|
if (!empty($properties[$className])) { |
82
|
1 |
|
$result = []; |
83
|
1 |
|
foreach ($properties[$className] as $key => $name) { |
84
|
1 |
|
if (is_int($key)) { |
85
|
1 |
|
$result[$name] = $object->$name; |
86
|
|
|
} else { |
87
|
1 |
|
$result[$key] = static::getValue($object, $name); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
1 |
|
return $recursive ? static::toArray($result, $properties) : $result; |
92
|
|
|
} |
93
|
|
|
} |
94
|
8 |
|
if ($object instanceof Arrayable) { |
95
|
1 |
|
$result = $object->toArray([], [], $recursive); |
96
|
|
|
} else { |
97
|
8 |
|
$result = []; |
98
|
8 |
|
foreach ($object as $key => $value) { |
99
|
8 |
|
$result[$key] = $value; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
8 |
|
return $recursive ? static::toArray($result, $properties) : $result; |
104
|
|
|
} |
105
|
|
|
|
106
|
1 |
|
return [$object]; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Merges two or more arrays into one recursively. |
111
|
|
|
* If each array has an element with the same string key value, the latter |
112
|
|
|
* will overwrite the former (different from array_merge_recursive). |
113
|
|
|
* Recursive merging will be conducted if both arrays have an element of array |
114
|
|
|
* type and are having the same key. |
115
|
|
|
* For integer-keyed elements, the elements from the latter array will |
116
|
|
|
* be appended to the former array. |
117
|
|
|
* You can use [[UnsetArrayValue]] object to unset value from previous array or |
118
|
|
|
* [[ReplaceArrayValue]] to force replace former value instead of recursive merging. |
119
|
|
|
* @param array $a array to be merged to |
120
|
|
|
* @param array $b array to be merged from. You can specify additional |
121
|
|
|
* arrays via third argument, fourth argument etc. |
122
|
|
|
* @return array the merged array (the original arrays are not changed.) |
123
|
|
|
*/ |
124
|
4518 |
|
public static function merge($a, $b) |
125
|
|
|
{ |
126
|
4518 |
|
$args = func_get_args(); |
127
|
4518 |
|
$res = array_shift($args); |
128
|
4518 |
|
while (!empty($args)) { |
129
|
4518 |
|
foreach (array_shift($args) as $k => $v) { |
130
|
1535 |
|
if ($v instanceof UnsetArrayValue) { |
131
|
1 |
|
unset($res[$k]); |
132
|
1535 |
|
} elseif ($v instanceof ReplaceArrayValue) { |
133
|
1 |
|
$res[$k] = $v->value; |
134
|
1535 |
|
} elseif (is_int($k)) { |
135
|
5 |
|
if (array_key_exists($k, $res)) { |
136
|
5 |
|
$res[] = $v; |
137
|
|
|
} else { |
138
|
5 |
|
$res[$k] = $v; |
139
|
|
|
} |
140
|
1533 |
|
} elseif (is_array($v) && isset($res[$k]) && is_array($res[$k])) { |
141
|
268 |
|
$res[$k] = static::merge($res[$k], $v); |
142
|
|
|
} else { |
143
|
1533 |
|
$res[$k] = $v; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
4518 |
|
return $res; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Retrieves the value of an array element or object property with the given key or property name. |
153
|
|
|
* If the key does not exist in the array, the default value will be returned instead. |
154
|
|
|
* Not used when getting value from an object. |
155
|
|
|
* |
156
|
|
|
* The key may be specified in a dot format to retrieve the value of a sub-array or the property |
157
|
|
|
* of an embedded object. In particular, if the key is `x.y.z`, then the returned value would |
158
|
|
|
* be `$array['x']['y']['z']` or `$array->x->y->z` (if `$array` is an object). If `$array['x']` |
159
|
|
|
* or `$array->x` is neither an array nor an object, the default value will be returned. |
160
|
|
|
* Note that if the array already has an element `x.y.z`, then its value will be returned |
161
|
|
|
* instead of going through the sub-arrays. So it is better to be done specifying an array of key names |
162
|
|
|
* like `['x', 'y', 'z']`. |
163
|
|
|
* |
164
|
|
|
* Below are some usage examples, |
165
|
|
|
* |
166
|
|
|
* ```php |
167
|
|
|
* // working with array |
168
|
|
|
* $username = \yii\helpers\ArrayHelper::getValue($_POST, 'username'); |
169
|
|
|
* // working with object |
170
|
|
|
* $username = \yii\helpers\ArrayHelper::getValue($user, 'username'); |
171
|
|
|
* // working with anonymous function |
172
|
|
|
* $fullName = \yii\helpers\ArrayHelper::getValue($user, function ($user, $defaultValue) { |
173
|
|
|
* return $user->firstName . ' ' . $user->lastName; |
174
|
|
|
* }); |
175
|
|
|
* // using dot format to retrieve the property of embedded object |
176
|
|
|
* $street = \yii\helpers\ArrayHelper::getValue($users, 'address.street'); |
177
|
|
|
* // using an array of keys to retrieve the value |
178
|
|
|
* $value = \yii\helpers\ArrayHelper::getValue($versions, ['1.0', 'date']); |
179
|
|
|
* ``` |
180
|
|
|
* |
181
|
|
|
* @param array|object $array array or object to extract value from |
182
|
|
|
* @param string|\Closure|array $key key name of the array element, an array of keys or property name of the object, |
183
|
|
|
* or an anonymous function returning the value. The anonymous function signature should be: |
184
|
|
|
* `function($array, $defaultValue)`. |
185
|
|
|
* The possibility to pass an array of keys is available since version 2.0.4. |
186
|
|
|
* @param mixed $default the default value to be returned if the specified array key does not exist. Not used when |
187
|
|
|
* getting value from an object. |
188
|
|
|
* @return mixed the value of the element if found, default value otherwise |
189
|
|
|
*/ |
190
|
476 |
|
public static function getValue($array, $key, $default = null) |
191
|
|
|
{ |
192
|
476 |
|
if ($key instanceof \Closure) { |
193
|
15 |
|
return $key($array, $default); |
194
|
|
|
} |
195
|
|
|
|
196
|
474 |
|
if (is_array($key)) { |
197
|
2 |
|
$lastKey = array_pop($key); |
198
|
2 |
|
foreach ($key as $keyPart) { |
199
|
2 |
|
$array = static::getValue($array, $keyPart); |
200
|
|
|
} |
201
|
2 |
|
$key = $lastKey; |
202
|
|
|
} |
203
|
|
|
|
204
|
474 |
|
if (is_object($array) && property_exists($array, $key)) { |
205
|
11 |
|
return $array->$key; |
206
|
|
|
} |
207
|
|
|
|
208
|
467 |
|
if (static::keyExists($key, $array)) { |
209
|
408 |
|
return $array[$key]; |
210
|
|
|
} |
211
|
|
|
|
212
|
102 |
|
if ($key && ($pos = strrpos($key, '.')) !== false) { |
213
|
45 |
|
$array = static::getValue($array, substr($key, 0, $pos), $default); |
214
|
45 |
|
$key = substr($key, $pos + 1); |
215
|
|
|
} |
216
|
|
|
|
217
|
102 |
|
if (static::keyExists($key, $array)) { |
218
|
16 |
|
return $array[$key]; |
219
|
|
|
} |
220
|
93 |
|
if (is_object($array)) { |
221
|
|
|
// this is expected to fail if the property does not exist, or __get() is not implemented |
222
|
|
|
// it is not reliably possible to check whether a property is accessible beforehand |
223
|
|
|
try { |
224
|
5 |
|
return $array->$key; |
225
|
3 |
|
} catch (\Exception $e) { |
|
|
|
|
226
|
3 |
|
if ($array instanceof ArrayAccess) { |
227
|
2 |
|
return $default; |
228
|
|
|
} |
229
|
1 |
|
throw $e; |
230
|
|
|
} |
231
|
|
|
} |
232
|
|
|
|
233
|
89 |
|
return $default; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Writes a value into an associative array at the key path specified. |
238
|
|
|
* If there is no such key path yet, it will be created recursively. |
239
|
|
|
* If the key exists, it will be overwritten. |
240
|
|
|
* |
241
|
|
|
* ```php |
242
|
|
|
* $array = [ |
243
|
|
|
* 'key' => [ |
244
|
|
|
* 'in' => [ |
245
|
|
|
* 'val1', |
246
|
|
|
* 'key' => 'val' |
247
|
|
|
* ] |
248
|
|
|
* ] |
249
|
|
|
* ]; |
250
|
|
|
* ``` |
251
|
|
|
* |
252
|
|
|
* The result of `ArrayHelper::setValue($array, 'key.in.0', ['arr' => 'val']);` will be the following: |
253
|
|
|
* |
254
|
|
|
* ```php |
255
|
|
|
* [ |
256
|
|
|
* 'key' => [ |
257
|
|
|
* 'in' => [ |
258
|
|
|
* ['arr' => 'val'], |
259
|
|
|
* 'key' => 'val' |
260
|
|
|
* ] |
261
|
|
|
* ] |
262
|
|
|
* ] |
263
|
|
|
* |
264
|
|
|
* ``` |
265
|
|
|
* |
266
|
|
|
* The result of |
267
|
|
|
* `ArrayHelper::setValue($array, 'key.in', ['arr' => 'val']);` or |
268
|
|
|
* `ArrayHelper::setValue($array, ['key', 'in'], ['arr' => 'val']);` |
269
|
|
|
* will be the following: |
270
|
|
|
* |
271
|
|
|
* ```php |
272
|
|
|
* [ |
273
|
|
|
* 'key' => [ |
274
|
|
|
* 'in' => [ |
275
|
|
|
* 'arr' => 'val' |
276
|
|
|
* ] |
277
|
|
|
* ] |
278
|
|
|
* ] |
279
|
|
|
* ``` |
280
|
|
|
* |
281
|
|
|
* @param array $array the array to write the value to |
282
|
|
|
* @param string|array|null $path the path of where do you want to write a value to `$array` |
283
|
|
|
* the path can be described by a string when each key should be separated by a dot |
284
|
|
|
* you can also describe the path as an array of keys |
285
|
|
|
* if the path is null then `$array` will be assigned the `$value` |
286
|
|
|
* @param mixed $value the value to be written |
287
|
|
|
* @since 2.0.13 |
288
|
|
|
*/ |
289
|
16 |
|
public static function setValue(&$array, $path, $value) |
290
|
|
|
{ |
291
|
16 |
|
if ($path === null) { |
292
|
1 |
|
$array = $value; |
293
|
1 |
|
return; |
294
|
|
|
} |
295
|
|
|
|
296
|
15 |
|
$keys = is_array($path) ? $path : explode('.', $path); |
297
|
|
|
|
298
|
15 |
|
while (count($keys) > 1) { |
299
|
12 |
|
$key = array_shift($keys); |
300
|
12 |
|
if (!isset($array[$key])) { |
301
|
4 |
|
$array[$key] = []; |
302
|
|
|
} |
303
|
12 |
|
if (!is_array($array[$key])) { |
304
|
2 |
|
$array[$key] = [$array[$key]]; |
305
|
|
|
} |
306
|
12 |
|
$array = &$array[$key]; |
307
|
|
|
} |
308
|
|
|
|
309
|
15 |
|
$array[array_shift($keys)] = $value; |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* Removes an item from an array and returns the value. If the key does not exist in the array, the default value |
314
|
|
|
* will be returned instead. |
315
|
|
|
* |
316
|
|
|
* Usage examples, |
317
|
|
|
* |
318
|
|
|
* ```php |
319
|
|
|
* // $array = ['type' => 'A', 'options' => [1, 2]]; |
320
|
|
|
* // working with array |
321
|
|
|
* $type = \yii\helpers\ArrayHelper::remove($array, 'type'); |
322
|
|
|
* // $array content |
323
|
|
|
* // $array = ['options' => [1, 2]]; |
324
|
|
|
* ``` |
325
|
|
|
* |
326
|
|
|
* @param array $array the array to extract value from |
327
|
|
|
* @param string $key key name of the array element |
328
|
|
|
* @param mixed $default the default value to be returned if the specified key does not exist |
329
|
|
|
* @return mixed|null the value of the element if found, default value otherwise |
330
|
|
|
*/ |
331
|
241 |
|
public static function remove(&$array, $key, $default = null) |
332
|
|
|
{ |
333
|
|
|
// ToDo: This check can be removed when the minimum PHP version is >= 8.1 (Yii2.2) |
334
|
241 |
|
if (is_float($key)) { |
|
|
|
|
335
|
|
|
$key = (int)$key; |
336
|
|
|
} |
337
|
|
|
|
338
|
241 |
|
if (is_array($array) && array_key_exists($key, $array)) { |
339
|
58 |
|
$value = $array[$key]; |
340
|
58 |
|
unset($array[$key]); |
341
|
|
|
|
342
|
58 |
|
return $value; |
343
|
|
|
} |
344
|
|
|
|
345
|
233 |
|
return $default; |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
/** |
349
|
|
|
* Removes items with matching values from the array and returns the removed items. |
350
|
|
|
* |
351
|
|
|
* Example, |
352
|
|
|
* |
353
|
|
|
* ```php |
354
|
|
|
* $array = ['Bob' => 'Dylan', 'Michael' => 'Jackson', 'Mick' => 'Jagger', 'Janet' => 'Jackson']; |
355
|
|
|
* $removed = \yii\helpers\ArrayHelper::removeValue($array, 'Jackson'); |
356
|
|
|
* // result: |
357
|
|
|
* // $array = ['Bob' => 'Dylan', 'Mick' => 'Jagger']; |
358
|
|
|
* // $removed = ['Michael' => 'Jackson', 'Janet' => 'Jackson']; |
359
|
|
|
* ``` |
360
|
|
|
* |
361
|
|
|
* @param array $array the array where to look the value from |
362
|
|
|
* @param mixed $value the value to remove from the array |
363
|
|
|
* @return array the items that were removed from the array |
364
|
|
|
* @since 2.0.11 |
365
|
|
|
*/ |
366
|
2 |
|
public static function removeValue(&$array, $value) |
367
|
|
|
{ |
368
|
2 |
|
$result = []; |
369
|
2 |
|
if (is_array($array)) { |
|
|
|
|
370
|
2 |
|
foreach ($array as $key => $val) { |
371
|
2 |
|
if ($val === $value) { |
372
|
1 |
|
$result[$key] = $val; |
373
|
1 |
|
unset($array[$key]); |
374
|
|
|
} |
375
|
|
|
} |
376
|
|
|
} |
377
|
|
|
|
378
|
2 |
|
return $result; |
379
|
|
|
} |
380
|
|
|
|
381
|
|
|
/** |
382
|
|
|
* Indexes and/or groups the array according to a specified key. |
383
|
|
|
* The input should be either multidimensional array or an array of objects. |
384
|
|
|
* |
385
|
|
|
* The $key can be either a key name of the sub-array, a property name of object, or an anonymous |
386
|
|
|
* function that must return the value that will be used as a key. |
387
|
|
|
* |
388
|
|
|
* $groups is an array of keys, that will be used to group the input array into one or more sub-arrays based |
389
|
|
|
* on keys specified. |
390
|
|
|
* |
391
|
|
|
* If the `$key` is specified as `null` or a value of an element corresponding to the key is `null` in addition |
392
|
|
|
* to `$groups` not specified then the element is discarded. |
393
|
|
|
* |
394
|
|
|
* For example: |
395
|
|
|
* |
396
|
|
|
* ```php |
397
|
|
|
* $array = [ |
398
|
|
|
* ['id' => '123', 'data' => 'abc', 'device' => 'laptop'], |
399
|
|
|
* ['id' => '345', 'data' => 'def', 'device' => 'tablet'], |
400
|
|
|
* ['id' => '345', 'data' => 'hgi', 'device' => 'smartphone'], |
401
|
|
|
* ]; |
402
|
|
|
* $result = ArrayHelper::index($array, 'id'); |
403
|
|
|
* ``` |
404
|
|
|
* |
405
|
|
|
* The result will be an associative array, where the key is the value of `id` attribute |
406
|
|
|
* |
407
|
|
|
* ```php |
408
|
|
|
* [ |
409
|
|
|
* '123' => ['id' => '123', 'data' => 'abc', 'device' => 'laptop'], |
410
|
|
|
* '345' => ['id' => '345', 'data' => 'hgi', 'device' => 'smartphone'] |
411
|
|
|
* // The second element of an original array is overwritten by the last element because of the same id |
412
|
|
|
* ] |
413
|
|
|
* ``` |
414
|
|
|
* |
415
|
|
|
* An anonymous function can be used in the grouping array as well. |
416
|
|
|
* |
417
|
|
|
* ```php |
418
|
|
|
* $result = ArrayHelper::index($array, function ($element) { |
419
|
|
|
* return $element['id']; |
420
|
|
|
* }); |
421
|
|
|
* ``` |
422
|
|
|
* |
423
|
|
|
* Passing `id` as a third argument will group `$array` by `id`: |
424
|
|
|
* |
425
|
|
|
* ```php |
426
|
|
|
* $result = ArrayHelper::index($array, null, 'id'); |
427
|
|
|
* ``` |
428
|
|
|
* |
429
|
|
|
* The result will be a multidimensional array grouped by `id` on the first level, by `device` on the second level |
430
|
|
|
* and indexed by `data` on the third level: |
431
|
|
|
* |
432
|
|
|
* ```php |
433
|
|
|
* [ |
434
|
|
|
* '123' => [ |
435
|
|
|
* ['id' => '123', 'data' => 'abc', 'device' => 'laptop'] |
436
|
|
|
* ], |
437
|
|
|
* '345' => [ // all elements with this index are present in the result array |
438
|
|
|
* ['id' => '345', 'data' => 'def', 'device' => 'tablet'], |
439
|
|
|
* ['id' => '345', 'data' => 'hgi', 'device' => 'smartphone'], |
440
|
|
|
* ] |
441
|
|
|
* ] |
442
|
|
|
* ``` |
443
|
|
|
* |
444
|
|
|
* The anonymous function can be used in the array of grouping keys as well: |
445
|
|
|
* |
446
|
|
|
* ```php |
447
|
|
|
* $result = ArrayHelper::index($array, 'data', [function ($element) { |
448
|
|
|
* return $element['id']; |
449
|
|
|
* }, 'device']); |
450
|
|
|
* ``` |
451
|
|
|
* |
452
|
|
|
* The result will be a multidimensional array grouped by `id` on the first level, by the `device` on the second one |
453
|
|
|
* and indexed by the `data` on the third level: |
454
|
|
|
* |
455
|
|
|
* ```php |
456
|
|
|
* [ |
457
|
|
|
* '123' => [ |
458
|
|
|
* 'laptop' => [ |
459
|
|
|
* 'abc' => ['id' => '123', 'data' => 'abc', 'device' => 'laptop'] |
460
|
|
|
* ] |
461
|
|
|
* ], |
462
|
|
|
* '345' => [ |
463
|
|
|
* 'tablet' => [ |
464
|
|
|
* 'def' => ['id' => '345', 'data' => 'def', 'device' => 'tablet'] |
465
|
|
|
* ], |
466
|
|
|
* 'smartphone' => [ |
467
|
|
|
* 'hgi' => ['id' => '345', 'data' => 'hgi', 'device' => 'smartphone'] |
468
|
|
|
* ] |
469
|
|
|
* ] |
470
|
|
|
* ] |
471
|
|
|
* ``` |
472
|
|
|
* |
473
|
|
|
* @param array $array the array that needs to be indexed or grouped |
474
|
|
|
* @param string|\Closure|null $key the column name or anonymous function which result will be used to index the array |
475
|
|
|
* @param string|string[]|\Closure[]|null $groups the array of keys, that will be used to group the input array |
476
|
|
|
* by one or more keys. If the $key attribute or its value for the particular element is null and $groups is not |
477
|
|
|
* defined, the array element will be discarded. Otherwise, if $groups is specified, array element will be added |
478
|
|
|
* to the result array without any key. This parameter is available since version 2.0.8. |
479
|
|
|
* @return array the indexed and/or grouped array |
480
|
|
|
*/ |
481
|
212 |
|
public static function index($array, $key, $groups = []) |
482
|
|
|
{ |
483
|
212 |
|
$result = []; |
484
|
212 |
|
$groups = (array) $groups; |
485
|
|
|
|
486
|
212 |
|
foreach ($array as $element) { |
487
|
209 |
|
$lastArray = &$result; |
488
|
|
|
|
489
|
209 |
|
foreach ($groups as $group) { |
490
|
176 |
|
$value = static::getValue($element, $group); |
491
|
176 |
|
if (!array_key_exists($value, $lastArray)) { |
492
|
176 |
|
$lastArray[$value] = []; |
493
|
|
|
} |
494
|
176 |
|
$lastArray = &$lastArray[$value]; |
495
|
|
|
} |
496
|
|
|
|
497
|
209 |
|
if ($key === null) { |
498
|
177 |
|
if (!empty($groups)) { |
499
|
177 |
|
$lastArray[] = $element; |
500
|
|
|
} |
501
|
|
|
} else { |
502
|
34 |
|
$value = static::getValue($element, $key); |
503
|
34 |
|
if ($value !== null) { |
504
|
34 |
|
if (is_float($value)) { |
505
|
1 |
|
$value = StringHelper::floatToString($value); |
506
|
|
|
} |
507
|
34 |
|
$lastArray[$value] = $element; |
508
|
|
|
} |
509
|
|
|
} |
510
|
209 |
|
unset($lastArray); |
511
|
|
|
} |
512
|
|
|
|
513
|
212 |
|
return $result; |
514
|
|
|
} |
515
|
|
|
|
516
|
|
|
/** |
517
|
|
|
* Returns the values of a specified column in an array. |
518
|
|
|
* The input array should be multidimensional or an array of objects. |
519
|
|
|
* |
520
|
|
|
* For example, |
521
|
|
|
* |
522
|
|
|
* ```php |
523
|
|
|
* $array = [ |
524
|
|
|
* ['id' => '123', 'data' => 'abc'], |
525
|
|
|
* ['id' => '345', 'data' => 'def'], |
526
|
|
|
* ]; |
527
|
|
|
* $result = ArrayHelper::getColumn($array, 'id'); |
528
|
|
|
* // the result is: ['123', '345'] |
529
|
|
|
* |
530
|
|
|
* // using anonymous function |
531
|
|
|
* $result = ArrayHelper::getColumn($array, function ($element) { |
532
|
|
|
* return $element['id']; |
533
|
|
|
* }); |
534
|
|
|
* ``` |
535
|
|
|
* |
536
|
|
|
* @param array $array |
537
|
|
|
* @param int|string|array|\Closure $name |
538
|
|
|
* @param bool $keepKeys whether to maintain the array keys. If false, the resulting array |
539
|
|
|
* will be re-indexed with integers. |
540
|
|
|
* @return array the list of column values |
541
|
|
|
*/ |
542
|
267 |
|
public static function getColumn($array, $name, $keepKeys = true) |
543
|
|
|
{ |
544
|
267 |
|
$result = []; |
545
|
267 |
|
if ($keepKeys) { |
546
|
267 |
|
foreach ($array as $k => $element) { |
547
|
266 |
|
$result[$k] = static::getValue($element, $name); |
548
|
|
|
} |
549
|
|
|
} else { |
550
|
1 |
|
foreach ($array as $element) { |
551
|
1 |
|
$result[] = static::getValue($element, $name); |
552
|
|
|
} |
553
|
|
|
} |
554
|
|
|
|
555
|
267 |
|
return $result; |
556
|
|
|
} |
557
|
|
|
|
558
|
|
|
/** |
559
|
|
|
* Builds a map (key-value pairs) from a multidimensional array or an array of objects. |
560
|
|
|
* The `$from` and `$to` parameters specify the key names or property names to set up the map. |
561
|
|
|
* Optionally, one can further group the map according to a grouping field `$group`. |
562
|
|
|
* |
563
|
|
|
* For example, |
564
|
|
|
* |
565
|
|
|
* ```php |
566
|
|
|
* $array = [ |
567
|
|
|
* ['id' => '123', 'name' => 'aaa', 'class' => 'x'], |
568
|
|
|
* ['id' => '124', 'name' => 'bbb', 'class' => 'x'], |
569
|
|
|
* ['id' => '345', 'name' => 'ccc', 'class' => 'y'], |
570
|
|
|
* ]; |
571
|
|
|
* |
572
|
|
|
* $result = ArrayHelper::map($array, 'id', 'name'); |
573
|
|
|
* // the result is: |
574
|
|
|
* // [ |
575
|
|
|
* // '123' => 'aaa', |
576
|
|
|
* // '124' => 'bbb', |
577
|
|
|
* // '345' => 'ccc', |
578
|
|
|
* // ] |
579
|
|
|
* |
580
|
|
|
* $result = ArrayHelper::map($array, 'id', 'name', 'class'); |
581
|
|
|
* // the result is: |
582
|
|
|
* // [ |
583
|
|
|
* // 'x' => [ |
584
|
|
|
* // '123' => 'aaa', |
585
|
|
|
* // '124' => 'bbb', |
586
|
|
|
* // ], |
587
|
|
|
* // 'y' => [ |
588
|
|
|
* // '345' => 'ccc', |
589
|
|
|
* // ], |
590
|
|
|
* // ] |
591
|
|
|
* ``` |
592
|
|
|
* |
593
|
|
|
* @param array $array |
594
|
|
|
* @param string|\Closure $from |
595
|
|
|
* @param string|\Closure $to |
596
|
|
|
* @param string|\Closure|null $group |
597
|
|
|
* @return array |
598
|
|
|
*/ |
599
|
78 |
|
public static function map($array, $from, $to, $group = null) |
600
|
|
|
{ |
601
|
78 |
|
$result = []; |
602
|
78 |
|
foreach ($array as $element) { |
603
|
73 |
|
$key = static::getValue($element, $from); |
604
|
73 |
|
$value = static::getValue($element, $to); |
605
|
73 |
|
if ($group !== null) { |
606
|
1 |
|
$result[static::getValue($element, $group)][$key] = $value; |
607
|
|
|
} else { |
608
|
73 |
|
$result[$key] = $value; |
609
|
|
|
} |
610
|
|
|
} |
611
|
|
|
|
612
|
78 |
|
return $result; |
613
|
|
|
} |
614
|
|
|
|
615
|
|
|
/** |
616
|
|
|
* Checks if the given array contains the specified key. |
617
|
|
|
* This method enhances the `array_key_exists()` function by supporting case-insensitive |
618
|
|
|
* key comparison. |
619
|
|
|
* @param string|int $key the key to check |
620
|
|
|
* @param array|ArrayAccess $array the array with keys to check |
621
|
|
|
* @param bool $caseSensitive whether the key comparison should be case-sensitive |
622
|
|
|
* @return bool whether the array contains the specified key |
623
|
|
|
*/ |
624
|
487 |
|
public static function keyExists($key, $array, $caseSensitive = true) |
625
|
|
|
{ |
626
|
|
|
// ToDo: This check can be removed when the minimum PHP version is >= 8.1 (Yii2.2) |
627
|
487 |
|
if (is_float($key)) { |
|
|
|
|
628
|
|
|
$key = (int)$key; |
629
|
|
|
} |
630
|
|
|
|
631
|
487 |
|
if ($caseSensitive) { |
632
|
486 |
|
if (is_array($array) && array_key_exists($key, $array)) { |
633
|
375 |
|
return true; |
634
|
|
|
} |
635
|
|
|
// Cannot use `array_has_key` on Objects for PHP 7.4+, therefore we need to check using [[ArrayAccess::offsetExists()]] |
636
|
150 |
|
return $array instanceof ArrayAccess && $array->offsetExists($key); |
637
|
|
|
} |
638
|
|
|
|
639
|
2 |
|
if ($array instanceof ArrayAccess) { |
640
|
1 |
|
throw new InvalidArgumentException('Second parameter($array) cannot be ArrayAccess in case insensitive mode'); |
641
|
|
|
} |
642
|
|
|
|
643
|
1 |
|
foreach (array_keys($array) as $k) { |
644
|
1 |
|
if (strcasecmp($key, $k) === 0) { |
645
|
1 |
|
return true; |
646
|
|
|
} |
647
|
|
|
} |
648
|
|
|
|
649
|
1 |
|
return false; |
650
|
|
|
} |
651
|
|
|
|
652
|
|
|
/** |
653
|
|
|
* Sorts an array of objects or arrays (with the same structure) by one or several keys. |
654
|
|
|
* @param array $array the array to be sorted. The array will be modified after calling this method. |
655
|
|
|
* @param string|\Closure|array $key the key(s) to be sorted by. This refers to a key name of the sub-array |
656
|
|
|
* elements, a property name of the objects, or an anonymous function returning the values for comparison |
657
|
|
|
* purpose. The anonymous function signature should be: `function($item)`. |
658
|
|
|
* To sort by multiple keys, provide an array of keys here. |
659
|
|
|
* @param int|array $direction the sorting direction. It can be either `SORT_ASC` or `SORT_DESC`. |
660
|
|
|
* When sorting by multiple keys with different sorting directions, use an array of sorting directions. |
661
|
|
|
* @param int|array $sortFlag the PHP sort flag. Valid values include |
662
|
|
|
* `SORT_REGULAR`, `SORT_NUMERIC`, `SORT_STRING`, `SORT_LOCALE_STRING`, `SORT_NATURAL` and `SORT_FLAG_CASE`. |
663
|
|
|
* Please refer to [PHP manual](https://www.php.net/manual/en/function.sort.php) |
664
|
|
|
* for more details. When sorting by multiple keys with different sort flags, use an array of sort flags. |
665
|
|
|
* @throws InvalidArgumentException if the $direction or $sortFlag parameters do not have |
666
|
|
|
* correct number of elements as that of $key. |
667
|
|
|
*/ |
668
|
65 |
|
public static function multisort(&$array, $key, $direction = SORT_ASC, $sortFlag = SORT_REGULAR) |
669
|
|
|
{ |
670
|
65 |
|
$keys = is_array($key) ? $key : [$key]; |
671
|
65 |
|
if (empty($keys) || empty($array)) { |
672
|
1 |
|
return; |
673
|
|
|
} |
674
|
65 |
|
$n = count($keys); |
675
|
65 |
|
if (is_scalar($direction)) { |
676
|
58 |
|
$direction = array_fill(0, $n, $direction); |
677
|
7 |
|
} elseif (count($direction) !== $n) { |
678
|
1 |
|
throw new InvalidArgumentException('The length of $direction parameter must be the same as that of $keys.'); |
679
|
|
|
} |
680
|
64 |
|
if (is_scalar($sortFlag)) { |
681
|
63 |
|
$sortFlag = array_fill(0, $n, $sortFlag); |
682
|
2 |
|
} elseif (count($sortFlag) !== $n) { |
683
|
1 |
|
throw new InvalidArgumentException('The length of $sortFlag parameter must be the same as that of $keys.'); |
684
|
|
|
} |
685
|
63 |
|
$args = []; |
686
|
63 |
|
foreach ($keys as $i => $k) { |
687
|
63 |
|
$flag = $sortFlag[$i]; |
688
|
63 |
|
$args[] = static::getColumn($array, $k); |
689
|
63 |
|
$args[] = $direction[$i]; |
690
|
63 |
|
$args[] = $flag; |
691
|
|
|
} |
692
|
|
|
|
693
|
|
|
// This fix is used for cases when main sorting specified by columns has equal values |
694
|
|
|
// Without it it will lead to Fatal Error: Nesting level too deep - recursive dependency? |
695
|
63 |
|
$args[] = range(1, count($array)); |
696
|
63 |
|
$args[] = SORT_ASC; |
697
|
63 |
|
$args[] = SORT_NUMERIC; |
698
|
|
|
|
699
|
63 |
|
$args[] = &$array; |
700
|
63 |
|
call_user_func_array('array_multisort', $args); |
701
|
|
|
} |
702
|
|
|
|
703
|
|
|
/** |
704
|
|
|
* Encodes special characters in an array of strings into HTML entities. |
705
|
|
|
* Only array values will be encoded by default. |
706
|
|
|
* If a value is an array, this method will also encode it recursively. |
707
|
|
|
* Only string values will be encoded. |
708
|
|
|
* @param array $data data to be encoded |
709
|
|
|
* @param bool $valuesOnly whether to encode array values only. If false, |
710
|
|
|
* both the array keys and array values will be encoded. |
711
|
|
|
* @param string|null $charset the charset that the data is using. If not set, |
712
|
|
|
* [[\yii\base\Application::charset]] will be used. |
713
|
|
|
* @return array the encoded data |
714
|
|
|
* @see https://www.php.net/manual/en/function.htmlspecialchars.php |
715
|
|
|
*/ |
716
|
1 |
|
public static function htmlEncode($data, $valuesOnly = true, $charset = null) |
717
|
|
|
{ |
718
|
1 |
|
if ($charset === null) { |
719
|
1 |
|
$charset = Yii::$app ? Yii::$app->charset : 'UTF-8'; |
720
|
|
|
} |
721
|
1 |
|
$d = []; |
722
|
1 |
|
foreach ($data as $key => $value) { |
723
|
1 |
|
if (!$valuesOnly && is_string($key)) { |
724
|
1 |
|
$key = htmlspecialchars($key, ENT_QUOTES | ENT_SUBSTITUTE, $charset); |
725
|
|
|
} |
726
|
1 |
|
if (is_string($value)) { |
727
|
1 |
|
$d[$key] = htmlspecialchars($value, ENT_QUOTES | ENT_SUBSTITUTE, $charset); |
728
|
1 |
|
} elseif (is_array($value)) { |
729
|
1 |
|
$d[$key] = static::htmlEncode($value, $valuesOnly, $charset); |
730
|
|
|
} else { |
731
|
1 |
|
$d[$key] = $value; |
732
|
|
|
} |
733
|
|
|
} |
734
|
|
|
|
735
|
1 |
|
return $d; |
736
|
|
|
} |
737
|
|
|
|
738
|
|
|
/** |
739
|
|
|
* Decodes HTML entities into the corresponding characters in an array of strings. |
740
|
|
|
* |
741
|
|
|
* Only array values will be decoded by default. |
742
|
|
|
* If a value is an array, this method will also decode it recursively. |
743
|
|
|
* Only string values will be decoded. |
744
|
|
|
* |
745
|
|
|
* @param array $data data to be decoded |
746
|
|
|
* @param bool $valuesOnly whether to decode array values only. If `false`, |
747
|
|
|
* then both the array keys and array values will be decoded. |
748
|
|
|
* @return array the decoded data |
749
|
|
|
* @see https://www.php.net/manual/en/function.htmlspecialchars-decode.php |
750
|
|
|
*/ |
751
|
1 |
|
public static function htmlDecode($data, $valuesOnly = true) |
752
|
|
|
{ |
753
|
1 |
|
$d = []; |
754
|
1 |
|
foreach ($data as $key => $value) { |
755
|
1 |
|
if (!$valuesOnly && is_string($key)) { |
756
|
1 |
|
$key = htmlspecialchars_decode($key, ENT_QUOTES | ENT_SUBSTITUTE); |
757
|
|
|
} |
758
|
1 |
|
if (is_string($value)) { |
759
|
1 |
|
$d[$key] = htmlspecialchars_decode($value, ENT_QUOTES | ENT_SUBSTITUTE); |
760
|
1 |
|
} elseif (is_array($value)) { |
761
|
1 |
|
$d[$key] = static::htmlDecode($value, $valuesOnly); |
762
|
|
|
} else { |
763
|
1 |
|
$d[$key] = $value; |
764
|
|
|
} |
765
|
|
|
} |
766
|
|
|
|
767
|
1 |
|
return $d; |
768
|
|
|
} |
769
|
|
|
|
770
|
|
|
/** |
771
|
|
|
* Returns a value indicating whether the given array is an associative array. |
772
|
|
|
* |
773
|
|
|
* An array is associative if all its keys are strings. If `$allStrings` is false, |
774
|
|
|
* then an array will be treated as associative if at least one of its keys is a string. |
775
|
|
|
* |
776
|
|
|
* Note that an empty array will NOT be considered associative. |
777
|
|
|
* |
778
|
|
|
* @param array $array the array being checked |
779
|
|
|
* @param bool $allStrings whether the array keys must be all strings in order for |
780
|
|
|
* the array to be treated as associative. |
781
|
|
|
* @return bool whether the array is associative |
782
|
|
|
*/ |
783
|
322 |
|
public static function isAssociative($array, $allStrings = true) |
784
|
|
|
{ |
785
|
322 |
|
if (empty($array) || !is_array($array)) { |
786
|
184 |
|
return false; |
787
|
|
|
} |
788
|
|
|
|
789
|
160 |
|
if ($allStrings) { |
790
|
160 |
|
foreach ($array as $key => $value) { |
791
|
160 |
|
if (!is_string($key)) { |
792
|
12 |
|
return false; |
793
|
|
|
} |
794
|
|
|
} |
795
|
|
|
|
796
|
150 |
|
return true; |
797
|
|
|
} |
798
|
|
|
|
799
|
1 |
|
foreach ($array as $key => $value) { |
800
|
1 |
|
if (is_string($key)) { |
801
|
1 |
|
return true; |
802
|
|
|
} |
803
|
|
|
} |
804
|
|
|
|
805
|
1 |
|
return false; |
806
|
|
|
} |
807
|
|
|
|
808
|
|
|
/** |
809
|
|
|
* Returns a value indicating whether the given array is an indexed array. |
810
|
|
|
* |
811
|
|
|
* An array is indexed if all its keys are integers. If `$consecutive` is true, |
812
|
|
|
* then the array keys must be a consecutive sequence starting from 0. |
813
|
|
|
* |
814
|
|
|
* Note that an empty array will be considered indexed. |
815
|
|
|
* |
816
|
|
|
* @param array $array the array being checked |
817
|
|
|
* @param bool $consecutive whether the array keys must be a consecutive sequence |
818
|
|
|
* in order for the array to be treated as indexed. |
819
|
|
|
* @return bool whether the array is indexed |
820
|
|
|
*/ |
821
|
21 |
|
public static function isIndexed($array, $consecutive = false) |
822
|
|
|
{ |
823
|
21 |
|
if (!is_array($array)) { |
|
|
|
|
824
|
1 |
|
return false; |
825
|
|
|
} |
826
|
|
|
|
827
|
21 |
|
if (empty($array)) { |
828
|
3 |
|
return true; |
829
|
|
|
} |
830
|
|
|
|
831
|
21 |
|
$keys = array_keys($array); |
832
|
|
|
|
833
|
21 |
|
if ($consecutive) { |
834
|
1 |
|
return $keys === array_keys($keys); |
835
|
|
|
} |
836
|
|
|
|
837
|
21 |
|
foreach ($keys as $key) { |
838
|
21 |
|
if (!is_int($key)) { |
839
|
14 |
|
return false; |
840
|
|
|
} |
841
|
|
|
} |
842
|
|
|
|
843
|
9 |
|
return true; |
844
|
|
|
} |
845
|
|
|
|
846
|
|
|
/** |
847
|
|
|
* Check whether an array or [[Traversable]] contains an element. |
848
|
|
|
* |
849
|
|
|
* This method does the same as the PHP function [in_array()](https://www.php.net/manual/en/function.in-array.php) |
850
|
|
|
* but additionally works for objects that implement the [[Traversable]] interface. |
851
|
|
|
* |
852
|
|
|
* @param mixed $needle The value to look for. |
853
|
|
|
* @param iterable $haystack The set of values to search. |
854
|
|
|
* @param bool $strict Whether to enable strict (`===`) comparison. |
855
|
|
|
* @return bool `true` if `$needle` was found in `$haystack`, `false` otherwise. |
856
|
|
|
* @throws InvalidArgumentException if `$haystack` is neither traversable nor an array. |
857
|
|
|
* @see https://www.php.net/manual/en/function.in-array.php |
858
|
|
|
* @since 2.0.7 |
859
|
|
|
*/ |
860
|
20 |
|
public static function isIn($needle, $haystack, $strict = false) |
861
|
|
|
{ |
862
|
20 |
|
if (!static::isTraversable($haystack)) { |
863
|
1 |
|
throw new InvalidArgumentException('Argument $haystack must be an array or implement Traversable'); |
864
|
|
|
} |
865
|
|
|
|
866
|
19 |
|
if (is_array($haystack)) { |
867
|
19 |
|
return in_array($needle, $haystack, $strict); |
868
|
|
|
} |
869
|
|
|
|
870
|
4 |
|
foreach ($haystack as $value) { |
871
|
4 |
|
if ($strict ? $needle === $value : $needle == $value) { |
872
|
4 |
|
return true; |
873
|
|
|
} |
874
|
|
|
} |
875
|
|
|
|
876
|
3 |
|
return false; |
877
|
|
|
} |
878
|
|
|
|
879
|
|
|
/** |
880
|
|
|
* Checks whether a variable is an array or [[Traversable]]. |
881
|
|
|
* |
882
|
|
|
* This method does the same as the PHP function [is_array()](https://www.php.net/manual/en/function.is-array.php) |
883
|
|
|
* but additionally works on objects that implement the [[Traversable]] interface. |
884
|
|
|
* @param mixed $var The variable being evaluated. |
885
|
|
|
* @return bool whether $var can be traversed via foreach |
886
|
|
|
* @see https://www.php.net/manual/en/function.is-array.php |
887
|
|
|
* @since 2.0.8 |
888
|
|
|
*/ |
889
|
808 |
|
public static function isTraversable($var) |
890
|
|
|
{ |
891
|
808 |
|
return is_array($var) || $var instanceof Traversable; |
892
|
|
|
} |
893
|
|
|
|
894
|
|
|
/** |
895
|
|
|
* Checks whether an array or [[Traversable]] is a subset of another array or [[Traversable]]. |
896
|
|
|
* |
897
|
|
|
* This method will return `true`, if all elements of `$needles` are contained in |
898
|
|
|
* `$haystack`. If at least one element is missing, `false` will be returned. |
899
|
|
|
* |
900
|
|
|
* @param iterable $needles The values that must **all** be in `$haystack`. |
901
|
|
|
* @param iterable $haystack The set of value to search. |
902
|
|
|
* @param bool $strict Whether to enable strict (`===`) comparison. |
903
|
|
|
* @return bool `true` if `$needles` is a subset of `$haystack`, `false` otherwise. |
904
|
|
|
* @throws InvalidArgumentException if `$haystack` or `$needles` is neither traversable nor an array. |
905
|
|
|
* @since 2.0.7 |
906
|
|
|
*/ |
907
|
6 |
|
public static function isSubset($needles, $haystack, $strict = false) |
908
|
|
|
{ |
909
|
6 |
|
if (!static::isTraversable($needles)) { |
910
|
1 |
|
throw new InvalidArgumentException('Argument $needles must be an array or implement Traversable'); |
911
|
|
|
} |
912
|
|
|
|
913
|
5 |
|
foreach ($needles as $needle) { |
914
|
4 |
|
if (!static::isIn($needle, $haystack, $strict)) { |
915
|
3 |
|
return false; |
916
|
|
|
} |
917
|
|
|
} |
918
|
|
|
|
919
|
4 |
|
return true; |
920
|
|
|
} |
921
|
|
|
|
922
|
|
|
/** |
923
|
|
|
* Filters array according to rules specified. |
924
|
|
|
* |
925
|
|
|
* For example: |
926
|
|
|
* |
927
|
|
|
* ```php |
928
|
|
|
* $array = [ |
929
|
|
|
* 'A' => [1, 2], |
930
|
|
|
* 'B' => [ |
931
|
|
|
* 'C' => 1, |
932
|
|
|
* 'D' => 2, |
933
|
|
|
* ], |
934
|
|
|
* 'E' => 1, |
935
|
|
|
* ]; |
936
|
|
|
* |
937
|
|
|
* $result = \yii\helpers\ArrayHelper::filter($array, ['A']); |
938
|
|
|
* // $result will be: |
939
|
|
|
* // [ |
940
|
|
|
* // 'A' => [1, 2], |
941
|
|
|
* // ] |
942
|
|
|
* |
943
|
|
|
* $result = \yii\helpers\ArrayHelper::filter($array, ['A', 'B.C']); |
944
|
|
|
* // $result will be: |
945
|
|
|
* // [ |
946
|
|
|
* // 'A' => [1, 2], |
947
|
|
|
* // 'B' => ['C' => 1], |
948
|
|
|
* // ] |
949
|
|
|
* |
950
|
|
|
* $result = \yii\helpers\ArrayHelper::filter($array, ['B', '!B.C']); |
951
|
|
|
* // $result will be: |
952
|
|
|
* // [ |
953
|
|
|
* // 'B' => ['D' => 2], |
954
|
|
|
* // ] |
955
|
|
|
* ``` |
956
|
|
|
* |
957
|
|
|
* @param array $array Source array |
958
|
|
|
* @param iterable $filters Rules that define array keys which should be left or removed from results. |
959
|
|
|
* Each rule is: |
960
|
|
|
* - `var` - `$array['var']` will be left in result. |
961
|
|
|
* - `var.key` = only `$array['var']['key'] will be left in result. |
962
|
|
|
* - `!var.key` = `$array['var']['key'] will be removed from result. |
963
|
|
|
* @return array Filtered array |
964
|
|
|
* @since 2.0.9 |
965
|
|
|
*/ |
966
|
31 |
|
public static function filter($array, $filters) |
967
|
|
|
{ |
968
|
31 |
|
$result = []; |
969
|
31 |
|
$excludeFilters = []; |
970
|
|
|
|
971
|
31 |
|
foreach ($filters as $filter) { |
972
|
11 |
|
if (!is_string($filter) && !is_int($filter)) { |
973
|
1 |
|
continue; |
974
|
|
|
} |
975
|
|
|
|
976
|
10 |
|
if (is_string($filter) && strncmp($filter, '!', 1) === 0) { |
977
|
3 |
|
$excludeFilters[] = substr($filter, 1); |
978
|
3 |
|
continue; |
979
|
|
|
} |
980
|
|
|
|
981
|
10 |
|
$nodeValue = $array; //set $array as root node |
982
|
10 |
|
$keys = explode('.', (string) $filter); |
983
|
10 |
|
foreach ($keys as $key) { |
984
|
10 |
|
if (!array_key_exists($key, $nodeValue)) { |
985
|
8 |
|
continue 2; //Jump to next filter |
986
|
|
|
} |
987
|
10 |
|
$nodeValue = $nodeValue[$key]; |
988
|
|
|
} |
989
|
|
|
|
990
|
|
|
//We've found a value now let's insert it |
991
|
10 |
|
$resultNode = &$result; |
992
|
10 |
|
foreach ($keys as $key) { |
993
|
10 |
|
if (!array_key_exists($key, $resultNode)) { |
994
|
10 |
|
$resultNode[$key] = []; |
995
|
|
|
} |
996
|
10 |
|
$resultNode = &$resultNode[$key]; |
997
|
|
|
} |
998
|
10 |
|
$resultNode = $nodeValue; |
999
|
|
|
} |
1000
|
|
|
|
1001
|
31 |
|
foreach ($excludeFilters as $filter) { |
1002
|
3 |
|
$excludeNode = &$result; |
1003
|
3 |
|
$keys = explode('.', (string) $filter); |
1004
|
3 |
|
$numNestedKeys = count($keys) - 1; |
1005
|
3 |
|
foreach ($keys as $i => $key) { |
1006
|
3 |
|
if (!array_key_exists($key, $excludeNode)) { |
1007
|
1 |
|
continue 2; //Jump to next filter |
1008
|
|
|
} |
1009
|
|
|
|
1010
|
3 |
|
if ($i < $numNestedKeys) { |
1011
|
3 |
|
$excludeNode = &$excludeNode[$key]; |
1012
|
|
|
} else { |
1013
|
3 |
|
unset($excludeNode[$key]); |
1014
|
3 |
|
break; |
1015
|
|
|
} |
1016
|
|
|
} |
1017
|
|
|
} |
1018
|
|
|
|
1019
|
31 |
|
return $result; |
1020
|
|
|
} |
1021
|
|
|
|
1022
|
|
|
/** |
1023
|
|
|
* Sorts array recursively. |
1024
|
|
|
* |
1025
|
|
|
* @param array $array An array passing by reference. |
1026
|
|
|
* @param callable|null $sorter The array sorter. If omitted, sort index array by values, sort assoc array by keys. |
1027
|
|
|
* @return array |
1028
|
|
|
*/ |
1029
|
12 |
|
public static function recursiveSort(array &$array, $sorter = null) |
1030
|
|
|
{ |
1031
|
12 |
|
foreach ($array as &$value) { |
1032
|
12 |
|
if (is_array($value)) { |
1033
|
3 |
|
static::recursiveSort($value, $sorter); |
1034
|
|
|
} |
1035
|
|
|
} |
1036
|
12 |
|
unset($value); |
1037
|
|
|
|
1038
|
12 |
|
if ($sorter === null) { |
1039
|
12 |
|
$sorter = static::isIndexed($array) ? 'sort' : 'ksort'; |
1040
|
|
|
} |
1041
|
|
|
|
1042
|
12 |
|
call_user_func_array($sorter, [&$array]); |
1043
|
|
|
|
1044
|
12 |
|
return $array; |
1045
|
|
|
} |
1046
|
|
|
} |
1047
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths