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
|
|
|
/** |
13
|
|
|
* Arr.php. |
14
|
|
|
* |
15
|
|
|
* @author overtrue <[email protected]> |
16
|
|
|
* @copyright 2015 overtrue <[email protected]> |
17
|
|
|
* |
18
|
|
|
* @link https://github.com/overtrue |
19
|
|
|
* @link http://overtrue.me |
20
|
|
|
* @link https://github.com/laravel/framework/blob/4.2/src/Illuminate/Support/Arr.php |
21
|
|
|
*/ |
22
|
|
|
namespace EasyWeChat\Support; |
23
|
|
|
|
24
|
|
|
use Closure; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Array helper from Illuminate\Support\Arr. |
28
|
|
|
*/ |
29
|
|
|
class Arr |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* Add an element to an array using "dot" notation if it doesn't exist. |
33
|
|
|
* |
34
|
|
|
* @param array $array |
35
|
|
|
* @param string $key |
36
|
|
|
* @param mixed $value |
37
|
|
|
* |
38
|
|
|
* @return array |
39
|
|
|
*/ |
40
|
|
|
public static function add($array, $key, $value) |
41
|
|
|
{ |
42
|
|
|
if (is_null(static::get($array, $key))) { |
43
|
|
|
static::set($array, $key, $value); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return $array; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Build a new array using a callback. |
51
|
|
|
* |
52
|
|
|
* @param array $array |
53
|
|
|
* @param \Closure $callback |
54
|
|
|
* |
55
|
|
|
* @return array |
56
|
|
|
*/ |
57
|
|
View Code Duplication |
public static function build($array, Closure $callback) |
58
|
|
|
{ |
59
|
|
|
$results = []; |
60
|
|
|
|
61
|
|
|
foreach ($array as $key => $value) { |
62
|
|
|
list($innerKey, $innerValue) = call_user_func($callback, $key, $value); |
63
|
|
|
$results[$innerKey] = $innerValue; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $results; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Divide an array into two arrays. One with keys and the other with values. |
71
|
|
|
* |
72
|
|
|
* @param array $array |
73
|
|
|
* |
74
|
|
|
* @return array |
75
|
|
|
*/ |
76
|
|
|
public static function divide($array) |
77
|
|
|
{ |
78
|
|
|
return [ |
79
|
|
|
array_keys($array), |
80
|
|
|
array_values($array), |
81
|
|
|
]; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Flatten a multi-dimensional associative array with dots. |
86
|
|
|
* |
87
|
|
|
* @param array $array |
88
|
|
|
* @param string $prepend |
89
|
|
|
* |
90
|
|
|
* @return array |
91
|
|
|
*/ |
92
|
|
|
public static function dot($array, $prepend = '') |
93
|
|
|
{ |
94
|
|
|
$results = []; |
95
|
|
|
|
96
|
|
|
foreach ($array as $key => $value) { |
97
|
|
|
if (is_array($value)) { |
98
|
|
|
$results = array_merge($results, static::dot($value, $prepend.$key.'.')); |
99
|
|
|
} else { |
100
|
|
|
$results[$prepend.$key] = $value; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $results; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Get all of the given array except for a specified array of items. |
109
|
|
|
* |
110
|
|
|
* @param array $array |
111
|
|
|
* @param array|string $keys |
112
|
|
|
* |
113
|
|
|
* @return array |
114
|
|
|
*/ |
115
|
|
|
public static function except($array, $keys) |
116
|
|
|
{ |
117
|
|
|
return array_diff_key($array, array_flip((array) $keys)); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Fetch a flattened array of a nested array element. |
122
|
|
|
* |
123
|
|
|
* @param array $array |
124
|
|
|
* @param string $key |
125
|
|
|
* |
126
|
|
|
* @return array |
127
|
|
|
*/ |
128
|
|
|
public static function fetch($array, $key) |
129
|
|
|
{ |
130
|
|
|
$results = []; |
131
|
|
|
|
132
|
|
|
foreach (explode('.', $key) as $segment) { |
133
|
|
|
$results = []; |
134
|
|
|
foreach ($array as $value) { |
135
|
|
|
$value = (array) $value; |
136
|
|
|
$results[] = $value[$segment]; |
137
|
|
|
} |
138
|
|
|
$array = array_values($results); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return array_values($results); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Return the first element in an array passing a given truth test. |
146
|
|
|
* |
147
|
|
|
* @param array $array |
148
|
|
|
* @param \Closure $callback |
149
|
|
|
* @param mixed $default |
150
|
|
|
* |
151
|
|
|
* @return mixed |
152
|
|
|
*/ |
153
|
|
|
public static function first($array, $callback, $default = null) |
154
|
|
|
{ |
155
|
|
|
foreach ($array as $key => $value) { |
156
|
|
|
if (call_user_func($callback, $key, $value)) { |
157
|
|
|
return $value; |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return $default; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Return the last element in an array passing a given truth test. |
166
|
|
|
* |
167
|
|
|
* @param array $array |
168
|
|
|
* @param \Closure $callback |
169
|
|
|
* @param mixed $default |
170
|
|
|
* |
171
|
|
|
* @return mixed |
172
|
|
|
*/ |
173
|
|
|
public static function last($array, $callback, $default = null) |
174
|
|
|
{ |
175
|
|
|
return static::first(array_reverse($array), $callback, $default); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Flatten a multi-dimensional array into a single level. |
180
|
|
|
* |
181
|
|
|
* @param array $array |
182
|
|
|
* |
183
|
|
|
* @return array |
184
|
|
|
*/ |
185
|
|
|
public static function flatten($array) |
186
|
|
|
{ |
187
|
|
|
$return = []; |
188
|
|
|
array_walk_recursive( |
189
|
|
|
$array, |
190
|
|
|
function ($x) use (&$return) { |
191
|
|
|
$return[] = $x; |
192
|
|
|
} |
193
|
|
|
); |
194
|
|
|
|
195
|
|
|
return $return; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Remove one or many array items from a given array using "dot" notation. |
200
|
|
|
* |
201
|
|
|
* @param array $array |
202
|
|
|
* @param array|string $keys |
203
|
|
|
*/ |
204
|
|
|
public static function forget(&$array, $keys) |
205
|
|
|
{ |
206
|
|
|
$original = &$array; |
207
|
|
|
|
208
|
|
|
foreach ((array) $keys as $key) { |
209
|
|
|
$parts = explode('.', $key); |
210
|
|
|
while (count($parts) > 1) { |
211
|
|
|
$part = array_shift($parts); |
212
|
|
|
if (isset($array[$part]) && is_array($array[$part])) { |
213
|
|
|
$array = &$array[$part]; |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
unset($array[array_shift($parts)]); |
217
|
|
|
// clean up after each pass |
218
|
|
|
$array = &$original; |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Get an item from an array using "dot" notation. |
224
|
|
|
* |
225
|
|
|
* @param array $array |
226
|
|
|
* @param string $key |
227
|
|
|
* @param mixed $default |
228
|
|
|
* |
229
|
|
|
* @return mixed |
230
|
|
|
*/ |
231
|
|
|
public static function get($array, $key, $default = null) |
232
|
|
|
{ |
233
|
|
|
if (is_null($key)) { |
234
|
|
|
return $array; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
if (isset($array[$key])) { |
238
|
|
|
return $array[$key]; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
foreach (explode('.', $key) as $segment) { |
242
|
|
|
if (!is_array($array) || !array_key_exists($segment, $array)) { |
243
|
|
|
return $default; |
244
|
|
|
} |
245
|
|
|
$array = $array[$segment]; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
return $array; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Get a subset of the items from the given array. |
253
|
|
|
* |
254
|
|
|
* @param array $array |
255
|
|
|
* @param array|string $keys |
256
|
|
|
* |
257
|
|
|
* @return array |
258
|
|
|
*/ |
259
|
|
|
public static function only($array, $keys) |
260
|
|
|
{ |
261
|
|
|
return array_intersect_key($array, array_flip((array) $keys)); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* Pluck an array of values from an array. |
266
|
|
|
* |
267
|
|
|
* @param array $array |
268
|
|
|
* @param string $value |
269
|
|
|
* @param string $key |
270
|
|
|
* |
271
|
|
|
* @return array |
272
|
|
|
*/ |
273
|
|
|
public static function pluck($array, $value, $key = null) |
274
|
|
|
{ |
275
|
|
|
$results = []; |
276
|
|
|
|
277
|
|
|
foreach ($array as $item) { |
278
|
|
|
$itemValue = is_object($item) ? $item->{$value} : $item[$value]; |
279
|
|
|
// If the key is "null", we will just append the value to the array and keep |
280
|
|
|
// looping. Otherwise we will key the array using the value of the key we |
281
|
|
|
// received from the developer. Then we'll return the final array form. |
282
|
|
|
if (is_null($key)) { |
283
|
|
|
$results[] = $itemValue; |
284
|
|
|
} else { |
285
|
|
|
$itemKey = is_object($item) ? $item->{$key} : $item[$key]; |
286
|
|
|
$results[$itemKey] = $itemValue; |
287
|
|
|
} |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
return $results; |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* Get a value from the array, and remove it. |
295
|
|
|
* |
296
|
|
|
* @param array $array |
297
|
|
|
* @param string $key |
298
|
|
|
* @param mixed $default |
299
|
|
|
* |
300
|
|
|
* @return mixed |
301
|
|
|
*/ |
302
|
|
|
public static function pull(&$array, $key, $default = null) |
303
|
|
|
{ |
304
|
|
|
$value = static::get($array, $key, $default); |
305
|
|
|
static::forget($array, $key); |
306
|
|
|
|
307
|
|
|
return $value; |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* Set an array item to a given value using "dot" notation. |
312
|
|
|
* |
313
|
|
|
* If no key is given to the method, the entire array will be replaced. |
314
|
|
|
* |
315
|
|
|
* @param array $array |
316
|
|
|
* @param string $key |
317
|
|
|
* @param mixed $value |
318
|
|
|
* |
319
|
|
|
* @return array |
320
|
|
|
*/ |
321
|
|
|
public static function set(&$array, $key, $value) |
322
|
|
|
{ |
323
|
|
|
if (is_null($key)) { |
324
|
|
|
return $array = $value; |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
$keys = explode('.', $key); |
328
|
|
|
|
329
|
|
|
while (count($keys) > 1) { |
330
|
|
|
$key = array_shift($keys); |
331
|
|
|
// If the key doesn't exist at this depth, we will just create an empty array |
332
|
|
|
// to hold the next value, allowing us to create the arrays to hold final |
333
|
|
|
// values at the correct depth. Then we'll keep digging into the array. |
334
|
|
|
if (!isset($array[$key]) || !is_array($array[$key])) { |
335
|
|
|
$array[$key] = []; |
336
|
|
|
} |
337
|
|
|
$array = &$array[$key]; |
338
|
|
|
} |
339
|
|
|
$array[array_shift($keys)] = $value; |
340
|
|
|
|
341
|
|
|
return $array; |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
/** |
345
|
|
|
* Sort the array using the given Closure. |
346
|
|
|
* |
347
|
|
|
* @param array $array |
348
|
|
|
* @param \Closure $callback |
349
|
|
|
* |
350
|
|
|
* @return array |
351
|
|
|
*/ |
352
|
|
|
public static function sort($array, Closure $callback) |
353
|
|
|
{ |
354
|
|
|
$results = []; |
355
|
|
|
|
356
|
|
|
foreach ($array as $key => $value) { |
357
|
|
|
$results[$key] = $callback($value); |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
return $results; |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
/** |
364
|
|
|
* Filter the array using the given Closure. |
365
|
|
|
* |
366
|
|
|
* @param array $array |
367
|
|
|
* @param \Closure $callback |
368
|
|
|
* |
369
|
|
|
* @return array |
370
|
|
|
*/ |
371
|
|
View Code Duplication |
public static function where($array, Closure $callback) |
372
|
|
|
{ |
373
|
|
|
$filtered = []; |
374
|
|
|
|
375
|
|
|
foreach ($array as $key => $value) { |
376
|
|
|
if (call_user_func($callback, $key, $value)) { |
377
|
|
|
$filtered[$key] = $value; |
378
|
|
|
} |
379
|
|
|
} |
380
|
|
|
|
381
|
|
|
return $filtered; |
382
|
|
|
} |
383
|
|
|
} |
384
|
|
|
|