1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace __\Traits; |
4
|
|
|
|
5
|
|
|
trait Arrays |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Append item to array |
9
|
|
|
* |
10
|
|
|
* @usage __::append([1, 2, 3], 4); |
11
|
|
|
* >> [1, 2, 3, 4] |
12
|
|
|
* |
13
|
|
|
* @param array $array original array |
14
|
|
|
* @param mixed $value new item or value to append |
15
|
|
|
* |
16
|
|
|
* @return array |
17
|
|
|
*/ |
18
|
1 |
|
public static function append(array $array = [], $value = null): array |
19
|
|
|
{ |
20
|
1 |
|
$array[] = $value; |
21
|
|
|
|
22
|
1 |
|
return $array; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Creates an array with all falsey values removed. The values false, null, 0, "", undefined, and NaN are all |
27
|
|
|
* falsey. |
28
|
|
|
* |
29
|
|
|
* @usage __::compact([0, 1, false, 2, '', 3]); |
30
|
|
|
* >> [1, 2, 3] |
31
|
|
|
* |
32
|
|
|
* @param array $array array to compact |
33
|
|
|
* |
34
|
|
|
* @return array |
35
|
|
|
*/ |
36
|
3 |
|
public static function compact(array $array = []): array |
37
|
|
|
{ |
38
|
3 |
|
$result = []; |
39
|
|
|
|
40
|
3 |
|
foreach ($array as $value) { |
41
|
3 |
|
if ($value) { |
42
|
3 |
|
$result[] = $value; |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
3 |
|
return $result; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* base flatten |
52
|
|
|
* |
53
|
|
|
* @param array $array |
54
|
|
|
* @param bool $shallow |
55
|
|
|
* @param bool $strict |
56
|
|
|
* @param int $startIndex |
57
|
|
|
* |
58
|
|
|
* @return array |
59
|
|
|
*/ |
60
|
1 |
|
public static function baseFlatten( |
61
|
|
|
array $array, |
62
|
|
|
bool $shallow = false, |
63
|
|
|
bool $strict = true, |
64
|
|
|
int $startIndex = 0 |
|
|
|
|
65
|
|
|
): array { |
66
|
1 |
|
$idx = 0; |
67
|
1 |
|
$output = []; |
68
|
|
|
|
69
|
1 |
|
foreach ($array as $index => $value) { |
70
|
1 |
|
if (is_array($value)) { |
71
|
1 |
|
if (!$shallow) { |
72
|
1 |
|
$value = static::baseFlatten($value, $shallow, $strict); |
73
|
|
|
} |
74
|
1 |
|
$j = 0; |
75
|
1 |
|
$len = count($value); |
76
|
1 |
|
while ($j < $len) { |
77
|
1 |
|
$output[$idx++] = $value[$j++]; |
78
|
|
|
} |
79
|
|
|
} else { |
80
|
1 |
|
if (!$strict) { |
81
|
1 |
|
$output[$idx++] = $value; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
1 |
|
return $output; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Flattens a multidimensional array. If you pass shallow, the array will only be flattened a single level. |
91
|
|
|
* |
92
|
|
|
* @usage __::flatten([1, 2, [3, [4]]], [flatten]); |
93
|
|
|
* >> [1, 2, 3, 4] |
94
|
|
|
* |
95
|
|
|
* @param array $array |
96
|
|
|
* @param bool $shallow |
97
|
|
|
* |
98
|
|
|
* @return array |
99
|
|
|
*/ |
100
|
1 |
|
public static function flatten(array $array, bool $shallow = false): array |
101
|
|
|
{ |
102
|
1 |
|
return static::baseFlatten($array, $shallow, false); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Patches array by xpath. |
107
|
|
|
* |
108
|
|
|
* @usage __::patch( |
109
|
|
|
* ['addr' => ['country' => 'US', 'zip' => 12345]], |
110
|
|
|
* ['/addr/country' => 'CA','/addr/zip' => 54321] |
111
|
|
|
* ); |
112
|
|
|
** >> ['addr' => ['country' => 'CA', 'zip' => 54321]] |
113
|
|
|
* |
114
|
|
|
* @param array $array The array to patch |
115
|
|
|
* @param array $patches List of new xpath-value pairs |
116
|
|
|
* @param string $parent |
117
|
|
|
* |
118
|
|
|
* @return array Returns patched array |
119
|
|
|
*/ |
120
|
1 |
|
public static function patch(array $array, array $patches, string $parent = ''): array |
121
|
|
|
{ |
122
|
1 |
|
foreach ($array as $key => $value) { |
123
|
1 |
|
$z = $parent . '/' . $key; |
124
|
|
|
|
125
|
1 |
|
if (isset($patches[$z])) { |
126
|
1 |
|
$array[$key] = $patches[$z]; |
127
|
1 |
|
unset($patches[$z]); |
128
|
|
|
|
129
|
1 |
|
if (!count($patches)) { |
130
|
1 |
|
break; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
1 |
|
if (is_array($value)) { |
135
|
1 |
|
$array[$key] = static::patch($value, $patches, $z); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
1 |
|
return $array; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Prepend item or value to an array |
144
|
|
|
* |
145
|
|
|
* @usage __::prepend([1, 2, 3], 4); |
146
|
|
|
* >> [4, 1, 2, 3] |
147
|
|
|
* |
148
|
|
|
* @param array $array |
149
|
|
|
* @param mixed $value |
150
|
|
|
* |
151
|
|
|
* @return array |
152
|
|
|
*/ |
153
|
2 |
|
public static function prepend(array $array = [], $value = null): array |
154
|
|
|
{ |
155
|
2 |
|
array_unshift($array, $value); |
156
|
|
|
|
157
|
2 |
|
return $array; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Generate range of values based on start , end and step |
162
|
|
|
* |
163
|
|
|
* @usage __::range(1, 10, 2); |
164
|
|
|
** >> [1, 3, 5, 7, 9] |
165
|
|
|
* |
166
|
|
|
* @param int|null $start range start |
167
|
|
|
* @param int|null $stop range end |
168
|
|
|
* @param int $step range step value |
169
|
|
|
* |
170
|
|
|
* @return array range of values |
171
|
|
|
*/ |
172
|
1 |
|
public static function range($start = null, $stop = null, int $step = 1): array |
173
|
|
|
{ |
174
|
1 |
|
if ($stop == null && $start != null) { |
|
|
|
|
175
|
1 |
|
$stop = $start; |
176
|
1 |
|
$start = 1; |
177
|
|
|
} |
178
|
|
|
|
179
|
1 |
|
return range($start, $stop, $step); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Generate array of repeated values |
184
|
|
|
* |
185
|
|
|
* @usage __::repeat('foo', 3); |
186
|
|
|
** >> ['foo', 'foo', 'foo'] |
187
|
|
|
* |
188
|
|
|
* @param string $object The object to repeat. |
189
|
|
|
* @param null $times ow many times has to be repeated. |
|
|
|
|
190
|
|
|
* |
191
|
|
|
* @return array Returns a new array of filled values. |
192
|
|
|
* |
193
|
|
|
*/ |
194
|
1 |
|
public static function repeat(string $object = '', $times = null): array |
195
|
|
|
{ |
196
|
1 |
|
$times = abs($times); |
197
|
1 |
|
if ($times == null) { |
|
|
|
|
198
|
1 |
|
return []; |
199
|
|
|
} |
200
|
|
|
|
201
|
1 |
|
return array_fill(0, $times, $object); |
|
|
|
|
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Creates an array of elements split into groups the length of size. If array can't be split evenly, the final |
206
|
|
|
* chunk will be the remaining elements. |
207
|
|
|
* |
208
|
|
|
* @usage __::chunk([1, 2, 3, 4, 5], 3); |
209
|
|
|
* >> [[1, 2, 3], [4, 5]] |
210
|
|
|
* |
211
|
|
|
* @param array $array original array |
212
|
|
|
* @param int $size the chunk size |
213
|
|
|
* @param bool $preserveKeys When set to TRUE keys will be preserved. Default is FALSE which will reindex the |
214
|
|
|
* chunk numerically |
215
|
|
|
* |
216
|
|
|
* @return array |
217
|
|
|
*/ |
218
|
1 |
|
public static function chunk(array $array, int $size = 1, bool $preserveKeys = false): array |
219
|
|
|
{ |
220
|
1 |
|
return array_chunk($array, $size, $preserveKeys); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Creates a slice of array with n elements dropped from the beginning. |
225
|
|
|
* |
226
|
|
|
* @usage __::drop([0, 1, 3], 2); |
227
|
|
|
* >> [3] |
228
|
|
|
* |
229
|
|
|
* @param array $input The array to query. |
230
|
|
|
* @param int $number The number of elements to drop. |
231
|
|
|
* |
232
|
|
|
* @return array |
233
|
|
|
*/ |
234
|
1 |
|
public static function drop(array $input, int $number = 1): array |
235
|
|
|
{ |
236
|
1 |
|
return array_slice($input, $number); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Shuffle an array ensuring no item remains in the same position. |
241
|
|
|
* |
242
|
|
|
* @usage __::randomize([1, 2, 3]); |
243
|
|
|
* >> [2, 3, 1] |
244
|
|
|
* |
245
|
|
|
* @param array $array original array |
246
|
|
|
* |
247
|
|
|
* @return array |
248
|
|
|
*/ |
249
|
1 |
|
public static function randomize(array $array): array |
250
|
|
|
{ |
251
|
1 |
|
for ($i = 0, $c = count($array); $i < $c - 1; $i++) { |
252
|
1 |
|
$j = rand($i + 1, $c - 1); |
253
|
1 |
|
list($array[$i], $array[$j]) = [$array[$j], $array[$i]]; |
254
|
|
|
} |
255
|
|
|
|
256
|
1 |
|
return $array; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* Search for the index of a value in an array. |
261
|
|
|
* |
262
|
|
|
* @usage __::search(['a', 'b', 'c'], 'b') |
263
|
|
|
* >> 1 |
264
|
|
|
* |
265
|
|
|
* @param array $array |
266
|
|
|
* @param string $value |
267
|
|
|
* |
268
|
|
|
* @return mixed |
269
|
|
|
*/ |
270
|
1 |
|
public static function search(array $array, string $value) |
271
|
|
|
{ |
272
|
1 |
|
return array_search($value, $array, true); |
273
|
|
|
} |
274
|
|
|
} |
275
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.