1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the dingtalk. |
4
|
|
|
* User: Ilham Tahir <[email protected]> |
5
|
|
|
* This source file is subject to the MIT license that is bundled |
6
|
|
|
* with this source code in the file LICENSE. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Aplisin\DingTalk\Kernel\Support; |
10
|
|
|
|
11
|
|
|
class Arr |
12
|
|
|
{ |
13
|
|
|
public static function add(array $array, $key, $value) |
14
|
|
|
{ |
15
|
|
|
if (is_null(static::get($array, $key))) { |
16
|
|
|
static::set($array, $key, $value); |
17
|
|
|
} |
18
|
|
|
return $array; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public static function crossJoin(...$arrays) |
22
|
|
|
{ |
23
|
|
|
$results = [[]]; |
24
|
|
|
foreach ($arrays as $index => $array) { |
25
|
|
|
$append = []; |
26
|
|
|
foreach ($results as $product) { |
27
|
|
|
foreach ($array as $item) { |
28
|
|
|
$product[$index] = $item; |
29
|
|
|
$append[] = $product; |
|
|
|
|
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
$results = $append; |
33
|
|
|
} |
34
|
|
|
return $results; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public static function divide(array $array) |
38
|
|
|
{ |
39
|
|
|
return [array_keys($array), array_values($array)]; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public static function dot(array $array, $prepend = '') |
43
|
|
|
{ |
44
|
|
|
$results = []; |
45
|
|
|
foreach ($array as $key => $value) { |
46
|
|
|
if (is_array($value) && !empty($value)) { |
47
|
|
|
$results = array_merge($results, static::dot($value, $prepend.$key.'.')); |
48
|
|
|
} else { |
49
|
|
|
$results[$prepend.$key] = $value; |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
return $results; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public static function except(array $array, $keys) |
56
|
|
|
{ |
57
|
|
|
static::forget($array, $keys); |
58
|
|
|
return $array; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public static function exists(array $array, $key) |
62
|
|
|
{ |
63
|
|
|
return array_key_exists($key, $array); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public static function first(array $array, callable $callback = null, $default = null) |
67
|
|
|
{ |
68
|
|
|
if (is_null($callback)) { |
69
|
|
|
if (empty($array)) { |
70
|
|
|
return $default; |
71
|
|
|
} |
72
|
|
|
foreach ($array as $item) { |
73
|
|
|
return $item; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
foreach ($array as $key => $value) { |
77
|
|
|
if (call_user_func($callback, $value, $key)) { |
78
|
|
|
return $value; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
return $default; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public static function last(array $array, callable $callback = null, $default = null) |
85
|
|
|
{ |
86
|
|
|
if (is_null($callback)) { |
87
|
|
|
return empty($array) ? $default : end($array); |
88
|
|
|
} |
89
|
|
|
return static::first(array_reverse($array, true), $callback, $default); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public static function flatten(array $array, $depth = INF) |
93
|
|
|
{ |
94
|
|
|
return array_reduce($array, function($result, $item) use ($depth) { |
95
|
|
|
$item = $item instanceof Collection ? $item->all() : $item; |
96
|
|
|
if (!is_array($item)) { |
97
|
|
|
return array_merge($result, [$item]); |
98
|
|
|
} elseif (1 === $depth) { |
99
|
|
|
return array_merge($result, array_values($item)); |
100
|
|
|
} |
101
|
|
|
return array_merge($result, static::flatten($item, $depth - 1)); |
102
|
|
|
}, []); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public static function forget(array &$array, $keys) |
106
|
|
|
{ |
107
|
|
|
$original = &$array; |
108
|
|
|
$keys = (array) $keys; |
|
|
|
|
109
|
|
|
if (0 === count($keys)) { |
110
|
|
|
return; |
111
|
|
|
} |
112
|
|
|
foreach ($keys as $key) { |
113
|
|
|
// if the exact key exists in the top-level, remove it |
114
|
|
|
if (static::exists($array, $key)) { |
115
|
|
|
unset($array[$key]); |
116
|
|
|
continue; |
117
|
|
|
} |
118
|
|
|
$parts = explode('.', $key); |
119
|
|
|
// clean up before each pass |
120
|
|
|
$array = &$original; |
121
|
|
|
while (count($parts) > 1) { |
122
|
|
|
$part = array_shift($parts); |
123
|
|
|
if (isset($array[$part]) && is_array($array[$part])) { |
124
|
|
|
$array = &$array[$part]; |
125
|
|
|
} else { |
126
|
|
|
continue 2; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
unset($array[array_shift($parts)]); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public static function get(array $array, $key, $default = null) |
134
|
|
|
{ |
135
|
|
|
if (is_null($key)) { |
136
|
|
|
return $array; |
137
|
|
|
} |
138
|
|
|
if (static::exists($array, $key)) { |
139
|
|
|
return $array[$key]; |
140
|
|
|
} |
141
|
|
|
foreach (explode('.', $key) as $segment) { |
142
|
|
|
if (static::exists($array, $segment)) { |
143
|
|
|
$array = $array[$segment]; |
144
|
|
|
} else { |
145
|
|
|
return $default; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
return $array; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public static function has(array $array, $keys) |
152
|
|
|
{ |
153
|
|
|
if (self::isArrayUnValidated($array, $keys)) { |
154
|
|
|
return false; |
155
|
|
|
} |
156
|
|
|
foreach ($keys as $key) { |
157
|
|
|
$subKeyArray = $array; |
158
|
|
|
if (static::exists($array, $key)) { |
159
|
|
|
continue; |
160
|
|
|
} |
161
|
|
|
foreach (explode('.', $key) as $segment) { |
162
|
|
|
if (static::exists($subKeyArray, $segment)) { |
163
|
|
|
$subKeyArray = $subKeyArray[$segment]; |
164
|
|
|
} else { |
165
|
|
|
return false; |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
return true; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
protected static function isArrayUnValidated(array $array, $keys) |
173
|
|
|
{ |
174
|
|
|
if (is_null($keys)) { |
175
|
|
|
return true; |
176
|
|
|
} |
177
|
|
|
$keys = (array) $keys; |
178
|
|
|
if (empty($array)) { |
179
|
|
|
return true; |
180
|
|
|
} |
181
|
|
|
if ($keys === []) { |
182
|
|
|
return true; |
183
|
|
|
} |
184
|
|
|
return false; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
public static function isAssoc(array $array) |
188
|
|
|
{ |
189
|
|
|
$keys = array_keys($array); |
190
|
|
|
return array_keys($keys) !== $keys; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
public static function only(array $array, $keys) |
194
|
|
|
{ |
195
|
|
|
return array_intersect_key($array, array_flip((array) $keys)); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
public static function prepend(array $array, $value, $key = null) |
199
|
|
|
{ |
200
|
|
|
if (is_null($key)) { |
201
|
|
|
array_unshift($array, $value); |
202
|
|
|
} else { |
203
|
|
|
$array = [$key => $value] + $array; |
204
|
|
|
} |
205
|
|
|
return $array; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
public static function pull(array &$array, $key, $default = null) |
209
|
|
|
{ |
210
|
|
|
$value = static::get($array, $key, $default); |
211
|
|
|
static::forget($array, $key); |
212
|
|
|
return $value; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
public static function random(array $array, int $amount = null) |
216
|
|
|
{ |
217
|
|
|
if (is_null($amount)) { |
218
|
|
|
return $array[array_rand($array)]; |
219
|
|
|
} |
220
|
|
|
$keys = array_rand($array, $amount); |
|
|
|
|
221
|
|
|
$results = []; |
222
|
|
|
foreach ((array) $keys as $key) { |
223
|
|
|
$results[] = $array[$key]; |
224
|
|
|
} |
225
|
|
|
return $results; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
public static function set(array &$array, string $key, $value) |
229
|
|
|
{ |
230
|
|
|
$keys = explode('.', $key); |
231
|
|
|
while (count($keys) > 1) { |
232
|
|
|
$key = array_shift($keys); |
233
|
|
|
// If the key doesn't exist at this depth, we will just create an empty array |
234
|
|
|
// to hold the next value, allowing us to create the arrays to hold final |
235
|
|
|
// values at the correct depth. Then we'll keep digging into the array. |
236
|
|
|
if (!isset($array[$key]) || !is_array($array[$key])) { |
237
|
|
|
$array[$key] = []; |
238
|
|
|
} |
239
|
|
|
$array = &$array[$key]; |
240
|
|
|
} |
241
|
|
|
$array[array_shift($keys)] = $value; |
242
|
|
|
return $array; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
|
246
|
|
|
public static function where(array $array, callable $callback) |
247
|
|
|
{ |
248
|
|
|
return array_filter($array, $callback, ARRAY_FILTER_USE_BOTH); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
public static function wrap($value) |
252
|
|
|
{ |
253
|
|
|
return !is_array($value) ? [$value] : $value; |
254
|
|
|
} |
255
|
|
|
} |
256
|
|
|
|
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.