1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
if (!function_exists('with')) { |
4
|
|
|
/** |
5
|
|
|
* Return the given object. Useful for chaining. |
6
|
|
|
* |
7
|
|
|
* @param mixed $object |
8
|
|
|
* |
9
|
|
|
* @return mixed |
10
|
|
|
*/ |
11
|
|
|
function with($object) |
12
|
|
|
{ |
13
|
|
|
return $object; |
14
|
|
|
} |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
if (!function_exists('array_set')) { |
18
|
|
|
/** |
19
|
|
|
* Set an array item to a given value using dot notation. |
20
|
|
|
* |
21
|
|
|
* @param array $array |
22
|
|
|
* @param string $key |
23
|
|
|
* @param mixed $value |
24
|
|
|
* |
25
|
|
|
* @return array |
26
|
|
|
*/ |
27
|
|
|
function array_set(&$array, $key, $value) |
28
|
|
|
{ |
29
|
|
|
if (is_null($key)) { |
30
|
|
|
return $array = $value; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
$keys = explode('.', $key); |
34
|
|
|
|
35
|
|
|
while (count($keys) > 1) { |
36
|
|
|
$key = array_shift($keys); |
37
|
|
|
|
38
|
|
|
if (!isset($array[$key]) || !is_array($array[$key])) { |
39
|
|
|
$array[$key] = []; |
40
|
|
|
} |
41
|
|
|
$array = &$array[$key]; |
42
|
|
|
} |
43
|
|
|
$array[array_shift($keys)] = $value; |
44
|
|
|
|
45
|
|
|
return $array; |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
if (!function_exists('array_get')) { |
50
|
|
|
/** |
51
|
|
|
* Get an item from an array using dot notation. |
52
|
|
|
* |
53
|
|
|
* @param array $array |
54
|
|
|
* @param string $key |
55
|
|
|
* @param mixed $default |
56
|
|
|
* |
57
|
|
|
* @return mixed |
58
|
|
|
*/ |
59
|
|
|
function array_get($array, $key, $default = null) |
60
|
|
|
{ |
61
|
|
|
if (is_null($key)) { |
62
|
|
|
return $array; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if (isset($array[$key])) { |
66
|
|
|
return $array[$key]; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
View Code Duplication |
foreach (explode('.', $key) as $segment) { |
|
|
|
|
70
|
|
|
if (!is_array($array) || !array_key_exists($segment, $array)) { |
71
|
|
|
return $default; |
72
|
|
|
} |
73
|
|
|
$array = $array[$segment]; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $array; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if (!function_exists('array_has')) { |
81
|
|
|
/** |
82
|
|
|
* Check if an item exists in an array using "dot" notation. |
83
|
|
|
* |
84
|
|
|
* @param array $array |
85
|
|
|
* @param string $key |
86
|
|
|
* |
87
|
|
|
* @return bool |
88
|
|
|
*/ |
89
|
|
|
function array_has($array, $key) |
90
|
|
|
{ |
91
|
|
|
if (empty($array) || is_null($key)) { |
92
|
|
|
return false; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
if (array_key_exists($key, $array)) { |
96
|
|
|
return true; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
View Code Duplication |
foreach (explode('.', $key) as $segment) { |
|
|
|
|
100
|
|
|
if (!is_array($array) || !array_key_exists($segment, $array)) { |
101
|
|
|
return false; |
102
|
|
|
} |
103
|
|
|
$array = $array[$segment]; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return true; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
if (!function_exists('array_first')) { |
111
|
|
|
/** |
112
|
|
|
* Return the first element in an array passing a given truth test. |
113
|
|
|
* |
114
|
|
|
* @param array $array |
115
|
|
|
* @param callable $callback |
116
|
|
|
* @param mixed $default |
117
|
|
|
* |
118
|
|
|
* @return mixed |
119
|
|
|
*/ |
120
|
|
|
function array_first($array, callable $callback, $default = null) |
121
|
|
|
{ |
122
|
|
|
foreach ($array as $key => $value) { |
123
|
|
|
if (call_user_func($callback, $key, $value)) { |
124
|
|
|
return $value; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return $default; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
if (!function_exists('array_last')) { |
133
|
|
|
/** |
134
|
|
|
* Return the last element in an array passing a given truth test. |
135
|
|
|
* |
136
|
|
|
* @param array $array |
137
|
|
|
* @param callable $callback |
138
|
|
|
* @param mixed $default |
139
|
|
|
* |
140
|
|
|
* @return mixed |
141
|
|
|
*/ |
142
|
|
|
function array_last($array, callable $callback, $default = null) |
143
|
|
|
{ |
144
|
|
|
return array_first(array_reverse($array), $callback, $default); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
if (!function_exists('array_flatten')) { |
149
|
|
|
/** |
150
|
|
|
* Flatten a multi-dimensional array into a single level. |
151
|
|
|
* |
152
|
|
|
* @param array $array |
153
|
|
|
* |
154
|
|
|
* @return array |
155
|
|
|
*/ |
156
|
|
|
function array_flatten($array) |
157
|
|
|
{ |
158
|
|
|
$return = []; |
159
|
|
|
|
160
|
|
|
array_walk_recursive($array, function ($x) use (&$return) { |
161
|
|
|
$return[] = $x; |
162
|
|
|
}); |
163
|
|
|
|
164
|
|
|
return $return; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
if (!function_exists('array_pluck')) { |
169
|
|
|
/** |
170
|
|
|
* Pluck an array of values from an array. |
171
|
|
|
* |
172
|
|
|
* @param array $array |
173
|
|
|
* @param string $value |
174
|
|
|
* @param string $key |
175
|
|
|
* |
176
|
|
|
* @return array |
177
|
|
|
*/ |
178
|
|
|
function array_pluck($array, $value, $key = null) |
179
|
|
|
{ |
180
|
|
|
$results = []; |
181
|
|
|
|
182
|
|
|
foreach ($array as $item) { |
183
|
|
|
$itemValue = data_get($item, $value); |
184
|
|
|
|
185
|
|
|
if (is_null($key)) { |
186
|
|
|
$results[] = $itemValue; |
187
|
|
|
} else { |
188
|
|
|
$itemKey = data_get($item, $key); |
189
|
|
|
|
190
|
|
|
$results[$itemKey] = $itemValue; |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
return $results; |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
if (!function_exists('data_get')) { |
199
|
|
|
/** |
200
|
|
|
* Get an item from an array or object using "dot" notation. |
201
|
|
|
* |
202
|
|
|
* @param mixed $target |
203
|
|
|
* @param string $key |
204
|
|
|
* @param mixed $default |
205
|
|
|
* |
206
|
|
|
* @return mixed |
207
|
|
|
*/ |
208
|
|
|
function data_get($target, $key, $default = null) |
209
|
|
|
{ |
210
|
|
|
if (is_null($key)) { |
211
|
|
|
return $target; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
foreach (explode('.', $key) as $segment) { |
215
|
|
|
if (is_array($target)) { |
216
|
|
|
if (!array_key_exists($segment, $target)) { |
217
|
|
|
return $default; |
218
|
|
|
} |
219
|
|
|
$target = $target[$segment]; |
220
|
|
|
} elseif ($target instanceof ArrayAccess) { |
221
|
|
|
if (!isset($target[$segment])) { |
222
|
|
|
return $default; |
223
|
|
|
} |
224
|
|
|
$target = $target[$segment]; |
225
|
|
|
} elseif (is_object($target)) { |
226
|
|
|
if (!isset($target->{$segment})) { |
227
|
|
|
return $default; |
228
|
|
|
} |
229
|
|
|
$target = $target->{$segment}; |
230
|
|
|
} else { |
231
|
|
|
return $default; |
232
|
|
|
} |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
return $target; |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
if (!function_exists('starts_with')) { |
240
|
|
|
/** |
241
|
|
|
* Determine if a given string starts with a given substring. |
242
|
|
|
* |
243
|
|
|
* @param string $haystack |
244
|
|
|
* @param string|array $needles |
245
|
|
|
* |
246
|
|
|
* @return bool |
247
|
|
|
*/ |
248
|
|
|
function starts_with($haystack, $needles) |
249
|
|
|
{ |
250
|
|
|
foreach ((array) $needles as $needle) { |
251
|
|
|
if ($needle != '' && strpos($haystack, $needle) === 0) { |
252
|
|
|
return true; |
253
|
|
|
} |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
return false; |
257
|
|
|
} |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
if (!function_exists('snake_case')) { |
261
|
|
|
/** |
262
|
|
|
* Convert a string to snake case. |
263
|
|
|
* |
264
|
|
|
* @param string $value |
265
|
|
|
* @param string $delimiter |
266
|
|
|
* |
267
|
|
|
* @return string |
268
|
|
|
*/ |
269
|
|
|
function snake_case($value, $delimiter = '_') |
270
|
|
|
{ |
271
|
|
|
if (!ctype_lower($value)) { |
272
|
|
|
$value = strtolower(preg_replace('/(.)(?=[A-Z])/', '$1'.$delimiter, $value)); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
return $value; |
276
|
|
|
} |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
if (!function_exists('camel_case')) { |
280
|
|
|
/** |
281
|
|
|
* Convert a value to camel case. |
282
|
|
|
* |
283
|
|
|
* @param string $value |
284
|
|
|
* |
285
|
|
|
* @return string |
286
|
|
|
*/ |
287
|
|
|
function camel_case($value) |
288
|
|
|
{ |
289
|
|
|
$value = ucwords(str_replace(['-', '_'], ' ', $value)); |
290
|
|
|
|
291
|
|
|
lcfirst(str_replace(' ', '', $value)); |
292
|
|
|
} |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
if (!function_exists('class_basename')) { |
296
|
|
|
/** |
297
|
|
|
* Get the class "basename" of the given object / class. |
298
|
|
|
* |
299
|
|
|
* @param string|object $class |
300
|
|
|
* |
301
|
|
|
* @return string |
302
|
|
|
*/ |
303
|
|
|
function class_basename($class) |
304
|
|
|
{ |
305
|
|
|
$class = is_object($class) ? get_class($class) : $class; |
306
|
|
|
|
307
|
|
|
return basename(str_replace('\\', '/', $class)); |
308
|
|
|
} |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
if (!function_exists('str_random')) { |
312
|
|
|
/** |
313
|
|
|
* Generate a more truly "random" alpha-numeric string. |
314
|
|
|
* |
315
|
|
|
* @param int $length |
316
|
|
|
* |
317
|
|
|
* @throws \RuntimeException |
318
|
|
|
* |
319
|
|
|
* @return string |
320
|
|
|
*/ |
321
|
|
|
function str_random($length = 16) |
322
|
|
|
{ |
323
|
|
|
$string = ''; |
324
|
|
|
|
325
|
|
|
while (($len = strlen($string)) < $length) { |
326
|
|
|
$size = $length - $len; |
327
|
|
|
|
328
|
|
|
$bytes = get_random_bytes($size); |
329
|
|
|
|
330
|
|
|
$string .= substr(str_replace(['/', '+', '='], '', base64_encode($bytes)), 0, $size); |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
return $string; |
334
|
|
|
} |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
if (!function_exists('get_random_bytes')) { |
338
|
|
|
/** |
339
|
|
|
* Generate a more truly "random" bytes. |
340
|
|
|
* |
341
|
|
|
* @param int $length |
342
|
|
|
* |
343
|
|
|
* @throws \RuntimeException |
344
|
|
|
* |
345
|
|
|
* @return string |
346
|
|
|
*/ |
347
|
|
|
function get_random_bytes($length = 16) |
348
|
|
|
{ |
349
|
|
|
if (PHP_MAJOR_VERSION >= 7) { |
350
|
|
|
$bytes = random_bytes($length); |
351
|
|
|
} elseif (function_exists('openssl_random_pseudo_bytes')) { |
352
|
|
|
$bytes = openssl_random_pseudo_bytes($length, $strong); |
353
|
|
|
|
354
|
|
|
if ($bytes === false || $strong === false) { |
355
|
|
|
throw new RuntimeException('Unable to generate random string.'); |
356
|
|
|
} |
357
|
|
|
} else { |
358
|
|
|
throw new RuntimeException('OpenSSL extension is required for PHP 5 users.'); |
359
|
|
|
} |
360
|
|
|
|
361
|
|
|
return $bytes; |
362
|
|
|
} |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
if (!function_exists('process')) { |
366
|
|
|
/** |
367
|
|
|
* Process the selected results. |
368
|
|
|
* |
369
|
|
|
* @param array $results |
370
|
|
|
* |
371
|
|
|
* @return array |
372
|
|
|
*/ |
373
|
|
|
function process($results) |
374
|
|
|
{ |
375
|
|
|
if (!isset($results) || isset($results['Fouttype'])) { |
376
|
|
|
return []; |
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
if (array_has($results, 'Items') || array_has($results, 'items')) { |
380
|
|
|
return reset($results); |
381
|
|
|
} |
382
|
|
|
|
383
|
|
|
foreach ($results as $result) { |
384
|
|
|
if (!is_array($result)) { |
385
|
|
|
return [$results]; |
386
|
|
|
} |
387
|
|
|
} |
388
|
|
|
|
389
|
|
|
return $results; |
390
|
|
|
} |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
if (!function_exists('translate')) { |
394
|
|
|
/** |
395
|
|
|
* Return the translation for the foreign. |
396
|
|
|
* |
397
|
|
|
* @param string $key |
|
|
|
|
398
|
|
|
* |
399
|
|
|
* @return string |
400
|
|
|
*/ |
401
|
|
|
function translate($foreign, $model) |
402
|
|
|
{ |
403
|
|
|
$translator = \App::make('translator'); |
404
|
|
|
|
405
|
|
|
if (!$translator->from($model)->hasTranslation($foreign)) { |
406
|
|
|
return $foreign; |
407
|
|
|
} |
408
|
|
|
|
409
|
|
|
return $translator->from($model)->translateForeign($foreign); |
410
|
|
|
} |
411
|
|
|
} |
412
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.