Passed
Push — master ( 5a4385...f15d7e )
by Alexander
05:05 queued 02:26
created

ArrayHelper::getRootValue()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 31
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 6.0163

Importance

Changes 0
Metric Value
eloc 13
c 0
b 0
f 0
dl 0
loc 31
ccs 12
cts 13
cp 0.9231
rs 9.2222
cc 6
nc 5
nop 3
crap 6.0163
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
                /** @var mixed $value */
79 4
                foreach ($object as $key => $value) {
80 4
                    if (is_array($value) || is_object($value)) {
81 4
                        $object[$key] = self::toArray($value, $properties);
82
                    }
83
                }
84
            }
85
86 5
            return $object;
87
        }
88
89 4
        if (is_object($object)) {
90 4
            if (!empty($properties)) {
91 1
                $className = $object::class;
92 1
                if (!empty($properties[$className])) {
93 1
                    $result = [];
94
                    /**
95
                     * @var int|string $key
96
                     * @var string $name
97
                     */
98 1
                    foreach ($properties[$className] as $key => $name) {
99 1
                        if (is_int($key)) {
100
                            /** @var mixed */
101 1
                            $result[$name] = $object->$name;
102
                        } else {
103
                            /** @var mixed */
104 1
                            $result[$key] = self::getValue($object, $name);
105
                        }
106
                    }
107
108 1
                    return $recursive ? self::toArray($result, $properties) : $result;
109
                }
110
            }
111 4
            if ($object instanceof ArrayableInterface) {
112 3
                $result = $object->toArray([], [], $recursive);
113
            } else {
114 4
                $result = [];
115
                /**
116
                 * @var string $key
117
                 * @var mixed $value
118
                 */
119 4
                foreach ($object as $key => $value) {
120
                    /** @var mixed */
121 4
                    $result[$key] = $value;
122
                }
123
            }
124
125 4
            return $recursive ? self::toArray($result, $properties) : $result;
126
        }
127
128 1
        return [$object];
129
    }
130
131
    /**
132
     * Merges two or more arrays into one recursively.
133
     * If each array has an element with the same string key value, the latter
134
     * will overwrite the former (different from {@see array_merge_recursive()}).
135
     * Recursive merging will be conducted if both arrays have an element of array
136
     * type and are having the same key.
137
     * For integer-keyed elements, the elements from the latter array will
138
     * be appended to the former array.
139
     *
140
     * @param array ...$arrays Arrays to be merged.
141
     *
142
     * @return array The merged array (the original arrays are not changed).
143
     */
144 4
    public static function merge(...$arrays): array
145
    {
146 4
        $result = array_shift($arrays) ?: [];
147 4
        while (!empty($arrays)) {
148
            /** @var mixed $value */
149 3
            foreach (array_shift($arrays) as $key => $value) {
150 3
                if (is_int($key)) {
151 3
                    if (array_key_exists($key, $result)) {
152 3
                        if ($result[$key] !== $value) {
153
                            /** @var mixed */
154 3
                            $result[] = $value;
155
                        }
156
                    } else {
157
                        /** @var mixed */
158 3
                        $result[$key] = $value;
159
                    }
160 1
                } elseif (isset($result[$key]) && is_array($value) && is_array($result[$key])) {
161 1
                    $result[$key] = self::merge($result[$key], $value);
162
                } else {
163
                    /** @var mixed */
164 1
                    $result[$key] = $value;
165
                }
166
            }
167
        }
168 4
        return $result;
169
    }
170
171
    /**
172
     * Retrieves the value of an array element or object property with the given key or property name.
173
     * If the key does not exist in the array or object, the default value will be returned instead.
174
     *
175
     * Below are some usage examples,
176
     *
177
     * ```php
178
     * // Working with array:
179
     * $username = \Yiisoft\Arrays\ArrayHelper::getValue($_POST, 'username');
180
     *
181
     * // Working with object:
182
     * $username = \Yiisoft\Arrays\ArrayHelper::getValue($user, 'username');
183
     *
184
     * // Working with anonymous function:
185
     * $fullName = \Yiisoft\Arrays\ArrayHelper::getValue($user, function ($user, $defaultValue) {
186
     *     return $user->firstName . ' ' . $user->lastName;
187
     * });
188
     *
189
     * // Using an array of keys to retrieve the value:
190
     * $value = \Yiisoft\Arrays\ArrayHelper::getValue($versions, ['1.0', 'date']);
191
     * ```
192
     *
193
     * @param array|object $array Array or object to extract value from.
194
     * @param array|Closure|float|int|string $key Key name of the array element,
195
     * an array of keys, object property name, object method like `getName()`, or an anonymous function
196
     * returning the value. The anonymous function signature should be:
197
     * `function($array, $defaultValue)`.
198
     * @param mixed $default The default value to be returned if the specified array key does not exist. Not used when
199
     * getting value from an object.
200
     *
201
     * @psalm-param ArrayKey|Closure $key
202
     *
203
     * @return mixed The value of the element if found, default value otherwise.
204
     */
205 95
    public static function getValue(
206
        array|object $array,
207
        array|Closure|float|int|string $key,
208
        mixed $default = null
209
    ): mixed {
210 95
        if ($key instanceof Closure) {
0 ignored issues
show
introduced by
$key is never a sub-type of Closure.
Loading history...
211 16
            return $key($array, $default);
212
        }
213
214 87
        if (is_array($key)) {
0 ignored issues
show
introduced by
The condition is_array($key) is always true.
Loading history...
215
            /** @psalm-var array<mixed,string|int> $key */
216 42
            $lastKey = array_pop($key);
217 42
            foreach ($key as $keyPart) {
218
                /** @var mixed */
219 39
                $array = self::getRootValue($array, $keyPart, null);
220 39
                if (!is_array($array) && !is_object($array)) {
221 10
                    return $default;
222
                }
223
            }
224 33
            return self::getRootValue($array, $lastKey, $default);
225
        }
226
227 47
        return self::getRootValue($array, $key, $default);
228
    }
229
230
    /**
231
     * @param mixed $array Array or object to extract value from, otherwise method will return $default.
232
     * @param float|int|string $key Key name of the array element, object property name or object method like `getValue()`.
233
     * @param mixed $default The default value to be returned if the specified array key does not exist. Not used when
234
     * getting value from an object.
235
     *
236
     * @return mixed The value of the element if found, default value otherwise.
237
     */
238 114
    private static function getRootValue(mixed $array, float|int|string $key, mixed $default): mixed
239
    {
240 114
        if (is_array($array)) {
241 101
            $key = self::normalizeArrayKey($key);
242 101
            return array_key_exists($key, $array) ? $array[$key] : $default;
243
        }
244
245 15
        if (is_object($array)) {
246 15
            $key = (string) $key;
247
248 15
            if (str_ends_with($key, '()')) {
249 1
                $method = substr($key, 0, -2);
250
                /** @psalm-suppress MixedMethodCall */
251 1
                return $array->$method();
252
            }
253
254
            try {
255
                /** @psalm-suppress MixedPropertyFetch */
256 14
                return $array::$$key;
257 14
            } catch (Throwable) {
0 ignored issues
show
Unused Code introduced by
catch (\Throwable) is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
258
                /**
259
                 * This is expected to fail if the property does not exist, or __get() is not implemented.
260
                 * It is not reliably possible to check whether a property is accessible beforehand.
261
                 *
262
                 * @psalm-suppress MixedPropertyFetch
263
                 */
264 14
                return $array->$key;
265
            }
266
        }
267
268
        return $default;
269
    }
270
271
    /**
272
     * Retrieves the value of an array element or object property with the given key or property name.
273
     * If the key does not exist in the array or object, the default value will be returned instead.
274
     *
275
     * The key may be specified in a dot-separated format to retrieve the value of a sub-array or the property
276
     * of an embedded object. In particular, if the key is `x.y.z`, then the returned value would
277
     * be `$array['x']['y']['z']` or `$array->x->y->z` (if `$array` is an object). If `$array['x']`
278
     * or `$array->x` is neither an array nor an object, the default value will be returned.
279
     * Note that if the array already has an element `x.y.z`, then its value will be returned
280
     * instead of going through the sub-arrays. So it is better to be done specifying an array of key names
281
     * like `['x', 'y', 'z']`.
282
     *
283
     * Below are some usage examples,
284
     *
285
     * ```php
286
     * // Using separated format to retrieve the property of embedded object:
287
     * $street = \Yiisoft\Arrays\ArrayHelper::getValue($users, 'address.street');
288
     *
289
     * // Using an array of keys to retrieve the value:
290
     * $value = \Yiisoft\Arrays\ArrayHelper::getValue($versions, ['1.0', 'date']);
291
     * ```
292
     *
293
     * @param array|object $array Array or object to extract value from.
294
     * @param array|Closure|float|int|string $path Key name of the array element, an array of keys or property name
295
     * of the object, or an anonymous function returning the value. The anonymous function signature should be:
296
     * `function($array, $defaultValue)`.
297
     * @param mixed $default The default value to be returned if the specified array key does not exist. Not used when
298
     * getting value from an object.
299
     * @param string $delimiter A separator, used to parse string $key for embedded object property retrieving. Defaults
300
     * to "." (dot).
301
     *
302
     * @psalm-param ArrayPath|Closure $path
303
     *
304
     * @return mixed The value of the element if found, default value otherwise.
305
     */
306 37
    public static function getValueByPath(
307
        array|object $array,
308
        array|Closure|float|int|string $path,
309
        mixed $default = null,
310
        string $delimiter = '.'
311
    ): mixed {
312 37
        return self::getValue(
313 37
            $array,
314 37
            $path instanceof Closure ? $path : self::parseMixedPath($path, $delimiter),
0 ignored issues
show
introduced by
$path is never a sub-type of Closure.
Loading history...
315 37
            $default
316 37
        );
317
    }
318
319
    /**
320
     * Writes a value into an associative array at the key path specified.
321
     * If there is no such key path yet, it will be created recursively.
322
     * If the key exists, it will be overwritten.
323
     *
324
     * ```php
325
     *  $array = [
326
     *      'key' => [
327
     *          'in' => [
328
     *              'val1',
329
     *              'key' => 'val'
330
     *          ]
331
     *      ]
332
     *  ];
333
     * ```
334
     *
335
     * The result of `ArrayHelper::setValue($array, ['key', 'in'], ['arr' => 'val']);`
336
     * will be the following:
337
     *
338
     * ```php
339
     *  [
340
     *      'key' => [
341
     *          'in' => [
342
     *              'arr' => 'val'
343
     *          ]
344
     *      ]
345
     *  ]
346
     * ```
347
     *
348
     * @param array $array The array to write the value to.
349
     * @param array|float|int|string|null $key The path of where do you want to write a value to `$array`
350
     * the path can be described by an array of keys. If the path is null then `$array` will be assigned the `$value`.
351
     *
352
     * @psalm-param ArrayKey|null $key
353
     *
354
     * @param mixed $value The value to be written.
355
     */
356 29
    public static function setValue(array &$array, array|float|int|string|null $key, mixed $value): void
357
    {
358 29
        if ($key === null) {
0 ignored issues
show
introduced by
The condition $key === null is always false.
Loading history...
359
            /** @var mixed */
360 2
            $array = $value;
361 2
            return;
362
        }
363
364 27
        $keys = is_array($key) ? $key : [$key];
0 ignored issues
show
introduced by
The condition is_array($key) is always true.
Loading history...
365
366 27
        while (count($keys) > 1) {
367 15
            $k = self::normalizeArrayKey(array_shift($keys));
368 15
            if (!isset($array[$k])) {
369 8
                $array[$k] = [];
370
            }
371 15
            if (!is_array($array[$k])) {
372 2
                $array[$k] = [$array[$k]];
373
            }
374 15
            $array = &$array[$k];
375
        }
376
377
        /** @var mixed */
378 27
        $array[self::normalizeArrayKey(array_shift($keys))] = $value;
379
    }
380
381
    /**
382
     * Writes a value into an associative array at the key path specified.
383
     * If there is no such key path yet, it will be created recursively.
384
     * If the key exists, it will be overwritten.
385
     *
386
     * ```php
387
     *  $array = [
388
     *      'key' => [
389
     *          'in' => [
390
     *              'val1',
391
     *              'key' => 'val'
392
     *          ]
393
     *      ]
394
     *  ];
395
     * ```
396
     *
397
     * The result of `ArrayHelper::setValue($array, 'key.in.0', ['arr' => 'val']);` will be the following:
398
     *
399
     * ```php
400
     *  [
401
     *      'key' => [
402
     *          'in' => [
403
     *              ['arr' => 'val'],
404
     *              'key' => 'val'
405
     *          ]
406
     *      ]
407
     *  ]
408
     *
409
     * ```
410
     *
411
     * The result of
412
     * `ArrayHelper::setValue($array, 'key.in', ['arr' => 'val']);` or
413
     * `ArrayHelper::setValue($array, ['key', 'in'], ['arr' => 'val']);`
414
     * will be the following:
415
     *
416
     * ```php
417
     *  [
418
     *      'key' => [
419
     *          'in' => [
420
     *              'arr' => 'val'
421
     *          ]
422
     *      ]
423
     *  ]
424
     * ```
425
     *
426
     * @param array $array The array to write the value to.
427
     * @param array|float|int|string|null $path The path of where do you want to write a value to `$array`.
428
     * The path can be described by a string when each key should be separated by a dot.
429
     * You can also describe the path as an array of keys. If the path is null then `$array` will be assigned
430
     * the `$value`.
431
     * @param mixed $value The value to be written.
432
     * @param string $delimiter A separator, used to parse string $key for embedded object property retrieving. Defaults
433
     * to "." (dot).
434
     *
435
     * @psalm-param ArrayPath|null $path
436
     */
437 21
    public static function setValueByPath(
438
        array &$array,
439
        array|float|int|string|null $path,
440
        mixed $value,
441
        string $delimiter = '.'
442
    ): void {
443 21
        self::setValue($array, $path === null ? null : self::parseMixedPath($path, $delimiter), $value);
0 ignored issues
show
introduced by
The condition $path === null is always false.
Loading history...
444
    }
445
446
    /**
447
     * Removes an item from an array and returns the value. If the key does not exist in the array, the default value
448
     * will be returned instead.
449
     *
450
     * Usage examples,
451
     *
452
     * ```php
453
     * // $array = ['type' => 'A', 'options' => [1, 2]];
454
     *
455
     * // Working with array:
456
     * $type = \Yiisoft\Arrays\ArrayHelper::remove($array, 'type');
457
     *
458
     * // $array content
459
     * // $array = ['options' => [1, 2]];
460
     * ```
461
     *
462
     * @param array $array The array to extract value from.
463
     * @param array|float|int|string $key Key name of the array element or associative array at the key path specified.
464
     * @param mixed $default The default value to be returned if the specified key does not exist.
465
     *
466
     * @psalm-param ArrayKey $key
467
     *
468
     * @return mixed The value of the element if found, default value otherwise.
469
     */
470 13
    public static function remove(array &$array, array|float|int|string $key, mixed $default = null): mixed
471
    {
472 13
        $keys = is_array($key) ? $key : [$key];
0 ignored issues
show
introduced by
The condition is_array($key) is always true.
Loading history...
473
474 13
        while (count($keys) > 1) {
475 7
            $key = self::normalizeArrayKey(array_shift($keys));
476 7
            if (!isset($array[$key]) || !is_array($array[$key])) {
477 1
                return $default;
478
            }
479 6
            $array = &$array[$key];
480
        }
481
482 12
        $key = self::normalizeArrayKey(array_shift($keys));
483 12
        if (array_key_exists($key, $array)) {
484
            /** @var mixed */
485 11
            $value = $array[$key];
486 11
            unset($array[$key]);
487 11
            return $value;
488
        }
489
490 1
        return $default;
491
    }
492
493
    /**
494
     * Removes an item from an array and returns the value. If the key does not exist in the array, the default value
495
     * will be returned instead.
496
     *
497
     * Usage examples,
498
     *
499
     * ```php
500
     * // $array = ['type' => 'A', 'options' => [1, 2]];
501
     *
502
     * // Working with array:
503
     * $type = \Yiisoft\Arrays\ArrayHelper::remove($array, 'type');
504
     *
505
     * // $array content
506
     * // $array = ['options' => [1, 2]];
507
     * ```
508
     *
509
     * @param array $array The array to extract value from.
510
     * @param array|float|int|string $path Key name of the array element or associative array at the key path specified.
511
     * The path can be described by a string when each key should be separated by a delimiter (default is dot).
512
     * @param mixed $default The default value to be returned if the specified key does not exist.
513
     * @param string $delimiter A separator, used to parse string $key for embedded object property retrieving. Defaults
514
     * to "." (dot).
515
     *
516
     * @psalm-param ArrayPath $path
517
     *
518
     * @return mixed The value of the element if found, default value otherwise.
519
     */
520 5
    public static function removeByPath(
521
        array &$array,
522
        array|float|int|string $path,
523
        mixed $default = null,
524
        string $delimiter = '.'
525
    ): mixed {
526 5
        return self::remove($array, self::parseMixedPath($path, $delimiter), $default);
527
    }
528
529
    /**
530
     * Removes items with matching values from the array and returns the removed items.
531
     *
532
     * Example,
533
     *
534
     * ```php
535
     * $array = ['Bob' => 'Dylan', 'Michael' => 'Jackson', 'Mick' => 'Jagger', 'Janet' => 'Jackson'];
536
     * $removed = \Yiisoft\Arrays\ArrayHelper::removeValue($array, 'Jackson');
537
     * // result:
538
     * // $array = ['Bob' => 'Dylan', 'Mick' => 'Jagger'];
539
     * // $removed = ['Michael' => 'Jackson', 'Janet' => 'Jackson'];
540
     * ```
541
     *
542
     * @param array $array The array where to look the value from.
543
     * @param mixed $value The value to remove from the array.
544
     *
545
     * @return array The items that were removed from the array.
546
     */
547 2
    public static function removeValue(array &$array, mixed $value): array
548
    {
549 2
        $result = [];
550
        /** @psalm-var mixed $val */
551 2
        foreach ($array as $key => $val) {
552 2
            if ($val === $value) {
553
                /** @var mixed */
554 1
                $result[$key] = $val;
555 1
                unset($array[$key]);
556
            }
557
        }
558
559 2
        return $result;
560
    }
561
562
    /**
563
     * Indexes and/or groups the array according to a specified key.
564
     * The input should be either multidimensional array or an array of objects.
565
     *
566
     * The `$key` can be either a key name of the sub-array, a property name of object, or an anonymous
567
     * function that must return the value that will be used as a key.
568
     *
569
     * `$groups` is an array of keys, that will be used to group the input array into one or more sub-arrays based
570
     * on keys specified.
571
     *
572
     * If the `$key` is specified as `null` or a value of an element corresponding to the key is `null` in addition
573
     * to `$groups` not specified then the element is discarded.
574
     *
575
     * For example:
576
     *
577
     * ```php
578
     * $array = [
579
     *     ['id' => '123', 'data' => 'abc', 'device' => 'laptop'],
580
     *     ['id' => '345', 'data' => 'def', 'device' => 'tablet'],
581
     *     ['id' => '345', 'data' => 'hgi', 'device' => 'smartphone'],
582
     * ];
583
     * $result = ArrayHelper::index($array, 'id');
584
     * ```
585
     *
586
     * The result will be an associative array, where the key is the value of `id` attribute
587
     *
588
     * ```php
589
     * [
590
     *     '123' => ['id' => '123', 'data' => 'abc', 'device' => 'laptop'],
591
     *     '345' => ['id' => '345', 'data' => 'hgi', 'device' => 'smartphone']
592
     *     // The second element of an original array is overwritten by the last element because of the same id
593
     * ]
594
     * ```
595
     *
596
     * An anonymous function can be used in the grouping array as well.
597
     *
598
     * ```php
599
     * $result = ArrayHelper::index($array, function ($element) {
600
     *     return $element['id'];
601
     * });
602
     * ```
603
     *
604
     * Passing `id` as a third argument will group `$array` by `id`:
605
     *
606
     * ```php
607
     * $result = ArrayHelper::index($array, null, 'id');
608
     * ```
609
     *
610
     * The result will be a multidimensional array grouped by `id` on the first level, by `device` on the second level
611
     * and indexed by `data` on the third level:
612
     *
613
     * ```php
614
     * [
615
     *     '123' => [
616
     *         ['id' => '123', 'data' => 'abc', 'device' => 'laptop']
617
     *     ],
618
     *     '345' => [ // all elements with this index are present in the result array
619
     *         ['id' => '345', 'data' => 'def', 'device' => 'tablet'],
620
     *         ['id' => '345', 'data' => 'hgi', 'device' => 'smartphone'],
621
     *     ]
622
     * ]
623
     * ```
624
     *
625
     * The anonymous function can be used in the array of grouping keys as well:
626
     *
627
     * ```php
628
     * $result = ArrayHelper::index($array, 'data', [function ($element) {
629
     *     return $element['id'];
630
     * }, 'device']);
631
     * ```
632
     *
633
     * The result will be a multidimensional array grouped by `id` on the first level, by the `device` on the second one
634
     * and indexed by the `data` on the third level:
635
     *
636
     * ```php
637
     * [
638
     *     '123' => [
639
     *         'laptop' => [
640
     *             'abc' => ['id' => '123', 'data' => 'abc', 'device' => 'laptop']
641
     *         ]
642
     *     ],
643
     *     '345' => [
644
     *         'tablet' => [
645
     *             'def' => ['id' => '345', 'data' => 'def', 'device' => 'tablet']
646
     *         ],
647
     *         'smartphone' => [
648
     *             'hgi' => ['id' => '345', 'data' => 'hgi', 'device' => 'smartphone']
649
     *         ]
650
     *     ]
651
     * ]
652
     * ```
653
     *
654
     * @param iterable $array The array or iterable object that needs to be indexed or grouped.
655
     * @param Closure|string|null $key The column name or anonymous function which result will be used
656
     * to index the array.
657
     * @param Closure[]|string|string[]|null $groups The array of keys, that will be used to group the input
658
     * array by one or more keys. If the `$key` attribute or its value for the particular element is null and `$groups`
659
     * is not defined, the array element will be discarded. Otherwise, if `$groups` is specified, array element will be
660
     * added to the result array without any key.
661
     *
662
     * @psalm-param iterable<mixed, array|object> $array
663
     *
664
     * @return array The indexed and/or grouped array.
665
     */
666 24
    public static function index(
667
        iterable $array,
668
        Closure|string|null $key,
669
        array|string|null $groups = []
670
    ): array {
671 24
        $result = [];
672 24
        $groups = (array)$groups;
673
674
        /** @var mixed $element */
675 24
        foreach ($array as $element) {
676 24
            if (!is_array($element) && !is_object($element)) {
677 8
                throw new InvalidArgumentException(
678 8
                    'index() can not get value from ' . gettype($element) .
679 8
                    '. The $array should be either multidimensional array or an array of objects.'
680 8
                );
681
            }
682
683 20
            $lastArray = &$result;
684
685 20
            foreach ($groups as $group) {
686 9
                $value = self::normalizeArrayKey(
687 9
                    self::getValue($element, $group)
688 9
                );
689 9
                if (!array_key_exists($value, $lastArray)) {
690 9
                    $lastArray[$value] = [];
691
                }
692
                /** @psalm-suppress MixedAssignment */
693 9
                $lastArray = &$lastArray[$value];
694
                /** @var array $lastArray */
695
            }
696
697 20
            if ($key === null) {
698 7
                if (!empty($groups)) {
699 7
                    $lastArray[] = $element;
700
                }
701
            } else {
702
                /** @var mixed */
703 13
                $value = self::getValue($element, $key);
704 13
                if ($value !== null) {
705 12
                    $lastArray[self::normalizeArrayKey($value)] = $element;
706
                }
707
            }
708 20
            unset($lastArray);
709
        }
710
711 16
        return $result;
712
    }
713
714
    /**
715
     * Groups the array according to a specified key.
716
     * This is just an alias for indexing by groups
717
     *
718
     * @param iterable $array The array or iterable object that needs to be grouped.
719
     * @param Closure[]|string|string[] $groups The array of keys, that will be used to group the input array
720
     * by one or more keys.
721
     *
722
     * @psalm-param iterable<mixed, array|object> $array
723
     *
724
     * @return array The grouped array.
725
     */
726 1
    public static function group(iterable $array, array|string $groups): array
727
    {
728 1
        return self::index($array, null, $groups);
729
    }
730
731
    /**
732
     * Returns the values of a specified column in an array.
733
     * The input array should be multidimensional or an array of objects.
734
     *
735
     * For example,
736
     *
737
     * ```php
738
     * $array = [
739
     *     ['id' => '123', 'data' => 'abc'],
740
     *     ['id' => '345', 'data' => 'def'],
741
     * ];
742
     * $result = ArrayHelper::getColumn($array, 'id');
743
     * // the result is: ['123', '345']
744
     *
745
     * // using anonymous function
746
     * $result = ArrayHelper::getColumn($array, function ($element) {
747
     *     return $element['id'];
748
     * });
749
     * ```
750
     *
751
     * @param iterable $array The array or iterable object to get column from.
752
     * @param Closure|string $name Column name or a closure returning column name.
753
     * @param bool $keepKeys Whether to maintain the array keys. If false, the resulting array
754
     * will be re-indexed with integers.
755
     *
756
     * @psalm-param iterable<array-key, array|object> $array
757
     *
758
     * @return array The list of column values.
759
     */
760 8
    public static function getColumn(iterable $array, Closure|string $name, bool $keepKeys = true): array
761
    {
762 8
        $result = [];
763 8
        if ($keepKeys) {
764 6
            foreach ($array as $k => $element) {
765
                /** @var mixed */
766 6
                $result[$k] = self::getValue($element, $name);
767
            }
768
        } else {
769 2
            foreach ($array as $element) {
770
                /** @var mixed */
771 2
                $result[] = self::getValue($element, $name);
772
            }
773
        }
774
775 8
        return $result;
776
    }
777
778
    /**
779
     * Builds a map (key-value pairs) from a multidimensional array or an array of objects.
780
     * The `$from` and `$to` parameters specify the key names or property names to set up the map.
781
     * Optionally, one can further group the map according to a grouping field `$group`.
782
     *
783
     * For example,
784
     *
785
     * ```php
786
     * $array = [
787
     *     ['id' => '123', 'name' => 'aaa', 'class' => 'x'],
788
     *     ['id' => '124', 'name' => 'bbb', 'class' => 'x'],
789
     *     ['id' => '345', 'name' => 'ccc', 'class' => 'y'],
790
     * ];
791
     *
792
     * $result = ArrayHelper::map($array, 'id', 'name');
793
     * // the result is:
794
     * // [
795
     * //     '123' => 'aaa',
796
     * //     '124' => 'bbb',
797
     * //     '345' => 'ccc',
798
     * // ]
799
     *
800
     * $result = ArrayHelper::map($array, 'id', 'name', 'class');
801
     * // the result is:
802
     * // [
803
     * //     'x' => [
804
     * //         '123' => 'aaa',
805
     * //         '124' => 'bbb',
806
     * //     ],
807
     * //     'y' => [
808
     * //         '345' => 'ccc',
809
     * //     ],
810
     * // ]
811
     * ```
812
     *
813
     * @param iterable $array Array or iterable object to build map from.
814
     * @param Closure|string $from Key or property name to map from.
815
     * @param Closure|string $to Key or property name to map to.
816
     * @param Closure|string|null $group Key or property to group the map.
817
     *
818
     * @psalm-param iterable<mixed, array|object> $array
819
     *
820
     * @return array Resulting map.
821
     */
822 9
    public static function map(
823
        iterable $array,
824
        Closure|string $from,
825
        Closure|string $to,
826
        Closure|string|null $group = null
827
    ): array {
828 9
        if ($group === null) {
829 4
            if ($from instanceof Closure || $to instanceof Closure || !is_array($array)) {
830 4
                $result = [];
831 4
                foreach ($array as $element) {
832 4
                    $key = (string)self::getValue($element, $from);
833
                    /** @var mixed */
834 4
                    $result[$key] = self::getValue($element, $to);
835
                }
836
837 4
                return $result;
838
            }
839
840 2
            return array_column($array, $to, $from);
841
        }
842
843 5
        $result = [];
844 5
        foreach ($array as $element) {
845 5
            $groupKey = (string)self::getValue($element, $group);
846 5
            $key = (string)self::getValue($element, $from);
847
            /** @var mixed */
848 5
            $result[$groupKey][$key] = self::getValue($element, $to);
849
        }
850
851 5
        return $result;
852
    }
853
854
    /**
855
     * Checks if the given array contains the specified key.
856
     * This method enhances the `array_key_exists()` function by supporting case-insensitive
857
     * key comparison.
858
     *
859
     * @param array $array The array with keys to check.
860
     * @param array|float|int|string $key The key to check.
861
     * @param bool $caseSensitive Whether the key comparison should be case-sensitive.
862
     *
863
     * @psalm-param ArrayKey $key
864
     *
865
     * @return bool Whether the array contains the specified key.
866
     */
867 41
    public static function keyExists(array $array, array|float|int|string $key, bool $caseSensitive = true): bool
868
    {
869 41
        if (is_array($key)) {
0 ignored issues
show
introduced by
The condition is_array($key) is always true.
Loading history...
870 31
            if (count($key) === 1) {
871 25
                return self::rootKeyExists($array, end($key), $caseSensitive);
872
            }
873
874 27
            foreach (self::getExistsKeys($array, array_shift($key), $caseSensitive) as $existKey) {
875
                /** @var mixed */
876 27
                $array = self::getRootValue($array, $existKey, null);
877 27
                if (is_array($array) && self::keyExists($array, $key, $caseSensitive)) {
878 14
                    return true;
879
                }
880
            }
881
882 13
            return false;
883
        }
884
885 10
        return self::rootKeyExists($array, $key, $caseSensitive);
886
    }
887
888 35
    private static function rootKeyExists(array $array, float|int|string $key, bool $caseSensitive): bool
889
    {
890 35
        $key = (string)$key;
891
892 35
        if ($caseSensitive) {
893 29
            return array_key_exists($key, $array);
894
        }
895
896 6
        foreach (array_keys($array) as $k) {
897 6
            if (strcasecmp($key, (string)$k) === 0) {
898 5
                return true;
899
            }
900
        }
901
902 1
        return false;
903
    }
904
905
    /**
906
     * @return array<int, array-key>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<int, array-key> at position 4 could not be parsed: Unknown type name 'array-key' at position 4 in array<int, array-key>.
Loading history...
907
     */
908 27
    private static function getExistsKeys(array $array, float|int|string $key, bool $caseSensitive): array
909
    {
910 27
        $key = (string)$key;
911
912 27
        if ($caseSensitive) {
913 22
            return [$key];
914
        }
915
916 5
        return array_filter(
917 5
            array_keys($array),
918 5
            static fn ($k) => strcasecmp($key, (string)$k) === 0
919 5
        );
920
    }
921
922
    /**
923
     * Checks if the given array contains the specified key. The key may be specified in a dot format.
924
     * In particular, if the key is `x.y.z`, then key would be `$array['x']['y']['z']`.
925
     *
926
     * This method enhances the `array_key_exists()` function by supporting case-insensitive
927
     * key comparison.
928
     *
929
     * @param array $array The array to check path in.
930
     * @param array|float|int|string $path The key path. Can be described by a string when each key should be separated
931
     * by delimiter. You can also describe the path as an array of keys.
932
     * @param bool $caseSensitive Whether the key comparison should be case-sensitive.
933
     * @param string $delimiter A separator, used to parse string $key for embedded object property retrieving. Defaults
934
     * to "." (dot).
935
     *
936
     * @psalm-param ArrayPath $path
937
     */
938 26
    public static function pathExists(
939
        array $array,
940
        array|float|int|string $path,
941
        bool $caseSensitive = true,
942
        string $delimiter = '.'
943
    ): bool {
944 26
        return self::keyExists($array, self::parseMixedPath($path, $delimiter), $caseSensitive);
945
    }
946
947
    /**
948
     * Encodes special characters in an array of strings into HTML entities.
949
     * Only array values will be encoded by default.
950
     * If a value is an array, this method will also encode it recursively.
951
     * Only string values will be encoded.
952
     *
953
     * @param iterable $data Data to be encoded.
954
     * @param bool $valuesOnly Whether to encode array values only. If false,
955
     * both the array keys and array values will be encoded.
956
     * @param string|null $encoding The encoding to use, defaults to `ini_get('default_charset')`.
957
     *
958
     * @psalm-param iterable<mixed, mixed> $data
959
     *
960
     * @return array The encoded data.
961
     *
962
     * @link https://www.php.net/manual/en/function.htmlspecialchars.php
963
     */
964 2
    public static function htmlEncode(iterable $data, bool $valuesOnly = true, string $encoding = null): array
965
    {
966 2
        $d = [];
967
        /**
968
         * @var mixed $key
969
         * @var mixed $value
970
         */
971 2
        foreach ($data as $key => $value) {
972 2
            if (!is_int($key)) {
973 2
                $key = (string)$key;
974
            }
975 2
            if (!$valuesOnly && is_string($key)) {
976 1
                $key = htmlspecialchars($key, ENT_QUOTES | ENT_SUBSTITUTE, $encoding, true);
977
            }
978 2
            if (is_string($value)) {
979 2
                $d[$key] = htmlspecialchars($value, ENT_QUOTES | ENT_SUBSTITUTE, $encoding, true);
980 2
            } elseif (is_array($value)) {
981 2
                $d[$key] = self::htmlEncode($value, $valuesOnly, $encoding);
982
            } else {
983
                /** @var mixed */
984 2
                $d[$key] = $value;
985
            }
986
        }
987
988 2
        return $d;
989
    }
990
991
    /**
992
     * Decodes HTML entities into the corresponding characters in an array of strings.
993
     * Only array values will be decoded by default.
994
     * If a value is an array, this method will also decode it recursively.
995
     * Only string values will be decoded.
996
     *
997
     * @param iterable $data Data to be decoded.
998
     * @param bool $valuesOnly Whether to decode array values only. If false,
999
     * both the array keys and array values will be decoded.
1000
     *
1001
     * @psalm-param iterable<mixed, mixed> $data
1002
     *
1003
     * @return array The decoded data.
1004
     *
1005
     * @link https://www.php.net/manual/en/function.htmlspecialchars-decode.php
1006
     */
1007 2
    public static function htmlDecode(iterable $data, bool $valuesOnly = true): array
1008
    {
1009 2
        $decoded = [];
1010
        /**
1011
         * @var mixed $key
1012
         * @var mixed $value
1013
         */
1014 2
        foreach ($data as $key => $value) {
1015 2
            if (!is_int($key)) {
1016 2
                $key = (string)$key;
1017
            }
1018 2
            if (!$valuesOnly && is_string($key)) {
1019 1
                $key = htmlspecialchars_decode($key, ENT_QUOTES);
1020
            }
1021 2
            if (is_string($value)) {
1022 2
                $decoded[$key] = htmlspecialchars_decode($value, ENT_QUOTES);
1023 2
            } elseif (is_array($value)) {
1024 2
                $decoded[$key] = self::htmlDecode($value);
1025
            } else {
1026
                /** @var mixed */
1027 2
                $decoded[$key] = $value;
1028
            }
1029
        }
1030
1031 2
        return $decoded;
1032
    }
1033
1034
    /**
1035
     * Returns a value indicating whether the given array is an associative array.
1036
     *
1037
     * An array is associative if all its keys are strings. If `$allStrings` is false,
1038
     * then an array will be treated as associative if at least one of its keys is a string.
1039
     *
1040
     * Note that an empty array will NOT be considered associative.
1041
     *
1042
     * @param array $array The array being checked.
1043
     * @param bool $allStrings Whether the array keys must be all strings in order for
1044
     * the array to be treated as associative.
1045
     *
1046
     * @return bool Whether the array is associative.
1047
     */
1048 1
    public static function isAssociative(array $array, bool $allStrings = true): bool
1049
    {
1050 1
        if ($array === []) {
1051 1
            return false;
1052
        }
1053
1054 1
        if ($allStrings) {
1055 1
            foreach ($array as $key => $_value) {
1056 1
                if (!is_string($key)) {
1057 1
                    return false;
1058
                }
1059
            }
1060
1061 1
            return true;
1062
        }
1063
1064 1
        foreach ($array as $key => $_value) {
1065 1
            if (is_string($key)) {
1066 1
                return true;
1067
            }
1068
        }
1069
1070 1
        return false;
1071
    }
1072
1073
    /**
1074
     * Returns a value indicating whether the given array is an indexed array.
1075
     *
1076
     * An array is indexed if all its keys are integers. If `$consecutive` is true,
1077
     * then the array keys must be a consecutive sequence starting from 0.
1078
     *
1079
     * Note that an empty array will be considered indexed.
1080
     *
1081
     * @param array $array The array being checked.
1082
     * @param bool $consecutive Whether the array keys must be a consecutive sequence
1083
     * in order for the array to be treated as indexed.
1084
     *
1085
     * @return bool Whether the array is indexed.
1086
     */
1087 1
    public static function isIndexed(array $array, bool $consecutive = false): bool
1088
    {
1089 1
        if ($array === []) {
1090 1
            return true;
1091
        }
1092
1093 1
        if ($consecutive) {
1094 1
            return array_keys($array) === range(0, count($array) - 1);
1095
        }
1096
1097
        /** @psalm-var mixed $value */
1098 1
        foreach ($array as $key => $_value) {
1099 1
            if (!is_int($key)) {
1100 1
                return false;
1101
            }
1102
        }
1103
1104 1
        return true;
1105
    }
1106
1107
    /**
1108
     * Check whether an array or `\Traversable` contains an element.
1109
     *
1110
     * This method does the same as the PHP function {@see in_array()}
1111
     * but additionally works for objects that implement the {@see \Traversable} interface.
1112
     *
1113
     * @param mixed $needle The value to look for.
1114
     * @param iterable $haystack The set of values to search.
1115
     * @param bool $strict Whether to enable strict (`===`) comparison.
1116
     *
1117
     * @throws InvalidArgumentException if `$haystack` is neither traversable nor an array.
1118
     *
1119
     * @return bool `true` if `$needle` was found in `$haystack`, `false` otherwise.
1120
     *
1121
     * @link https://php.net/manual/en/function.in-array.php
1122
     */
1123 3
    public static function isIn(mixed $needle, iterable $haystack, bool $strict = false): bool
1124
    {
1125 3
        if (is_array($haystack)) {
1126 3
            return in_array($needle, $haystack, $strict);
1127
        }
1128
1129
        /** @psalm-var mixed $value */
1130 3
        foreach ($haystack as $value) {
1131 3
            if ($needle == $value && (!$strict || $needle === $value)) {
1132 3
                return true;
1133
            }
1134
        }
1135
1136 3
        return false;
1137
    }
1138
1139
    /**
1140
     * Checks whether an array or {@see \Traversable} is a subset of another array or {@see \Traversable}.
1141
     *
1142
     * This method will return `true`, if all elements of `$needles` are contained in
1143
     * `$haystack`. If at least one element is missing, `false` will be returned.
1144
     *
1145
     * @param iterable $needles The values that must **all** be in `$haystack`.
1146
     * @param iterable $haystack The set of value to search.
1147
     * @param bool $strict Whether to enable strict (`===`) comparison.
1148
     *
1149
     * @throws InvalidArgumentException if `$haystack` or `$needles` is neither traversable nor an array.
1150
     *
1151
     * @return bool `true` if `$needles` is a subset of `$haystack`, `false` otherwise.
1152
     */
1153 1
    public static function isSubset(iterable $needles, iterable $haystack, bool $strict = false): bool
1154
    {
1155
        /** @psalm-var mixed $needle */
1156 1
        foreach ($needles as $needle) {
1157 1
            if (!self::isIn($needle, $haystack, $strict)) {
1158 1
                return false;
1159
            }
1160
        }
1161
1162 1
        return true;
1163
    }
1164
1165
    /**
1166
     * Filters array according to rules specified.
1167
     *
1168
     * For example:
1169
     *
1170
     * ```php
1171
     * $array = [
1172
     *     'A' => [1, 2],
1173
     *     'B' => [
1174
     *         'C' => 1,
1175
     *         'D' => 2,
1176
     *     ],
1177
     *     'E' => 1,
1178
     * ];
1179
     *
1180
     * $result = \Yiisoft\Arrays\ArrayHelper::filter($array, ['A']);
1181
     * // $result will be:
1182
     * // [
1183
     * //     'A' => [1, 2],
1184
     * // ]
1185
     *
1186
     * $result = \Yiisoft\Arrays\ArrayHelper::filter($array, ['A', 'B.C']);
1187
     * // $result will be:
1188
     * // [
1189
     * //     'A' => [1, 2],
1190
     * //     'B' => ['C' => 1],
1191
     * // ]
1192
     *
1193
     * $result = \Yiisoft\Arrays\ArrayHelper::filter($array, ['B', '!B.C']);
1194
     * // $result will be:
1195
     * // [
1196
     * //     'B' => ['D' => 2],
1197
     * // ]
1198
     * ```
1199
     *
1200
     * @param array $array Source array.
1201
     * @param list<string> $filters Rules that define array keys which should be left or removed from results.
0 ignored issues
show
Bug introduced by
The type Yiisoft\Arrays\list was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
1202
     * Each rule is:
1203
     * - `var` - `$array['var']` will be left in result.
1204
     * - `var.key` = only `$array['var']['key']` will be left in result.
1205
     * - `!var.key` = `$array['var']['key']` will be removed from result.
1206
     *
1207
     * @return array Filtered array.
1208
     */
1209 17
    public static function filter(array $array, array $filters): array
1210
    {
1211 17
        $result = [];
1212 17
        $excludeFilters = [];
1213
1214 17
        foreach ($filters as $filter) {
1215 17
            if ($filter[0] === '!') {
1216 6
                $excludeFilters[] = substr($filter, 1);
1217 6
                continue;
1218
            }
1219
1220 17
            $nodeValue = $array; // Set $array as root node.
1221 17
            $keys = explode('.', $filter);
1222 17
            foreach ($keys as $key) {
1223 17
                if (!is_array($nodeValue) || !array_key_exists($key, $nodeValue)) {
1224 4
                    continue 2; // Jump to next filter.
1225
                }
1226
                /** @var mixed */
1227 15
                $nodeValue = $nodeValue[$key];
1228
            }
1229
1230
            // We've found a value now let's insert it.
1231 13
            $resultNode = &$result;
1232 13
            foreach ($keys as $key) {
1233 13
                if (!array_key_exists($key, $resultNode)) {
1234 13
                    $resultNode[$key] = [];
1235
                }
1236
                /** @psalm-suppress MixedAssignment */
1237 13
                $resultNode = &$resultNode[$key];
1238
                /** @var array $resultNode */
1239
            }
1240
            /** @var array */
1241 13
            $resultNode = $nodeValue;
1242
        }
1243
1244
        /**
1245
         * @psalm-suppress UnnecessaryVarAnnotation
1246
         *
1247
         * @var array $result
1248
         */
1249
1250 17
        foreach ($excludeFilters as $filter) {
1251 6
            $excludeNode = &$result;
1252 6
            $keys = explode('.', $filter);
1253 6
            $numNestedKeys = count($keys) - 1;
1254 6
            foreach ($keys as $i => $key) {
1255 6
                if (!is_array($excludeNode) || !array_key_exists($key, $excludeNode)) {
1256 2
                    continue 2; // Jump to next filter.
1257
                }
1258
1259 5
                if ($i < $numNestedKeys) {
1260
                    /** @psalm-suppress MixedAssignment */
1261 5
                    $excludeNode = &$excludeNode[$key];
1262
                } else {
1263 4
                    unset($excludeNode[$key]);
1264 4
                    break;
1265
                }
1266
            }
1267
        }
1268
1269
        /** @var array $result */
1270
1271 17
        return $result;
1272
    }
1273
1274
    /**
1275
     * Returns the public member variables of an object.
1276
     *
1277
     * This method is provided such that we can get the public member variables of an object, because a direct call of
1278
     * {@see get_object_vars()} (within the object itself) will return only private and protected variables.
1279
     *
1280
     * @param object $object The object to be handled.
1281
     *
1282
     * @return array|null The public member variables of the object or null if not object given.
1283
     *
1284
     * @link https://www.php.net/manual/en/function.get-object-vars.php
1285
     */
1286 4
    public static function getObjectVars(object $object): ?array
1287
    {
1288 4
        return get_object_vars($object);
1289
    }
1290
1291 142
    private static function normalizeArrayKey(mixed $key): string
1292
    {
1293 142
        return is_float($key) ? NumericHelper::normalize($key) : (string)$key;
1294
    }
1295
1296
    /**
1297
     * @psalm-param ArrayPath $path
1298
     *
1299
     * @psalm-return ArrayKey
1300
     */
1301 86
    private static function parseMixedPath(array|float|int|string $path, string $delimiter): array|float|int|string
1302
    {
1303 86
        if (is_array($path)) {
0 ignored issues
show
introduced by
The condition is_array($path) is always true.
Loading history...
1304 17
            $newPath = [];
1305 17
            foreach ($path as $key) {
1306 17
                if (is_string($key)) {
1307 17
                    $parsedPath = StringHelper::parsePath($key, $delimiter);
1308 17
                    $newPath = array_merge($newPath, $parsedPath);
1309 17
                    continue;
1310
                }
1311
1312 8
                if (is_array($key)) {
1313
                    /** @var list<float|int|string> $parsedPath */
1314 4
                    $parsedPath = self::parseMixedPath($key, $delimiter);
1315 4
                    $newPath = array_merge($newPath, $parsedPath);
0 ignored issues
show
Bug introduced by
$parsedPath of type Yiisoft\Arrays\list is incompatible with the type array expected by parameter $arrays of array_merge(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

1315
                    $newPath = array_merge($newPath, /** @scrutinizer ignore-type */ $parsedPath);
Loading history...
1316 4
                    continue;
1317
                }
1318
1319 4
                $newPath[] = $key;
1320
            }
1321 17
            return $newPath;
1322
        }
1323
1324 70
        return is_string($path) ? StringHelper::parsePath($path, $delimiter) : $path;
1325
    }
1326
}
1327