1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Db\Helper; |
6
|
|
|
|
7
|
|
|
use Closure; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Short implementation of ArrayHelper from Yii2 |
11
|
|
|
*/ |
12
|
|
|
class ArrayHelper |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Retrieves the value of an array element or object property with the given key or property name. |
16
|
|
|
* If the key does not exist in the array, the default value will be returned instead. |
17
|
|
|
* Not used when getting value from an object. |
18
|
|
|
* |
19
|
|
|
* The key may be specified in a dot format to retrieve the value of a sub-array or the property |
20
|
|
|
* of an embedded object. In particular, if the key is `x.y.z`, then the returned value would |
21
|
|
|
* be `$array['x']['y']['z']` or `$array->x->y->z` (if `$array` is an object). If `$array['x']` |
22
|
|
|
* or `$array->x` is neither an array nor an object, the default value will be returned. |
23
|
|
|
* Note that if the array already has an element `x.y.z`, then its value will be returned |
24
|
|
|
* instead of going through the sub-arrays. So it is better to be done specifying an array of key names |
25
|
|
|
* like `['x', 'y', 'z']`. |
26
|
|
|
* |
27
|
|
|
* Below are some usage examples, |
28
|
|
|
* |
29
|
|
|
* ```php |
30
|
|
|
* // working with array |
31
|
|
|
* $username = ArrayHelper::getValueByPath($_POST, 'username'); |
32
|
|
|
* // working with object |
33
|
|
|
* $username = ArrayHelper::getValueByPath($user, 'username'); |
34
|
|
|
* // working with anonymous function |
35
|
|
|
* $fullName = ArrayHelper::getValueByPath($user, function ($user, $defaultValue) { |
36
|
|
|
* return $user->firstName . ' ' . $user->lastName; |
37
|
|
|
* }); |
38
|
|
|
* // using dot format to retrieve the property of embedded object |
39
|
|
|
* $street = \yii\helpers\ArrayHelper::getValue($users, 'address.street'); |
40
|
|
|
* // using an array of keys to retrieve the value |
41
|
|
|
* $value = \yii\helpers\ArrayHelper::getValue($versions, ['1.0', 'date']); |
42
|
|
|
* ``` |
43
|
|
|
* |
44
|
|
|
* @param array|object $array array or object to extract value from |
45
|
|
|
* @param Closure|string $key key name of the array element, an array of keys or property name of the object, |
46
|
|
|
* or an anonymous function returning the value. The anonymous function signature should be: |
47
|
|
|
* `function($array, $defaultValue)`. |
48
|
|
|
* The possibility to pass an array of keys is available since version 2.0.4. |
49
|
|
|
* @param mixed|null $default the default value to be returned if the specified array key does not exist. Not used when |
50
|
|
|
* getting value from an object. |
51
|
|
|
* |
52
|
|
|
* @return mixed the value of the element if found, default value otherwise |
53
|
|
|
*/ |
54
|
|
|
public static function getValueByPath(object|array $array, Closure|string $key, mixed $default = null) |
55
|
|
|
{ |
56
|
|
|
if ($key instanceof Closure) { |
57
|
|
|
return $key($array, $default); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if (is_object($array) && property_exists($array, $key)) { |
|
|
|
|
61
|
|
|
return $array->$key; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if (is_array($array) && array_key_exists($key, $array)) { |
65
|
|
|
return $array[$key]; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
if ($key && ($pos = strrpos($key, '.')) !== false) { |
69
|
|
|
/** @psalm-var array<string, mixed>|object $array */ |
70
|
|
|
$array = static::getValueByPath($array, substr($key, 0, $pos), $default); |
71
|
|
|
$key = substr($key, $pos + 1); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
if (is_object($array)) { |
75
|
|
|
// this is expected to fail if the property does not exist, or __get() is not implemented |
76
|
|
|
// it is not reliably possible to check whether a property is accessible beforehand |
77
|
|
|
try { |
78
|
|
|
return $array->$key; |
79
|
|
|
} catch (\Exception $e) { |
|
|
|
|
80
|
|
|
throw $e; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
if (array_key_exists($key, $array)) { |
85
|
|
|
return $array[$key]; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $default; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|