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