| 1 |
<?php |
| 2 |
|
| 3 |
declare (strict_types=1); |
| 4 |
namespace WCPOS\Vendor\Sentry\Util; |
| 5 |
|
| 6 |
/** |
| 7 |
* This class provides some utility methods to work with arrays. |
| 8 |
* |
| 9 |
* @internal |
| 10 |
*/ |
| 11 |
class Arr |
| 12 |
{ |
| 13 |
/** |
| 14 |
* Flatten a multi-dimensional associative array with dots except for keys that contain lists. |
| 15 |
* |
| 16 |
* This method is similar to Laravel's `Arr::dot()` method but does not flatten lists. |
| 17 |
* See: https://github.com/laravel/framework/blob/1bfad3020ec5d542ac7352c6fd0d388cbe29c46c/src/Illuminate/Collections/Arr.php#L163 |
| 18 |
* |
| 19 |
* @param array<string, mixed> $array |
| 20 |
* |
| 21 |
* @return array<string, mixed> |
| 22 |
*/ |
| 23 |
public static function simpleDot(array $array) : array |
| 24 |
{ |
| 25 |
$results = []; |
| 26 |
$flatten = static function ($data, $prefix = '') use(&$results, &$flatten) : void { |
| 27 |
foreach ($data as $key => $value) { |
| 28 |
$newKey = $prefix . $key; |
| 29 |
if (\is_array($value) && !empty($value) && !self::isList($value)) { |
| 30 |
$flatten($value, $newKey . '.'); |
| 31 |
} else { |
| 32 |
$results[$newKey] = $value; |
| 33 |
} |
| 34 |
} |
| 35 |
}; |
| 36 |
$flatten($array); |
| 37 |
return $results; |
| 38 |
} |
| 39 |
/** |
| 40 |
* Checks whether a given array is a list. |
| 41 |
* |
| 42 |
* `array_is_list` is introduced in PHP 8.1, so we have a polyfill for it. |
| 43 |
* |
| 44 |
* @see https://www.php.net/manual/en/function.array-is-list.php#126794 |
| 45 |
* |
| 46 |
* @param array<array-key, mixed> $array |
| 47 |
*/ |
| 48 |
public static function isList(array $array) : bool |
| 49 |
{ |
| 50 |
$i = 0; |
| 51 |
foreach ($array as $k => $v) { |
| 52 |
if ($k !== $i++) { |
| 53 |
return \false; |
| 54 |
} |
| 55 |
} |
| 56 |
return \true; |
| 57 |
} |
| 58 |
/** |
| 59 |
* Checks whether a given value is an associative array. |
| 60 |
* |
| 61 |
* @param mixed $value |
| 62 |
*/ |
| 63 |
public static function isAssociative($value) : bool |
| 64 |
{ |
| 65 |
return \is_array($value) && !self::isList($value); |
| 66 |
} |
| 67 |
} |
| 68 |
|