1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace alkemann\h2l\util; |
4
|
|
|
|
5
|
|
|
use OutOfBoundsException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class Util |
9
|
|
|
* |
10
|
|
|
* @package alkemann\h2l |
11
|
|
|
*/ |
12
|
|
|
class ArrayManipulations |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Look for a deeo value in a nested data array. |
16
|
|
|
* |
17
|
|
|
* Given $data = ['one' => ['two' => ['three' => 55]], 'four' => []]; |
18
|
|
|
* |
19
|
|
|
* ```php |
20
|
|
|
* getFromArrayByKey('one.two.three', $data) -> 55 |
21
|
|
|
* getFromArrayByKey('one|two', $data, '|') -> ['three' => 55] |
22
|
|
|
* getFromArrayByKey('four.five', $data) -> throws OutOfBoundsException |
23
|
|
|
* ``` |
24
|
|
|
* |
25
|
|
|
* @param string $key |
26
|
|
|
* @param array $data |
27
|
|
|
* @param string $delimiter |
28
|
|
|
* @return mixed|null |
29
|
|
|
*/ |
30
|
|
|
public static function getFromArrayByKey(string $key, array $data, string $delimiter = '.') |
31
|
|
|
{ |
32
|
|
|
$keys = explode($delimiter, $key); |
33
|
|
|
try { |
34
|
|
|
return self::getArrayValueByKeys($keys, $data); |
35
|
|
|
} catch (\OutOfBoundsException $e) { |
36
|
|
|
return null; |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Look for a deep value in a data array. |
42
|
|
|
* |
43
|
|
|
* Given $data = ['one' => ['two' => ['three' => 55]], 'four' => []]; |
44
|
|
|
* |
45
|
|
|
* ```php |
46
|
|
|
* getArrayValueByKeys(['one','two','three'], $data) will return 55 |
47
|
|
|
* getArrayValueByKeys(['four','five'], $data) will throw OutOfBoundsException |
48
|
|
|
* ``` |
49
|
|
|
* |
50
|
|
|
* @param mixed $keys |
51
|
|
|
* @param mixed $data passed by reference |
52
|
|
|
* @return mixed |
53
|
|
|
* @throws OutOfBoundsException if the key does not exist in data |
54
|
|
|
*/ |
55
|
|
|
public static function getArrayValueByKeys(array $keys, &$data) |
56
|
|
|
{ |
57
|
|
|
$key = array_shift($keys); |
58
|
|
|
if (!is_array($data) || empty($key)) { |
59
|
|
|
return $data; |
60
|
|
|
} |
61
|
|
|
if (array_key_exists($key, $data) === false) { |
62
|
|
|
throw new OutOfBoundsException("Key [{$key}." . join('.', $keys) . "] not set in " . print_r($data, 1)); |
63
|
|
|
} |
64
|
|
|
if (empty($keys)) { |
65
|
|
|
return $data[$key]; |
66
|
|
|
} else { |
67
|
|
|
return self::getArrayValueByKeys($keys, $data[$key]); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Update or Insert a value in a nested array by "dot" notation string |
73
|
|
|
* |
74
|
|
|
* Given $data = ['one' => ['two' => ['three' => 55]], 'four' => []]; |
75
|
|
|
* |
76
|
|
|
* ```php |
77
|
|
|
* setToArrayByKey('one.two.three', 42, $data) // will replace 55 with 42 |
78
|
|
|
* setToArrayByKey('one.two.five', 42, $data) // will add a second key in the 'two' array |
79
|
|
|
* ``` |
80
|
|
|
* |
81
|
|
|
* @param string $key |
82
|
|
|
* @param mixed $value the value to assign |
83
|
|
|
* @param array $data The array to update |
84
|
|
|
* @param string $delimiter defaults to `.` |
85
|
|
|
*/ |
86
|
|
|
public static function setToArrayByKey(string $key, $value, array &$data, string $delimiter = '.') |
87
|
|
|
{ |
88
|
|
|
$keys = explode($delimiter, $key); |
89
|
|
|
return self::setArrayValueByKeys($keys, $value, $data); |
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Update or Insert value in a nested array |
94
|
|
|
* |
95
|
|
|
* @param array $keys |
96
|
|
|
* @param mixed $value |
97
|
|
|
* @param mixed $data passed by reference |
98
|
|
|
*/ |
99
|
|
|
public static function setArrayValueByKeys(array $keys, $value, &$data) |
100
|
|
|
{ |
101
|
|
|
$key = array_shift($keys); |
102
|
|
|
if (empty($keys)) { |
103
|
|
|
$data[$key] = $value; |
104
|
|
|
} else { |
105
|
|
|
if (array_key_exists($key, $data) === false) { |
106
|
|
|
$data[$key] = []; |
107
|
|
|
} |
108
|
|
|
self::setArrayValueByKeys($keys, $value, $data[$key]); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()
can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.