1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dazzle\Util\Support; |
4
|
|
|
|
5
|
|
|
abstract class ArraySupport |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Check if given array is empty. |
9
|
|
|
* |
10
|
|
|
* @param array $array |
11
|
|
|
* @return bool |
12
|
|
|
*/ |
13
|
7 |
|
public static function isEmpty($array) |
14
|
|
|
{ |
15
|
7 |
|
return empty($array); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Check if given key exists in array with dot notation support. |
20
|
|
|
* |
21
|
|
|
* @param array $array |
22
|
|
|
* @param string $key |
23
|
|
|
* @return bool |
24
|
|
|
*/ |
25
|
5 |
|
public static function exists($array, $key) |
26
|
|
|
{ |
27
|
5 |
|
$key = static::normalizeKey($key); |
28
|
|
|
|
29
|
5 |
|
if ($key === null || $key === '' || static::isEmpty($array)) |
30
|
|
|
{ |
31
|
|
|
return false; |
32
|
|
|
} |
33
|
|
|
|
34
|
5 |
|
$keys = explode('.', $key); |
35
|
5 |
|
$currentElement = $array; |
36
|
|
|
|
37
|
5 |
View Code Duplication |
foreach ($keys as $currentKey) |
|
|
|
|
38
|
|
|
{ |
39
|
5 |
|
if (!is_array($currentElement) || !array_key_exists($currentKey, $currentElement)) |
40
|
|
|
{ |
41
|
2 |
|
return false; |
42
|
|
|
} |
43
|
|
|
|
44
|
3 |
|
$currentElement = $currentElement[(string) $currentKey]; |
45
|
|
|
} |
46
|
|
|
|
47
|
3 |
|
return true; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Return the value stored under given key in the array with dot notation support. |
52
|
|
|
* |
53
|
|
|
* @param string $key |
54
|
|
|
* @param array $array |
55
|
|
|
* @param mixed $default |
56
|
|
|
* @return mixed |
57
|
|
|
*/ |
58
|
9 |
|
public static function get($array, $key, $default = null) |
59
|
|
|
{ |
60
|
9 |
|
$key = static::normalizeKey($key); |
61
|
|
|
|
62
|
9 |
|
if ($key === null || $key === '') |
63
|
|
|
{ |
64
|
2 |
|
return $array; |
65
|
|
|
} |
66
|
|
|
|
67
|
7 |
|
$keys = explode('.', $key); |
68
|
7 |
|
$currentElement = $array; |
69
|
|
|
|
70
|
7 |
View Code Duplication |
foreach ($keys as $currentKey) |
|
|
|
|
71
|
|
|
{ |
72
|
7 |
|
if (!is_array($currentElement) || !array_key_exists($currentKey, $currentElement)) |
73
|
|
|
{ |
74
|
2 |
|
return $default; |
75
|
|
|
} |
76
|
|
|
|
77
|
5 |
|
$currentElement = $currentElement[(string) $currentKey]; |
78
|
|
|
} |
79
|
|
|
|
80
|
5 |
|
return $currentElement; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Set the value for given key in the array with dot notation support. |
85
|
|
|
* |
86
|
|
|
* @param array &$array |
87
|
|
|
* @param string $key |
88
|
|
|
* @param mixed $value |
89
|
|
|
* @return array |
90
|
|
|
*/ |
91
|
6 |
View Code Duplication |
public static function set(&$array, $key, $value) |
|
|
|
|
92
|
|
|
{ |
93
|
6 |
|
$key = static::normalizeKey($key); |
94
|
|
|
|
95
|
6 |
|
if ($key === null || $key === '') |
96
|
|
|
{ |
97
|
2 |
|
return ($array = $value); |
98
|
|
|
} |
99
|
|
|
|
100
|
4 |
|
$keys = explode('.', $key); |
101
|
4 |
|
$last = array_pop($keys); |
102
|
4 |
|
$currentElement =& $array; |
103
|
|
|
|
104
|
4 |
|
foreach ($keys as $currentKey) |
105
|
|
|
{ |
106
|
4 |
|
if (!array_key_exists($currentKey, $currentElement) || !is_array($currentElement[$currentKey])) |
107
|
|
|
{ |
108
|
2 |
|
$currentElement[$currentKey] = []; |
109
|
|
|
} |
110
|
|
|
|
111
|
4 |
|
$currentElement =& $currentElement[$currentKey]; |
112
|
|
|
} |
113
|
|
|
|
114
|
4 |
|
$currentElement[$last] = $value; |
115
|
|
|
|
116
|
4 |
|
return $array; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Remove the value stored under given key from the array with dot notation support. |
121
|
|
|
* |
122
|
|
|
* @param array &$array |
123
|
|
|
* @param string $key |
124
|
|
|
* @return bool |
125
|
|
|
*/ |
126
|
4 |
View Code Duplication |
public static function remove(&$array, $key) |
|
|
|
|
127
|
|
|
{ |
128
|
4 |
|
$key = static::normalizeKey($key); |
129
|
|
|
|
130
|
4 |
|
if ($key === null || $key === '') |
131
|
|
|
{ |
132
|
2 |
|
return ($array = []); |
133
|
|
|
} |
134
|
|
|
|
135
|
2 |
|
$keys = explode('.', $key); |
136
|
2 |
|
$last = array_pop($keys); |
137
|
2 |
|
$currentElement =& $array; |
138
|
|
|
|
139
|
2 |
|
foreach ($keys as $currentKey) |
140
|
|
|
{ |
141
|
2 |
|
if (!array_key_exists($currentKey, $currentElement) || !is_array($currentElement[$currentKey])) |
142
|
|
|
{ |
143
|
1 |
|
$currentElement[$currentKey] = []; |
144
|
|
|
} |
145
|
|
|
|
146
|
2 |
|
$currentElement =& $currentElement[$currentKey]; |
147
|
|
|
} |
148
|
|
|
|
149
|
2 |
|
unset($currentElement[$last]); |
150
|
|
|
|
151
|
2 |
|
return $array; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Flatten a multi-dimensional array into a single level using dot notation. |
156
|
|
|
* |
157
|
|
|
* @param array $array |
158
|
|
|
* @return array |
159
|
|
|
*/ |
160
|
3 |
|
public static function flatten($array) |
161
|
|
|
{ |
162
|
3 |
|
return static::flattenRecursive($array, ''); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Expand flattened array into a multi-dimensional one. |
167
|
|
|
* |
168
|
|
|
* @param $array |
169
|
|
|
* @return array |
170
|
|
|
*/ |
171
|
4 |
|
public static function expand($array) |
172
|
|
|
{ |
173
|
4 |
|
$multiArray = []; |
174
|
|
|
|
175
|
4 |
|
foreach ($array as $key=>&$value) |
176
|
|
|
{ |
177
|
3 |
|
$keys = explode('.', $key); |
178
|
3 |
|
$lastKey = array_pop($keys); |
179
|
3 |
|
$currentPointer = &$multiArray; |
180
|
|
|
|
181
|
3 |
|
foreach ($keys as $currentKey) |
182
|
|
|
{ |
183
|
3 |
|
if (!isset($currentPointer[$currentKey])) |
184
|
|
|
{ |
185
|
3 |
|
$currentPointer[$currentKey] = []; |
186
|
|
|
} |
187
|
|
|
|
188
|
3 |
|
$currentPointer = &$currentPointer[$currentKey]; |
189
|
|
|
} |
190
|
|
|
|
191
|
3 |
|
$currentPointer[$lastKey] = $value; |
192
|
|
|
} |
193
|
|
|
|
194
|
4 |
|
return $multiArray; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Merge several arrays, preserving dot notation. |
199
|
|
|
* |
200
|
|
|
* @param array[] $arrays |
201
|
|
|
* @return array |
202
|
|
|
*/ |
203
|
3 |
|
public static function merge($arrays) |
204
|
|
|
{ |
205
|
3 |
|
$merged = []; |
206
|
|
|
|
207
|
3 |
|
foreach ($arrays as $array) |
208
|
|
|
{ |
209
|
2 |
|
$merged = array_merge($merged, static::flatten($array)); |
210
|
|
|
} |
211
|
|
|
|
212
|
3 |
|
return static::expand($merged); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Merge several arrays. |
217
|
|
|
* |
218
|
|
|
* @param array[] $arrays |
219
|
|
|
* @return array |
220
|
|
|
*/ |
221
|
3 |
|
public static function replace($arrays) |
222
|
|
|
{ |
223
|
3 |
|
$merged = []; |
224
|
|
|
|
225
|
3 |
|
foreach ($arrays as $array) |
226
|
|
|
{ |
227
|
2 |
|
$merged = array_merge($merged, $array); |
228
|
|
|
} |
229
|
|
|
|
230
|
3 |
|
return $merged; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Normalize key to dot notation valid format. |
235
|
|
|
* |
236
|
|
|
* @param string $key |
237
|
|
|
* @return string |
238
|
|
|
*/ |
239
|
23 |
|
public static function normalizeKey($key) |
240
|
|
|
{ |
241
|
23 |
|
return ($key === null) ? null : trim( |
242
|
19 |
|
str_replace( |
243
|
19 |
|
[ " ", "\t", "\n", "\r", "\0", "\x0B" ], [ '', '', '', '', '', '' ], $key |
244
|
23 |
|
), '.' |
245
|
|
|
); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Flatten a single recursion of array. |
250
|
|
|
* |
251
|
|
|
* @param array $recursion |
252
|
|
|
* @param string $prefix |
253
|
|
|
* @return array |
254
|
|
|
*/ |
255
|
3 |
|
protected static function flattenRecursive(&$recursion, $prefix) |
256
|
|
|
{ |
257
|
3 |
|
$values = []; |
258
|
|
|
|
259
|
3 |
|
foreach ($recursion as $key=>&$value) |
260
|
|
|
{ |
261
|
3 |
|
if (is_array($value) && !empty($value)) |
262
|
|
|
{ |
263
|
3 |
|
$values = array_merge($values, static::flattenRecursive($value, $prefix . $key . '.')); |
264
|
|
|
} |
265
|
|
|
else |
266
|
|
|
{ |
267
|
3 |
|
$values[$prefix . $key] = $value; |
268
|
|
|
} |
269
|
|
|
} |
270
|
|
|
|
271
|
3 |
|
return $values; |
272
|
|
|
} |
273
|
|
|
} |
274
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.