|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Baka\Support; |
|
4
|
|
|
|
|
5
|
|
|
class Arr |
|
6
|
|
|
{ |
|
7
|
|
|
/** |
|
8
|
|
|
*Returns true if the provided function returns true for all elements of an array, false otherwise. |
|
9
|
|
|
* |
|
10
|
|
|
* @param array $items |
|
11
|
|
|
* @param mixed $func |
|
12
|
|
|
*/ |
|
13
|
1 |
|
public static function all(array $items, $func): bool |
|
14
|
|
|
{ |
|
15
|
1 |
|
return \count(array_filter($items, $func)) === \count($items); |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Returns true if the provided function returns true for at least one element of an array, false otherwise. |
|
20
|
|
|
* |
|
21
|
|
|
* @param array $items |
|
22
|
|
|
* @param mixed $func |
|
23
|
|
|
*/ |
|
24
|
1 |
|
public static function any(array $items, $func): bool |
|
25
|
|
|
{ |
|
26
|
1 |
|
return \count(array_filter($items, $func)) > 0; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Chunks an array into smaller arrays of a specified size. |
|
31
|
|
|
* |
|
32
|
|
|
* @param array $items |
|
33
|
|
|
* @param int $size |
|
34
|
|
|
*/ |
|
35
|
|
|
public static function chunk(array $items, int $size): array |
|
36
|
|
|
{ |
|
37
|
|
|
return array_chunk($items, $size); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Deep flattens an array. |
|
42
|
|
|
* |
|
43
|
|
|
* @param array $items |
|
44
|
|
|
*/ |
|
45
|
1 |
View Code Duplication |
public static function deepFlatten(array $items): array |
|
46
|
|
|
{ |
|
47
|
1 |
|
$result = []; |
|
48
|
1 |
|
foreach ($items as $item) { |
|
49
|
1 |
|
if (!\is_array($item)) { |
|
50
|
1 |
|
$result[] = $item; |
|
51
|
|
|
} else { |
|
52
|
1 |
|
array_push($result, ...self::deepFlatten($item)); |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
1 |
|
return $result; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Returns a new array with n elements removed from the left. |
|
61
|
|
|
* |
|
62
|
|
|
* @param array $items |
|
63
|
|
|
* @param int $n |
|
64
|
|
|
*/ |
|
65
|
1 |
|
public static function drop(array $items, int $n = 1): array |
|
66
|
|
|
{ |
|
67
|
1 |
|
return \array_slice($items, $n); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Returns the last element for which the provided function returns a truthy value. |
|
72
|
|
|
* |
|
73
|
|
|
* @param array $items |
|
74
|
|
|
* @param mixed $func |
|
75
|
|
|
* |
|
76
|
|
|
* @return mixed |
|
77
|
|
|
*/ |
|
78
|
1 |
|
public static function findLast(array $items, $func) |
|
79
|
|
|
{ |
|
80
|
1 |
|
$filteredItems = array_filter($items, $func); |
|
81
|
|
|
|
|
82
|
1 |
|
return array_pop($filteredItems); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Returns the index of the last element for which the provided function returns a truthy value. |
|
87
|
|
|
* |
|
88
|
|
|
* @param array $items |
|
89
|
|
|
* @param mixed $func |
|
90
|
|
|
*/ |
|
91
|
1 |
|
public static function findLastIndex(array $items, $func) |
|
92
|
|
|
{ |
|
93
|
1 |
|
$keys = array_keys(array_filter($items, $func)); |
|
94
|
|
|
|
|
95
|
1 |
|
return array_pop($keys); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Flattens an array up to the one level depth. |
|
100
|
|
|
* |
|
101
|
|
|
* @param array $items |
|
102
|
|
|
*/ |
|
103
|
1 |
View Code Duplication |
public static function flatten(array $items): array |
|
104
|
|
|
{ |
|
105
|
1 |
|
$result = []; |
|
106
|
1 |
|
foreach ($items as $item) { |
|
107
|
1 |
|
if (!\is_array($item)) { |
|
108
|
1 |
|
$result[] = $item; |
|
109
|
|
|
} else { |
|
110
|
1 |
|
array_push($result, ...array_values($item)); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
1 |
|
return $result; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Groups the elements of an array based on the given function. |
|
119
|
|
|
* |
|
120
|
|
|
* @param array $items |
|
121
|
|
|
* @param mixed $func |
|
122
|
|
|
*/ |
|
123
|
1 |
|
public static function groupBy(array $items, $func): array |
|
124
|
|
|
{ |
|
125
|
1 |
|
$group = []; |
|
126
|
1 |
|
foreach ($items as $item) { |
|
127
|
1 |
|
if ((!\is_string($func) && \is_callable($func)) || \function_exists($func)) { |
|
128
|
1 |
|
$key = \call_user_func($func, $item); |
|
129
|
1 |
|
$group[$key][] = $item; |
|
130
|
1 |
|
} elseif (\is_object($item)) { |
|
131
|
1 |
|
$group[$item->{$func}][] = $item; |
|
132
|
1 |
|
} elseif (isset($item[$func])) { |
|
133
|
1 |
|
$group[$item[$func]][] = $item; |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
1 |
|
return $group; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Sorts a collection of arrays or objects by key |
|
142
|
|
|
* |
|
143
|
|
|
* @param array $items |
|
144
|
|
|
* @param mixed $attr |
|
145
|
|
|
* @param string $order |
|
146
|
|
|
* |
|
147
|
|
|
* @return array |
|
148
|
|
|
*/ |
|
149
|
1 |
|
public static function orderBy(array $items, $attr, string $order): array |
|
150
|
|
|
{ |
|
151
|
1 |
|
$sortedItems = []; |
|
152
|
1 |
|
foreach ($items as $item) { |
|
153
|
1 |
|
$key = \is_object($item) ? $item->{$attr} : $item[$attr]; |
|
154
|
1 |
|
$sortedItems[$key] = $item; |
|
155
|
|
|
} |
|
156
|
1 |
|
if ('desc' === $order) { |
|
157
|
1 |
|
krsort($sortedItems); |
|
158
|
|
|
} else { |
|
159
|
|
|
ksort($sortedItems); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
1 |
|
return array_values($sortedItems); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Checks a flat list for duplicate values. Returns true if duplicate values exists and false if values are all unique. |
|
167
|
|
|
* |
|
168
|
|
|
* @param array $items |
|
169
|
|
|
* |
|
170
|
|
|
* @return bool |
|
171
|
|
|
*/ |
|
172
|
1 |
|
public static function hasDuplicates(array $items): bool |
|
173
|
|
|
{ |
|
174
|
1 |
|
return \count($items) > \count(array_unique($items)); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* Returns the head of a list. |
|
179
|
|
|
* |
|
180
|
|
|
* @param array $items |
|
181
|
|
|
* |
|
182
|
|
|
* @return mixed |
|
183
|
|
|
*/ |
|
184
|
1 |
|
public static function head(array $items) |
|
185
|
|
|
{ |
|
186
|
1 |
|
return reset($items); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* Returns the last element in an array. |
|
191
|
|
|
* |
|
192
|
|
|
* @param array $items |
|
193
|
|
|
* |
|
194
|
|
|
* @return mixed |
|
195
|
|
|
*/ |
|
196
|
1 |
|
public static function last(array $items) |
|
197
|
|
|
{ |
|
198
|
1 |
|
return end($items); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* Retrieves all of the values for a given key: |
|
203
|
|
|
* |
|
204
|
|
|
* @param array $items |
|
205
|
|
|
* @param mixed $key |
|
206
|
|
|
*/ |
|
207
|
1 |
|
public static function pluck(array $items, $key) |
|
208
|
|
|
{ |
|
209
|
|
|
return array_map(function ($item) use ($key) { |
|
210
|
1 |
|
return \is_object($item) ? $item->$key : $item[$key]; |
|
211
|
1 |
|
}, $items); |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* Mutates the original array to filter out the values specified. |
|
216
|
|
|
* |
|
217
|
|
|
* @param array $items |
|
218
|
|
|
* @param mixed ...$params |
|
219
|
|
|
*/ |
|
|
|
|
|
|
220
|
1 |
|
public static function pull(&$items, ...$params) |
|
221
|
|
|
{ |
|
222
|
1 |
|
$items = array_values(array_diff($items, $params)); |
|
223
|
|
|
|
|
224
|
1 |
|
return $items; |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* Filters the collection using the given callback. |
|
229
|
|
|
* |
|
230
|
|
|
* @param array $items |
|
231
|
|
|
* @param mixed $func |
|
232
|
|
|
*/ |
|
233
|
1 |
|
public static function reject(array $items, $func) |
|
234
|
|
|
{ |
|
235
|
1 |
|
return array_values(array_diff($items, array_filter($items, $func))); |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
/** |
|
239
|
|
|
* Removes elements from an array for which the given function returns false. |
|
240
|
|
|
* |
|
241
|
|
|
* @param array $items |
|
242
|
|
|
* @param mixed $func |
|
243
|
|
|
*/ |
|
244
|
1 |
|
public static function remove(array $items, $func) |
|
245
|
|
|
{ |
|
246
|
1 |
|
$filtered = array_filter($items, $func); |
|
247
|
|
|
|
|
248
|
1 |
|
return array_diff_key($items, $filtered); |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
/** |
|
252
|
|
|
* Returns all elements in an array except for the first one. |
|
253
|
|
|
* |
|
254
|
|
|
* @param array $items |
|
255
|
|
|
*/ |
|
256
|
1 |
|
public static function tail(array $items) |
|
257
|
|
|
{ |
|
258
|
1 |
|
return \count($items) > 1 ? \array_slice($items, 1) : $items; |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
/** |
|
262
|
|
|
* Returns an array with n elements removed from the beginning. |
|
263
|
|
|
* |
|
264
|
|
|
* @param array $items |
|
265
|
|
|
* @param int $n |
|
266
|
|
|
*/ |
|
267
|
1 |
|
public static function take(array $items, int $n = 1): array |
|
268
|
|
|
{ |
|
269
|
1 |
|
return \array_slice($items, 0, $n); |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
/** |
|
273
|
|
|
* Filters out the elements of an array, that have one of the specified values. |
|
274
|
|
|
* |
|
275
|
|
|
* @param array $items |
|
276
|
|
|
* @param mixed ...$params |
|
277
|
|
|
*/ |
|
|
|
|
|
|
278
|
1 |
|
public static function without(array $items, ...$params) |
|
279
|
|
|
{ |
|
280
|
1 |
|
return array_values(array_diff($items, $params)); |
|
281
|
|
|
} |
|
282
|
|
|
} |
|
283
|
|
|
|