1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CarbonFramework\Support; |
4
|
|
|
|
5
|
|
|
use ArrayAccess; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* A collection of tools dealing with arrays |
9
|
|
|
* @credit (limited version of) illuminate/support |
10
|
|
|
*/ |
11
|
|
|
class Arr |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Determine whether the given value is array accessible. |
15
|
|
|
* |
16
|
|
|
* @param mixed $value |
17
|
|
|
* @return bool |
18
|
|
|
*/ |
19
|
|
|
public static function accessible($value) |
20
|
|
|
{ |
21
|
|
|
return is_array($value) || $value instanceof ArrayAccess; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Add an element to an array using "dot" notation if it doesn't exist. |
26
|
|
|
* |
27
|
|
|
* @param array $array |
28
|
|
|
* @param string $key |
29
|
|
|
* @param mixed $value |
30
|
|
|
* @return array |
31
|
|
|
*/ |
32
|
|
|
public static function add($array, $key, $value) |
33
|
|
|
{ |
34
|
|
|
if (is_null(static::get($array, $key))) { |
35
|
|
|
static::set($array, $key, $value); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
return $array; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Collapse an array of arrays into a single array. |
43
|
|
|
* |
44
|
|
|
* @param array $array |
45
|
|
|
* @return array |
46
|
|
|
*/ |
47
|
|
|
public static function collapse($array) |
48
|
|
|
{ |
49
|
|
|
$results = []; |
50
|
|
|
foreach ($array as $values) { |
51
|
|
|
if (! is_array($values)) { |
52
|
|
|
continue; |
53
|
|
|
} |
54
|
|
|
$results = array_merge($results, $values); |
55
|
|
|
} |
56
|
|
|
return $results; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Divide an array into two arrays. One with keys and the other with values. |
61
|
|
|
* |
62
|
|
|
* @param array $array |
63
|
|
|
* @return array |
64
|
|
|
*/ |
65
|
|
|
public static function divide($array) |
66
|
|
|
{ |
67
|
|
|
return [array_keys($array), array_values($array)]; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Flatten a multi-dimensional associative array with dots. |
72
|
|
|
* |
73
|
|
|
* @param array $array |
74
|
|
|
* @param string $prepend |
75
|
|
|
* @return array |
76
|
|
|
*/ |
77
|
|
|
public static function dot($array, $prepend = '') |
78
|
|
|
{ |
79
|
|
|
$results = []; |
80
|
|
|
|
81
|
|
|
foreach ($array as $key => $value) { |
82
|
|
|
if (is_array($value) && ! empty($value)) { |
83
|
|
|
$results = array_merge($results, static::dot($value, $prepend.$key.'.')); |
84
|
|
|
} else { |
85
|
|
|
$results[$prepend.$key] = $value; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $results; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Get all of the given array except for a specified array of items. |
94
|
|
|
* |
95
|
|
|
* @param array $array |
96
|
|
|
* @param array|string $keys |
97
|
|
|
* @return array |
98
|
|
|
*/ |
99
|
|
|
public static function except($array, $keys) |
100
|
|
|
{ |
101
|
|
|
static::forget($array, $keys); |
102
|
|
|
|
103
|
|
|
return $array; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Determine if the given key exists in the provided array. |
108
|
|
|
* |
109
|
|
|
* @param \ArrayAccess|array $array |
110
|
|
|
* @param string|int $key |
111
|
|
|
* @return bool |
112
|
|
|
*/ |
113
|
|
|
public static function exists($array, $key) |
114
|
|
|
{ |
115
|
|
|
if ($array instanceof ArrayAccess) { |
116
|
|
|
return $array->offsetExists($key); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return array_key_exists($key, $array); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Return the first element in an array passing a given truth test. |
124
|
|
|
* |
125
|
|
|
* @param array $array |
126
|
|
|
* @param callable|null $callback |
127
|
|
|
* @param mixed $default |
128
|
|
|
* @return mixed |
129
|
|
|
*/ |
130
|
|
|
public static function first($array, callable $callback = null, $default = null) |
131
|
|
|
{ |
132
|
|
|
if (is_null($callback)) { |
133
|
|
|
if (empty($array)) { |
134
|
|
|
return $default; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
foreach ($array as $item) { |
138
|
|
|
return $item; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
foreach ($array as $key => $value) { |
143
|
|
|
if (call_user_func($callback, $value, $key)) { |
144
|
|
|
return $value; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return $default; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Return the last element in an array passing a given truth test. |
153
|
|
|
* |
154
|
|
|
* @param array $array |
155
|
|
|
* @param callable|null $callback |
156
|
|
|
* @param mixed $default |
157
|
|
|
* @return mixed |
158
|
|
|
*/ |
159
|
|
|
public static function last($array, callable $callback = null, $default = null) |
160
|
|
|
{ |
161
|
|
|
if (is_null($callback)) { |
162
|
|
|
return empty($array) ? $default : end($array); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
return static::first(array_reverse($array, true), $callback, $default); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Remove one or many array items from a given array using "dot" notation. |
170
|
|
|
* |
171
|
|
|
* @param array $array |
172
|
|
|
* @param array|string $keys |
173
|
|
|
* @return void |
174
|
|
|
*/ |
175
|
|
|
public static function forget(&$array, $keys) |
176
|
|
|
{ |
177
|
|
|
$original = &$array; |
178
|
|
|
|
179
|
|
|
$keys = (array) $keys; |
180
|
|
|
|
181
|
|
|
if (count($keys) === 0) { |
182
|
|
|
return; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
foreach ($keys as $key) { |
186
|
|
|
// if the exact key exists in the top-level, remove it |
187
|
|
|
if (static::exists($array, $key)) { |
188
|
|
|
unset($array[$key]); |
189
|
|
|
|
190
|
|
|
continue; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
$parts = explode('.', $key); |
194
|
|
|
|
195
|
|
|
// clean up before each pass |
196
|
|
|
$array = &$original; |
197
|
|
|
|
198
|
|
|
while (count($parts) > 1) { |
199
|
|
|
$part = array_shift($parts); |
200
|
|
|
|
201
|
|
|
if (isset($array[$part]) && is_array($array[$part])) { |
202
|
|
|
$array = &$array[$part]; |
203
|
|
|
} else { |
204
|
|
|
continue 2; |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
unset($array[array_shift($parts)]); |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Get an item from an array using "dot" notation. |
214
|
|
|
* |
215
|
|
|
* @param \ArrayAccess|array $array |
216
|
|
|
* @param string $key |
217
|
|
|
* @param mixed $default |
218
|
|
|
* @return mixed |
219
|
|
|
*/ |
220
|
|
|
public static function get($array, $key, $default = null) |
221
|
|
|
{ |
222
|
|
|
if (! static::accessible($array)) { |
223
|
|
|
return $default; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
if (is_null($key)) { |
227
|
|
|
return $array; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
if (static::exists($array, $key)) { |
231
|
|
|
return $array[$key]; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
foreach (explode('.', $key) as $segment) { |
235
|
|
|
if (static::accessible($array) && static::exists($array, $segment)) { |
236
|
|
|
$array = $array[$segment]; |
237
|
|
|
} else { |
238
|
|
|
return $default; |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
return $array; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* Check if an item or items exist in an array using "dot" notation. |
247
|
|
|
* |
248
|
|
|
* @param \ArrayAccess|array $array |
249
|
|
|
* @param string|array $keys |
250
|
|
|
* @return bool |
251
|
|
|
*/ |
252
|
|
|
public static function has($array, $keys) |
253
|
|
|
{ |
254
|
|
|
if (is_null($keys)) { |
255
|
|
|
return false; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
$keys = (array) $keys; |
259
|
|
|
|
260
|
|
|
if (! $array) { |
261
|
|
|
return false; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
if ($keys === []) { |
265
|
|
|
return false; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
foreach ($keys as $key) { |
269
|
|
|
$subKeyArray = $array; |
270
|
|
|
|
271
|
|
|
if (static::exists($array, $key)) { |
272
|
|
|
continue; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
foreach (explode('.', $key) as $segment) { |
276
|
|
|
if (static::accessible($subKeyArray) && static::exists($subKeyArray, $segment)) { |
277
|
|
|
$subKeyArray = $subKeyArray[$segment]; |
278
|
|
|
} else { |
279
|
|
|
return false; |
280
|
|
|
} |
281
|
|
|
} |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
return true; |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* Determines if an array is associative. |
289
|
|
|
* |
290
|
|
|
* An array is "associative" if it doesn't have sequential numerical keys beginning with zero. |
291
|
|
|
* |
292
|
|
|
* @param array $array |
293
|
|
|
* @return bool |
294
|
|
|
*/ |
295
|
|
|
public static function isAssoc(array $array) |
296
|
|
|
{ |
297
|
|
|
$keys = array_keys($array); |
298
|
|
|
|
299
|
|
|
return array_keys($keys) !== $keys; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* Get a subset of the items from the given array. |
304
|
|
|
* |
305
|
|
|
* @param array $array |
306
|
|
|
* @param array|string $keys |
307
|
|
|
* @return array |
308
|
|
|
*/ |
309
|
|
|
public static function only($array, $keys) |
310
|
|
|
{ |
311
|
|
|
return array_intersect_key($array, array_flip((array) $keys)); |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* Pluck an array of values from an array. |
316
|
|
|
* |
317
|
|
|
* @param array $array |
318
|
|
|
* @param string|array $value |
319
|
|
|
* @param string|array|null $key |
320
|
|
|
* @return array |
321
|
|
|
*/ |
322
|
|
|
public static function pluck($array, $value, $key = null) |
323
|
|
|
{ |
324
|
|
|
$results = []; |
325
|
|
|
|
326
|
|
|
list($value, $key) = static::explodePluckParameters($value, $key); |
327
|
|
|
|
328
|
|
|
foreach ($array as $item) { |
329
|
|
|
$itemValue = static::data_get($item, $value); |
330
|
|
|
|
331
|
|
|
// If the key is "null", we will just append the value to the array and keep |
332
|
|
|
// looping. Otherwise we will key the array using the value of the key we |
333
|
|
|
// received from the developer. Then we'll return the final array form. |
334
|
|
|
if (is_null($key)) { |
335
|
|
|
$results[] = $itemValue; |
336
|
|
|
} else { |
337
|
|
|
$itemKey = static::data_get($item, $key); |
338
|
|
|
|
339
|
|
|
$results[$itemKey] = $itemValue; |
340
|
|
|
} |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
return $results; |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
/** |
347
|
|
|
* Explode the "value" and "key" arguments passed to "pluck". |
348
|
|
|
* |
349
|
|
|
* @param string|array $value |
350
|
|
|
* @param string|array|null $key |
351
|
|
|
* @return array |
352
|
|
|
*/ |
353
|
|
|
protected static function explodePluckParameters($value, $key) |
354
|
|
|
{ |
355
|
|
|
$value = is_string($value) ? explode('.', $value) : $value; |
356
|
|
|
|
357
|
|
|
$key = is_null($key) || is_array($key) ? $key : explode('.', $key); |
358
|
|
|
|
359
|
|
|
return [$value, $key]; |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
/** |
363
|
|
|
* Push an item onto the beginning of an array. |
364
|
|
|
* |
365
|
|
|
* @param array $array |
366
|
|
|
* @param mixed $value |
367
|
|
|
* @param mixed $key |
368
|
|
|
* @return array |
369
|
|
|
*/ |
370
|
|
|
public static function prepend($array, $value, $key = null) |
371
|
|
|
{ |
372
|
|
|
if (is_null($key)) { |
373
|
|
|
array_unshift($array, $value); |
374
|
|
|
} else { |
375
|
|
|
$array = [$key => $value] + $array; |
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
return $array; |
379
|
|
|
} |
380
|
|
|
|
381
|
|
|
/** |
382
|
|
|
* Get a value from the array, and remove it. |
383
|
|
|
* |
384
|
|
|
* @param array $array |
385
|
|
|
* @param string $key |
386
|
|
|
* @param mixed $default |
387
|
|
|
* @return mixed |
388
|
|
|
*/ |
389
|
|
|
public static function pull(&$array, $key, $default = null) |
390
|
|
|
{ |
391
|
|
|
$value = static::get($array, $key, $default); |
392
|
|
|
|
393
|
|
|
static::forget($array, $key); |
394
|
|
|
|
395
|
|
|
return $value; |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
/** |
399
|
|
|
* Set an array item to a given value using "dot" notation. |
400
|
|
|
* |
401
|
|
|
* If no key is given to the method, the entire array will be replaced. |
402
|
|
|
* |
403
|
|
|
* @param array $array |
404
|
|
|
* @param string $key |
405
|
|
|
* @param mixed $value |
406
|
|
|
* @return array |
407
|
|
|
*/ |
408
|
|
|
public static function set(&$array, $key, $value) |
409
|
|
|
{ |
410
|
|
|
if (is_null($key)) { |
411
|
|
|
return $array = $value; |
412
|
|
|
} |
413
|
|
|
|
414
|
|
|
$keys = explode('.', $key); |
415
|
|
|
|
416
|
|
|
while (count($keys) > 1) { |
417
|
|
|
$key = array_shift($keys); |
418
|
|
|
|
419
|
|
|
// If the key doesn't exist at this depth, we will just create an empty array |
420
|
|
|
// to hold the next value, allowing us to create the arrays to hold final |
421
|
|
|
// values at the correct depth. Then we'll keep digging into the array. |
422
|
|
|
if (! isset($array[$key]) || ! is_array($array[$key])) { |
423
|
|
|
$array[$key] = []; |
424
|
|
|
} |
425
|
|
|
|
426
|
|
|
$array = &$array[$key]; |
427
|
|
|
} |
428
|
|
|
|
429
|
|
|
$array[array_shift($keys)] = $value; |
430
|
|
|
|
431
|
|
|
return $array; |
432
|
|
|
} |
433
|
|
|
|
434
|
|
|
/** |
435
|
|
|
* Shuffle the given array and return the result. |
436
|
|
|
* |
437
|
|
|
* @param array $array |
438
|
|
|
* @return array |
439
|
|
|
*/ |
440
|
|
|
public static function shuffle($array) |
441
|
|
|
{ |
442
|
|
|
shuffle($array); |
443
|
|
|
|
444
|
|
|
return $array; |
445
|
|
|
} |
446
|
|
|
|
447
|
|
|
/** |
448
|
|
|
* Recursively sort an array by keys and values. |
449
|
|
|
* |
450
|
|
|
* @param array $array |
451
|
|
|
* @return array |
452
|
|
|
*/ |
453
|
|
|
public static function sortRecursive($array) |
454
|
|
|
{ |
455
|
|
|
foreach ($array as &$value) { |
456
|
|
|
if (is_array($value)) { |
457
|
|
|
$value = static::sortRecursive($value); |
458
|
|
|
} |
459
|
|
|
} |
460
|
|
|
|
461
|
|
|
if (static::isAssoc($array)) { |
462
|
|
|
ksort($array); |
463
|
|
|
} else { |
464
|
|
|
sort($array); |
465
|
|
|
} |
466
|
|
|
|
467
|
|
|
return $array; |
468
|
|
|
} |
469
|
|
|
|
470
|
|
|
/** |
471
|
|
|
* Get an item from an array or object using "dot" notation. |
472
|
|
|
* |
473
|
|
|
* @param mixed $target |
474
|
|
|
* @param string|array $key |
475
|
|
|
* @param mixed $default |
476
|
|
|
* @return mixed |
477
|
|
|
*/ |
478
|
|
|
public static function data_get($target, $key, $default = null) |
479
|
|
|
{ |
480
|
|
|
if (is_null($key)) { |
481
|
|
|
return $target; |
482
|
|
|
} |
483
|
|
|
$key = is_array($key) ? $key : explode('.', $key); |
484
|
|
|
while (! is_null($segment = array_shift($key))) { |
485
|
|
|
if ($segment === '*') { |
486
|
|
|
if (! is_array($target)) { |
487
|
|
|
return $default; |
488
|
|
|
} |
489
|
|
|
$result = static::pluck($target, $key); |
490
|
|
|
return in_array('*', $key) ? static::collapse($result) : $result; |
491
|
|
|
} |
492
|
|
|
if (static::accessible($target) && static::exists($target, $segment)) { |
493
|
|
|
$target = $target[$segment]; |
494
|
|
|
} elseif (is_object($target) && isset($target->{$segment})) { |
495
|
|
|
$target = $target->{$segment}; |
496
|
|
|
} else { |
497
|
|
|
return $default; |
498
|
|
|
} |
499
|
|
|
} |
500
|
|
|
return $target; |
501
|
|
|
} |
502
|
|
|
} |
503
|
|
|
|