1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use IlluminateAgnostic\Arr\Support\Arr; |
4
|
|
|
use IlluminateAgnostic\Arr\Support\Collection; |
5
|
|
|
use IlluminateAgnostic\Arr\Support\Debug\Dumper; |
6
|
|
|
use Illuminate\Support\Collection as IlluminateCollection; |
7
|
|
|
|
8
|
|
|
if (! function_exists('array_add')) { |
9
|
|
|
/** |
10
|
|
|
* Add an element to an array using "dot" notation if it doesn't exist. |
11
|
|
|
* |
12
|
|
|
* @param array $array |
13
|
|
|
* @param string $key |
14
|
|
|
* @param mixed $value |
15
|
|
|
* @return array |
16
|
|
|
*/ |
17
|
|
|
function array_add($array, $key, $value) |
18
|
|
|
{ |
19
|
|
|
return Arr::add($array, $key, $value); |
20
|
|
|
} |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
if (! function_exists('array_collapse')) { |
24
|
|
|
/** |
25
|
|
|
* Collapse an array of arrays into a single array. |
26
|
|
|
* |
27
|
|
|
* @param array $array |
28
|
|
|
* @return array |
29
|
|
|
*/ |
30
|
|
|
function array_collapse($array) |
31
|
|
|
{ |
32
|
|
|
return Arr::collapse($array); |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
if (! function_exists('array_divide')) { |
37
|
|
|
/** |
38
|
|
|
* Divide an array into two arrays. One with keys and the other with values. |
39
|
|
|
* |
40
|
|
|
* @param array $array |
41
|
|
|
* @return array |
42
|
|
|
*/ |
43
|
|
|
function array_divide($array) |
44
|
|
|
{ |
45
|
|
|
return Arr::divide($array); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
if (! function_exists('array_dot')) { |
50
|
|
|
/** |
51
|
|
|
* Flatten a multi-dimensional associative array with dots. |
52
|
|
|
* |
53
|
|
|
* @param array $array |
54
|
|
|
* @param string $prepend |
55
|
|
|
* @return array |
56
|
|
|
*/ |
57
|
|
|
function array_dot($array, $prepend = '') |
58
|
|
|
{ |
59
|
|
|
return Arr::dot($array, $prepend); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
if (! function_exists('array_except')) { |
64
|
|
|
/** |
65
|
|
|
* Get all of the given array except for a specified array of keys. |
66
|
|
|
* |
67
|
|
|
* @param array $array |
68
|
|
|
* @param array|string $keys |
69
|
|
|
* @return array |
70
|
|
|
*/ |
71
|
|
|
function array_except($array, $keys) |
72
|
|
|
{ |
73
|
|
|
return Arr::except($array, $keys); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if (! function_exists('array_first')) { |
78
|
|
|
/** |
79
|
|
|
* Return the first element in an array passing a given truth test. |
80
|
|
|
* |
81
|
|
|
* @param array $array |
82
|
|
|
* @param callable|null $callback |
83
|
|
|
* @param mixed $default |
84
|
|
|
* @return mixed |
85
|
|
|
*/ |
86
|
|
|
function array_first($array, callable $callback = null, $default = null) |
87
|
|
|
{ |
88
|
|
|
return Arr::first($array, $callback, $default); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
if (! function_exists('array_flatten')) { |
93
|
|
|
/** |
94
|
|
|
* Flatten a multi-dimensional array into a single level. |
95
|
|
|
* |
96
|
|
|
* @param array $array |
97
|
|
|
* @param int $depth |
98
|
|
|
* @return array |
99
|
|
|
*/ |
100
|
|
|
function array_flatten($array, $depth = INF) |
101
|
|
|
{ |
102
|
|
|
return Arr::flatten($array, $depth); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
if (! function_exists('array_forget')) { |
107
|
|
|
/** |
108
|
|
|
* Remove one or many array items from a given array using "dot" notation. |
109
|
|
|
* |
110
|
|
|
* @param array $array |
111
|
|
|
* @param array|string $keys |
112
|
|
|
* @return void |
113
|
|
|
*/ |
114
|
|
|
function array_forget(&$array, $keys) |
115
|
|
|
{ |
116
|
|
|
return Arr::forget($array, $keys); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
if (! function_exists('array_get')) { |
121
|
|
|
/** |
122
|
|
|
* Get an item from an array using "dot" notation. |
123
|
|
|
* |
124
|
|
|
* @param \ArrayAccess|array $array |
125
|
|
|
* @param string $key |
126
|
|
|
* @param mixed $default |
127
|
|
|
* @return mixed |
128
|
|
|
*/ |
129
|
|
|
function array_get($array, $key, $default = null) |
130
|
|
|
{ |
131
|
|
|
return Arr::get($array, $key, $default); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
if (! function_exists('array_has')) { |
136
|
|
|
/** |
137
|
|
|
* Check if an item or items exist in an array using "dot" notation. |
138
|
|
|
* |
139
|
|
|
* @param \ArrayAccess|array $array |
140
|
|
|
* @param string|array $keys |
141
|
|
|
* @return bool |
142
|
|
|
*/ |
143
|
|
|
function array_has($array, $keys) |
144
|
|
|
{ |
145
|
|
|
return Arr::has($array, $keys); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
if (! function_exists('array_last')) { |
150
|
|
|
/** |
151
|
|
|
* Return the last element in an array passing a given truth test. |
152
|
|
|
* |
153
|
|
|
* @param array $array |
154
|
|
|
* @param callable|null $callback |
155
|
|
|
* @param mixed $default |
156
|
|
|
* @return mixed |
157
|
|
|
*/ |
158
|
|
|
function array_last($array, callable $callback = null, $default = null) |
159
|
|
|
{ |
160
|
|
|
return Arr::last($array, $callback, $default); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
if (! function_exists('array_only')) { |
165
|
|
|
/** |
166
|
|
|
* Get a subset of the items from the given array. |
167
|
|
|
* |
168
|
|
|
* @param array $array |
169
|
|
|
* @param array|string $keys |
170
|
|
|
* @return array |
171
|
|
|
*/ |
172
|
|
|
function array_only($array, $keys) |
173
|
|
|
{ |
174
|
|
|
return Arr::only($array, $keys); |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
if (! function_exists('array_pluck')) { |
179
|
|
|
/** |
180
|
|
|
* Pluck an array of values from an array. |
181
|
|
|
* |
182
|
|
|
* @param array $array |
183
|
|
|
* @param string|array $value |
184
|
|
|
* @param string|array|null $key |
185
|
|
|
* @return array |
186
|
|
|
*/ |
187
|
|
|
function array_pluck($array, $value, $key = null) |
188
|
|
|
{ |
189
|
|
|
return Arr::pluck($array, $value, $key); |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
if (! function_exists('array_prepend')) { |
194
|
|
|
/** |
195
|
|
|
* Push an item onto the beginning of an array. |
196
|
|
|
* |
197
|
|
|
* @param array $array |
198
|
|
|
* @param mixed $value |
199
|
|
|
* @param mixed $key |
200
|
|
|
* @return array |
201
|
|
|
*/ |
202
|
|
|
function array_prepend($array, $value, $key = null) |
203
|
|
|
{ |
204
|
|
|
return Arr::prepend($array, $value, $key); |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
if (! function_exists('array_pull')) { |
209
|
|
|
/** |
210
|
|
|
* Get a value from the array, and remove it. |
211
|
|
|
* |
212
|
|
|
* @param array $array |
213
|
|
|
* @param string $key |
214
|
|
|
* @param mixed $default |
215
|
|
|
* @return mixed |
216
|
|
|
*/ |
217
|
|
|
function array_pull(&$array, $key, $default = null) |
218
|
|
|
{ |
219
|
|
|
return Arr::pull($array, $key, $default); |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
if (! function_exists('array_random')) { |
224
|
|
|
/** |
225
|
|
|
* Get a random value from an array. |
226
|
|
|
* |
227
|
|
|
* @param array $array |
228
|
|
|
* @param int|null $num |
229
|
|
|
* @return mixed |
230
|
|
|
*/ |
231
|
|
|
function array_random($array, $num = null) |
232
|
|
|
{ |
233
|
|
|
return Arr::random($array, $num); |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
if (! function_exists('array_set')) { |
238
|
|
|
/** |
239
|
|
|
* Set an array item to a given value using "dot" notation. |
240
|
|
|
* |
241
|
|
|
* If no key is given to the method, the entire array will be replaced. |
242
|
|
|
* |
243
|
|
|
* @param array $array |
244
|
|
|
* @param string $key |
245
|
|
|
* @param mixed $value |
246
|
|
|
* @return array |
247
|
|
|
*/ |
248
|
|
|
function array_set(&$array, $key, $value) |
249
|
|
|
{ |
250
|
|
|
return Arr::set($array, $key, $value); |
251
|
|
|
} |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
if (! function_exists('array_sort')) { |
255
|
|
|
/** |
256
|
|
|
* Sort the array by the given callback or attribute name. |
257
|
|
|
* |
258
|
|
|
* @param array $array |
259
|
|
|
* @param callable|string|null $callback |
260
|
|
|
* @return array |
261
|
|
|
*/ |
262
|
|
|
function array_sort($array, $callback = null) |
263
|
|
|
{ |
264
|
|
|
return Arr::sort($array, $callback); |
265
|
|
|
} |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
if (! function_exists('array_sort_recursive')) { |
269
|
|
|
/** |
270
|
|
|
* Recursively sort an array by keys and values. |
271
|
|
|
* |
272
|
|
|
* @param array $array |
273
|
|
|
* @return array |
274
|
|
|
*/ |
275
|
|
|
function array_sort_recursive($array) |
276
|
|
|
{ |
277
|
|
|
return Arr::sortRecursive($array); |
278
|
|
|
} |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
if (! function_exists('array_where')) { |
282
|
|
|
/** |
283
|
|
|
* Filter the array using the given callback. |
284
|
|
|
* |
285
|
|
|
* @param array $array |
286
|
|
|
* @param callable $callback |
287
|
|
|
* @return array |
288
|
|
|
*/ |
289
|
|
|
function array_where($array, callable $callback) |
290
|
|
|
{ |
291
|
|
|
return Arr::where($array, $callback); |
292
|
|
|
} |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
if (! function_exists('array_wrap')) { |
296
|
|
|
/** |
297
|
|
|
* If the given value is not an array, wrap it in one. |
298
|
|
|
* |
299
|
|
|
* @param mixed $value |
300
|
|
|
* @return array |
301
|
|
|
*/ |
302
|
|
|
function array_wrap($value) |
303
|
|
|
{ |
304
|
|
|
return Arr::wrap($value); |
305
|
|
|
} |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
if (! function_exists('collect')) { |
309
|
|
|
/** |
310
|
|
|
* Create a collection from the given value. |
311
|
|
|
* |
312
|
|
|
* @param mixed $value |
313
|
|
|
* @return \IlluminateAgnostic\Arr\Support\Collection |
314
|
|
|
*/ |
315
|
|
|
function collect($value = null) |
316
|
|
|
{ |
317
|
4 |
|
return new Collection($value); |
318
|
|
|
} |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
if (! function_exists('data_fill')) { |
322
|
|
|
/** |
323
|
|
|
* Fill in data where it's missing. |
324
|
|
|
* |
325
|
|
|
* @param mixed $target |
326
|
|
|
* @param string|array $key |
327
|
|
|
* @param mixed $value |
328
|
|
|
* @return mixed |
329
|
|
|
*/ |
330
|
|
|
function data_fill(&$target, $key, $value) |
331
|
|
|
{ |
332
|
|
|
return data_set($target, $key, $value, false); |
333
|
|
|
} |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
if (! function_exists('data_get')) { |
337
|
|
|
/** |
338
|
|
|
* Get an item from an array or object using "dot" notation. |
339
|
|
|
* |
340
|
|
|
* @param mixed $target |
341
|
|
|
* @param string|array $key |
342
|
|
|
* @param mixed $default |
343
|
|
|
* @return mixed |
344
|
|
|
*/ |
345
|
|
|
function data_get($target, $key, $default = null) |
346
|
|
|
{ |
347
|
41 |
|
if (is_null($key)) { |
348
|
6 |
|
return $target; |
349
|
|
|
} |
350
|
|
|
|
351
|
39 |
|
$key = is_array($key) ? $key : explode('.', $key); |
352
|
|
|
|
353
|
39 |
|
while (! is_null($segment = array_shift($key))) { |
354
|
39 |
|
if ($segment === '*') { |
355
|
|
|
if ($target instanceof Collection) { |
356
|
|
|
$target = $target->all(); |
357
|
|
|
} elseif (! is_array($target)) { |
358
|
|
|
return value($default); |
359
|
|
|
} |
360
|
|
|
|
361
|
|
|
$result = Arr::pluck($target, $key); |
362
|
|
|
|
363
|
|
|
return in_array('*', $key) ? Arr::collapse($result) : $result; |
364
|
|
|
} |
365
|
|
|
|
366
|
39 |
|
if (Arr::accessible($target) && Arr::exists($target, $segment)) { |
367
|
33 |
|
$target = $target[$segment]; |
368
|
12 |
|
} elseif (is_object($target) && isset($target->{$segment})) { |
369
|
11 |
|
$target = $target->{$segment}; |
370
|
|
|
} else { |
371
|
1 |
|
return value($default); |
372
|
|
|
} |
373
|
|
|
} |
374
|
|
|
|
375
|
39 |
|
return $target; |
376
|
|
|
} |
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
if (! function_exists('data_set')) { |
380
|
|
|
/** |
381
|
|
|
* Set an item on an array or object using dot notation. |
382
|
|
|
* |
383
|
|
|
* @param mixed $target |
384
|
|
|
* @param string|array $key |
385
|
|
|
* @param mixed $value |
386
|
|
|
* @param bool $overwrite |
387
|
|
|
* @return mixed |
388
|
|
|
*/ |
389
|
|
|
function data_set(&$target, $key, $value, $overwrite = true) |
390
|
|
|
{ |
391
|
|
|
$segments = is_array($key) ? $key : explode('.', $key); |
392
|
|
|
|
393
|
|
|
if (($segment = array_shift($segments)) === '*') { |
394
|
|
|
if (! Arr::accessible($target)) { |
395
|
|
|
$target = []; |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
if ($segments) { |
|
|
|
|
399
|
|
|
foreach ($target as &$inner) { |
400
|
|
|
data_set($inner, $segments, $value, $overwrite); |
401
|
|
|
} |
402
|
|
|
} elseif ($overwrite) { |
403
|
|
|
foreach ($target as &$inner) { |
404
|
|
|
$inner = $value; |
405
|
|
|
} |
406
|
|
|
} |
407
|
|
|
} elseif (Arr::accessible($target)) { |
408
|
|
|
if ($segments) { |
|
|
|
|
409
|
|
|
if (! Arr::exists($target, $segment)) { |
410
|
|
|
$target[$segment] = []; |
411
|
|
|
} |
412
|
|
|
|
413
|
|
|
data_set($target[$segment], $segments, $value, $overwrite); |
414
|
|
|
} elseif ($overwrite || ! Arr::exists($target, $segment)) { |
415
|
|
|
$target[$segment] = $value; |
416
|
|
|
} |
417
|
|
|
} elseif (is_object($target)) { |
418
|
|
|
if ($segments) { |
|
|
|
|
419
|
|
|
if (! isset($target->{$segment})) { |
420
|
|
|
$target->{$segment} = []; |
421
|
|
|
} |
422
|
|
|
|
423
|
|
|
data_set($target->{$segment}, $segments, $value, $overwrite); |
424
|
|
|
} elseif ($overwrite || ! isset($target->{$segment})) { |
425
|
|
|
$target->{$segment} = $value; |
426
|
|
|
} |
427
|
|
|
} else { |
428
|
|
|
$target = []; |
429
|
|
|
|
430
|
|
|
if ($segments) { |
|
|
|
|
431
|
|
|
data_set($target[$segment], $segments, $value, $overwrite); |
432
|
|
|
} elseif ($overwrite) { |
433
|
|
|
$target[$segment] = $value; |
434
|
|
|
} |
435
|
|
|
} |
436
|
|
|
|
437
|
|
|
return $target; |
438
|
|
|
} |
439
|
|
|
} |
440
|
|
|
|
441
|
|
|
if (! function_exists('dd')) { |
442
|
|
|
/** |
443
|
|
|
* Dump the passed variables and end the script. |
444
|
|
|
* |
445
|
|
|
* @param mixed $args |
446
|
|
|
* @return void |
447
|
|
|
*/ |
448
|
|
|
function dd(...$args) |
449
|
|
|
{ |
450
|
|
|
http_response_code(500); |
451
|
|
|
|
452
|
|
|
foreach ($args as $x) { |
453
|
|
|
(new Dumper)->dump($x); |
454
|
|
|
} |
455
|
|
|
|
456
|
|
|
die(1); |
457
|
|
|
} |
458
|
|
|
} |
459
|
|
|
|
460
|
|
|
if (! function_exists('value')) { |
461
|
|
|
/** |
462
|
|
|
* Return the default value of the given value. |
463
|
|
|
* |
464
|
|
|
* @param mixed $value |
465
|
|
|
* @return mixed |
466
|
|
|
*/ |
467
|
|
|
function value($value) |
468
|
|
|
{ |
469
|
13 |
|
return $value instanceof Closure ? $value() : $value; |
470
|
|
|
} |
471
|
|
|
} |
472
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.