1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Db\Helper; |
6
|
|
|
|
7
|
|
|
use ArrayAccess; |
8
|
|
|
use Closure; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Short implementation of ArrayHelper from Yii2 |
12
|
|
|
*/ |
13
|
|
|
class ArrayHelper |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Checks if the given array contains the specified key. |
17
|
|
|
* This method enhances the `array_key_exists()` function by supporting case-insensitive |
18
|
|
|
* key comparison. |
19
|
|
|
* |
20
|
|
|
* @param string $key the key to check |
21
|
|
|
* @param array|ArrayAccess $array the array with keys to check |
22
|
|
|
* @return bool whether the array contains the specified key |
23
|
|
|
*/ |
24
|
|
|
public static function keyExists(string $key, ArrayAccess|array $array): bool |
25
|
|
|
{ |
26
|
|
|
// Function `isset` checks key faster but skips `null`, `array_key_exists` handles this case |
27
|
|
|
// https://www.php.net/manual/en/function.array-key-exists.php#107786 |
28
|
|
|
if (is_array($array) && (isset($array[$key]) || array_key_exists($key, $array))) { |
29
|
|
|
return true; |
30
|
|
|
} |
31
|
|
|
// Cannot use `array_has_key` on Objects for PHP 7.4+, therefore we need to check using [[ArrayAccess::offsetExists()]] |
32
|
|
|
return $array instanceof ArrayAccess && $array->offsetExists($key); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Retrieves the value of an array element or object property with the given key or property name. |
37
|
|
|
* If the key does not exist in the array, the default value will be returned instead. |
38
|
|
|
* Not used when getting value from an object. |
39
|
|
|
* |
40
|
|
|
* The key may be specified in a dot format to retrieve the value of a sub-array or the property |
41
|
|
|
* of an embedded object. In particular, if the key is `x.y.z`, then the returned value would |
42
|
|
|
* be `$array['x']['y']['z']` or `$array->x->y->z` (if `$array` is an object). If `$array['x']` |
43
|
|
|
* or `$array->x` is neither an array nor an object, the default value will be returned. |
44
|
|
|
* Note that if the array already has an element `x.y.z`, then its value will be returned |
45
|
|
|
* instead of going through the sub-arrays. So it is better to be done specifying an array of key names |
46
|
|
|
* like `['x', 'y', 'z']`. |
47
|
|
|
* |
48
|
|
|
* Below are some usage examples, |
49
|
|
|
* |
50
|
|
|
* ```php |
51
|
|
|
* // working with array |
52
|
|
|
* $username = ArrayHelper::getValueByPath($_POST, 'username'); |
53
|
|
|
* // working with object |
54
|
|
|
* $username = ArrayHelper::getValueByPath($user, 'username'); |
55
|
|
|
* // working with anonymous function |
56
|
|
|
* $fullName = ArrayHelper::getValueByPath($user, function ($user, $defaultValue) { |
57
|
|
|
* return $user->firstName . ' ' . $user->lastName; |
58
|
|
|
* }); |
59
|
|
|
* // using dot format to retrieve the property of embedded object |
60
|
|
|
* $street = \yii\helpers\ArrayHelper::getValue($users, 'address.street'); |
61
|
|
|
* // using an array of keys to retrieve the value |
62
|
|
|
* $value = \yii\helpers\ArrayHelper::getValue($versions, ['1.0', 'date']); |
63
|
|
|
* ``` |
64
|
|
|
* |
65
|
|
|
* @param array|object $array array or object to extract value from |
66
|
|
|
* @param Closure|string $key key name of the array element, an array of keys or property name of the object, |
67
|
|
|
* or an anonymous function returning the value. The anonymous function signature should be: |
68
|
|
|
* `function($array, $defaultValue)`. |
69
|
|
|
* The possibility to pass an array of keys is available since version 2.0.4. |
70
|
|
|
* @param mixed|null $default the default value to be returned if the specified array key does not exist. Not used when |
71
|
|
|
* getting value from an object. |
72
|
|
|
* |
73
|
|
|
* @return mixed the value of the element if found, default value otherwise |
74
|
|
|
*/ |
75
|
|
|
public static function getValueByPath(object|array $array, Closure|string $key, mixed $default = null) |
76
|
|
|
{ |
77
|
|
|
if ($key instanceof Closure) { |
78
|
|
|
return $key($array, $default); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
if (is_array($key)) { |
|
|
|
|
82
|
|
|
$lastKey = array_pop($key); |
83
|
|
|
foreach ($key as $keyPart) { |
84
|
|
|
$array = static::getValueByPath($array, $keyPart); |
85
|
|
|
} |
86
|
|
|
$key = $lastKey ?? ''; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
if (is_object($array) && property_exists($array, $key)) { |
|
|
|
|
90
|
|
|
return $array->$key; |
91
|
|
|
} |
92
|
|
|
if (static::keyExists($key, $array)) { |
93
|
|
|
return $array[$key]; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
if ($key && ($pos = strrpos($key, '.')) !== false) { |
97
|
|
|
$array = static::getValueByPath($array, substr($key, 0, $pos), $default); |
98
|
|
|
$key = substr($key, $pos + 1); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
if (is_object($array)) { |
102
|
|
|
// this is expected to fail if the property does not exist, or __get() is not implemented |
103
|
|
|
// it is not reliably possible to check whether a property is accessible beforehand |
104
|
|
|
try { |
105
|
|
|
return $array->$key; |
106
|
|
|
} catch (\Exception $e) { |
|
|
|
|
107
|
|
|
if ($array instanceof ArrayAccess) { |
108
|
|
|
return $default; |
109
|
|
|
} |
110
|
|
|
throw $e; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
if (static::keyExists($key, $array)) { |
115
|
|
|
return $array[$key]; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return $default; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|