1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Laravel Helpers File |
5
|
|
|
* Extracted by Anthony Rappa |
6
|
|
|
* [email protected] |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
if ( ! function_exists('append_config')) |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Assign high numeric IDs to a config item to force appending. |
13
|
|
|
* |
14
|
|
|
* @param array $array |
15
|
|
|
* @return array |
16
|
|
|
*/ |
17
|
|
|
function append_config(array $array) |
18
|
|
|
{ |
19
|
|
|
$start = 9999; |
20
|
|
|
|
21
|
|
|
foreach ($array as $key => $value) |
22
|
|
|
{ |
23
|
|
|
if (is_numeric($key)) |
24
|
|
|
{ |
25
|
|
|
$start++; |
26
|
|
|
|
27
|
|
|
$array[$start] = array_pull($array, $key); |
28
|
|
|
} |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
return $array; |
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
if ( ! function_exists('array_add')) |
36
|
|
|
{ |
37
|
|
|
/** |
38
|
|
|
* Add an element to an array using "dot" notation if it doesn't exist. |
39
|
|
|
* |
40
|
|
|
* @param array $array |
41
|
|
|
* @param string $key |
42
|
|
|
* @param mixed $value |
43
|
|
|
* @return array |
44
|
|
|
*/ |
45
|
|
|
function array_add($array, $key, $value) |
46
|
|
|
{ |
47
|
|
|
if (is_null(get($array, $key))) |
48
|
|
|
{ |
49
|
|
|
set($array, $key, $value); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return $array; |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
View Code Duplication |
if ( ! function_exists('array_build')) |
|
|
|
|
57
|
|
|
{ |
58
|
|
|
/** |
59
|
|
|
* Build a new array using a callback. |
60
|
|
|
* |
61
|
|
|
* @param array $array |
62
|
|
|
* @param \Closure $callback |
63
|
|
|
* @return array |
64
|
|
|
*/ |
65
|
|
|
function array_build($array, Closure $callback) |
66
|
|
|
{ |
67
|
|
|
$results = array(); |
68
|
|
|
|
69
|
|
|
foreach ($array as $key => $value) |
70
|
|
|
{ |
71
|
|
|
list($innerKey, $innerValue) = call_user_func($callback, $key, $value); |
72
|
|
|
|
73
|
|
|
$results[$innerKey] = $innerValue; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $results; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if ( ! function_exists('array_divide')) |
81
|
|
|
{ |
82
|
|
|
/** |
83
|
|
|
* Divide an array into two arrays. One with keys and the other with values. |
84
|
|
|
* |
85
|
|
|
* @param array $array |
86
|
|
|
* @return array |
87
|
|
|
*/ |
88
|
|
|
function array_divide($array) |
89
|
|
|
{ |
90
|
|
|
return array(array_keys($array), array_values($array)); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
View Code Duplication |
if ( ! function_exists('array_dot')) |
|
|
|
|
95
|
|
|
{ |
96
|
|
|
/** |
97
|
|
|
* Flatten a multi-dimensional associative array with dots. |
98
|
|
|
* |
99
|
|
|
* @param array $array |
100
|
|
|
* @param string $prepend |
101
|
|
|
* @return array |
102
|
|
|
*/ |
103
|
|
|
function array_dot($array, $prepend = '') |
104
|
|
|
{ |
105
|
|
|
$results = array(); |
106
|
|
|
|
107
|
|
|
foreach ($array as $key => $value) |
108
|
|
|
{ |
109
|
|
|
if (is_array($value)) |
110
|
|
|
{ |
111
|
|
|
$results = array_merge($results, dot($value, $prepend.$key.'.')); |
112
|
|
|
} |
113
|
|
|
else |
114
|
|
|
{ |
115
|
|
|
$results[$prepend.$key] = $value; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $results; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
if ( ! function_exists('array_except')) |
124
|
|
|
{ |
125
|
|
|
/** |
126
|
|
|
* Get all of the given array except for a specified array of items. |
127
|
|
|
* |
128
|
|
|
* @param array $array |
129
|
|
|
* @param array|string $keys |
130
|
|
|
* @return array |
131
|
|
|
*/ |
132
|
|
|
function array_except($array, $keys) |
133
|
|
|
{ |
134
|
|
|
return array_diff_key($array, array_flip((array) $keys)); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
if ( ! function_exists('array_fetch')) |
139
|
|
|
{ |
140
|
|
|
/** |
141
|
|
|
* Fetch a flattened array of a nested array element. |
142
|
|
|
* |
143
|
|
|
* @param array $array |
144
|
|
|
* @param string $key |
145
|
|
|
* @return array |
146
|
|
|
*/ |
147
|
|
|
function array_fetch($array, $key) |
148
|
|
|
{ |
149
|
|
|
$results = array(); |
150
|
|
|
|
151
|
|
|
foreach (explode('.', $key) as $segment) |
152
|
|
|
{ |
153
|
|
|
foreach ($array as $value) |
154
|
|
|
{ |
155
|
|
|
if (array_key_exists($segment, $value = (array) $value)) |
156
|
|
|
{ |
157
|
|
|
$results[] = $value[$segment]; |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
$array = array_values($results); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
return array_values($results); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
168
|
|
View Code Duplication |
if ( ! function_exists('array_first')) |
|
|
|
|
169
|
|
|
{ |
170
|
|
|
/** |
171
|
|
|
* Return the first element in an array passing a given truth test. |
172
|
|
|
* |
173
|
|
|
* @param array $array |
174
|
|
|
* @param \Closure $callback |
175
|
|
|
* @param mixed $default |
176
|
|
|
* @return mixed |
177
|
|
|
*/ |
178
|
|
|
function array_first($array, $callback, $default = null) |
179
|
|
|
{ |
180
|
|
|
foreach ($array as $key => $value) |
181
|
|
|
{ |
182
|
|
|
if (call_user_func($callback, $key, $value)) return $value; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
return value($default); |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
if ( ! function_exists('array_last')) |
190
|
|
|
{ |
191
|
|
|
/** |
192
|
|
|
* Return the last element in an array passing a given truth test. |
193
|
|
|
* |
194
|
|
|
* @param array $array |
195
|
|
|
* @param \Closure $callback |
196
|
|
|
* @param mixed $default |
197
|
|
|
* @return mixed |
198
|
|
|
*/ |
199
|
|
|
function array_last($array, $callback, $default = null) |
200
|
|
|
{ |
201
|
|
|
return first(array_reverse($array), $callback, $default); |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
if ( ! function_exists('array_flatten')) |
206
|
|
|
{ |
207
|
|
|
/** |
208
|
|
|
* Flatten a multi-dimensional array into a single level. |
209
|
|
|
* |
210
|
|
|
* @param array $array |
211
|
|
|
* @return array |
212
|
|
|
*/ |
213
|
|
|
function array_flatten($array) |
214
|
|
|
{ |
215
|
|
|
$return = array(); |
216
|
|
|
|
217
|
|
|
array_walk_recursive($array, function($x) use (&$return) { $return[] = $x; }); |
218
|
|
|
|
219
|
|
|
return $return; |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
|
223
|
|
View Code Duplication |
if ( ! function_exists('array_forget')) |
|
|
|
|
224
|
|
|
{ |
225
|
|
|
/** |
226
|
|
|
* Remove one or many array items from a given array using "dot" notation. |
227
|
|
|
* |
228
|
|
|
* @param array $array |
229
|
|
|
* @param array|string $keys |
230
|
|
|
* @return void |
231
|
|
|
*/ |
232
|
|
|
function array_forget(&$array, $keys) |
233
|
|
|
{ |
234
|
|
|
$original =& $array; |
235
|
|
|
|
236
|
|
|
foreach ((array) $keys as $key) |
237
|
|
|
{ |
238
|
|
|
$parts = explode('.', $key); |
239
|
|
|
|
240
|
|
|
while (count($parts) > 1) |
241
|
|
|
{ |
242
|
|
|
$part = array_shift($parts); |
243
|
|
|
|
244
|
|
|
if (isset($array[$part]) && is_array($array[$part])) |
245
|
|
|
{ |
246
|
|
|
$array =& $array[$part]; |
247
|
|
|
} |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
unset($array[array_shift($parts)]); |
251
|
|
|
|
252
|
|
|
// clean up after each pass |
253
|
|
|
$array =& $original; |
254
|
|
|
} |
255
|
|
|
} |
256
|
|
|
} |
257
|
|
|
|
258
|
|
View Code Duplication |
if ( ! function_exists('array_get')) |
|
|
|
|
259
|
|
|
{ |
260
|
|
|
/** |
261
|
|
|
* Get an item from an array using "dot" notation. |
262
|
|
|
* |
263
|
|
|
* @param array $array |
264
|
|
|
* @param string $key |
265
|
|
|
* @param mixed $default |
266
|
|
|
* @return mixed |
267
|
|
|
*/ |
268
|
|
|
function array_get($array, $key, $default = null) |
269
|
|
|
{ |
270
|
6 |
|
if (is_null($key)) return $array; |
271
|
|
|
|
272
|
6 |
|
if (isset($array[$key])) return $array[$key]; |
273
|
|
|
|
274
|
5 |
|
foreach (explode('.', $key) as $segment) |
275
|
|
|
{ |
276
|
5 |
|
if ( ! is_array($array) || ! array_key_exists($segment, $array)) |
277
|
|
|
{ |
278
|
5 |
|
return value($default); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
$array = $array[$segment]; |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
return $array; |
285
|
|
|
} |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
if ( ! function_exists('array_has')) |
289
|
|
|
{ |
290
|
|
|
/** |
291
|
|
|
* Check if an item exists in an array using "dot" notation. |
292
|
|
|
* |
293
|
|
|
* @param array $array |
294
|
|
|
* @param string $key |
295
|
|
|
* @return bool |
296
|
|
|
*/ |
297
|
|
|
function array_has($array, $key) |
298
|
|
|
{ |
299
|
|
|
if (empty($array) || is_null($key)) return false; |
300
|
|
|
|
301
|
|
|
if (array_key_exists($key, $array)) return true; |
302
|
|
|
|
303
|
|
|
foreach (explode('.', $key) as $segment) |
304
|
|
|
{ |
305
|
|
|
if ( ! is_array($array) || ! array_key_exists($segment, $array)) |
306
|
|
|
{ |
307
|
|
|
return false; |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
$array = $array[$segment]; |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
return true; |
314
|
|
|
} |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
if ( ! function_exists('array_only')) |
318
|
|
|
{ |
319
|
|
|
/** |
320
|
|
|
* Get a subset of the items from the given array. |
321
|
|
|
* |
322
|
|
|
* @param array $array |
323
|
|
|
* @param array|string $keys |
324
|
|
|
* @return array |
325
|
|
|
*/ |
326
|
|
|
function array_only($array, $keys) |
327
|
|
|
{ |
328
|
|
|
return array_intersect_key($array, array_flip((array) $keys)); |
329
|
|
|
} |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
if ( ! function_exists('array_pluck')) |
333
|
|
|
{ |
334
|
|
|
/** |
335
|
|
|
* Pluck an array of values from an array. |
336
|
|
|
* |
337
|
|
|
* @param array $array |
338
|
|
|
* @param string $value |
339
|
|
|
* @param string $key |
340
|
|
|
* @return array |
341
|
|
|
*/ |
342
|
|
|
function array_pluck($array, $value, $key = null) |
343
|
|
|
{ |
344
|
|
|
$results = array(); |
345
|
|
|
|
346
|
|
|
foreach ($array as $item) |
347
|
|
|
{ |
348
|
|
|
$itemValue = data_get($item, $value); |
349
|
|
|
|
350
|
|
|
// If the key is "null", we will just append the value to the array and keep |
351
|
|
|
// looping. Otherwise we will key the array using the value of the key we |
352
|
|
|
// received from the developer. Then we'll return the final array form. |
353
|
|
|
if (is_null($key)) |
354
|
|
|
{ |
355
|
|
|
$results[] = $itemValue; |
356
|
|
|
} |
357
|
|
|
else |
358
|
|
|
{ |
359
|
|
|
$itemKey = data_get($item, $key); |
360
|
|
|
|
361
|
|
|
$results[$itemKey] = $itemValue; |
362
|
|
|
} |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
return $results; |
366
|
|
|
} |
367
|
|
|
} |
368
|
|
|
|
369
|
|
View Code Duplication |
if ( ! function_exists('array_pull')) |
|
|
|
|
370
|
|
|
{ |
371
|
|
|
/** |
372
|
|
|
* Get a value from the array, and remove it. |
373
|
|
|
* |
374
|
|
|
* @param array $array |
375
|
|
|
* @param string $key |
376
|
|
|
* @param mixed $default |
377
|
|
|
* @return mixed |
378
|
|
|
*/ |
379
|
|
|
function array_pull(&$array, $key, $default = null) |
380
|
|
|
{ |
381
|
|
|
$value = get($array, $key, $default); |
382
|
|
|
|
383
|
|
|
forget($array, $key); |
384
|
|
|
|
385
|
|
|
return $value; |
386
|
|
|
} |
387
|
|
|
} |
388
|
|
|
|
389
|
|
View Code Duplication |
if ( ! function_exists('array_set')) |
|
|
|
|
390
|
|
|
{ |
391
|
|
|
/** |
392
|
|
|
* Set an array item to a given value using "dot" notation. |
393
|
|
|
* |
394
|
|
|
* If no key is given to the method, the entire array will be replaced. |
395
|
|
|
* |
396
|
|
|
* @param array $array |
397
|
|
|
* @param string $key |
398
|
|
|
* @param mixed $value |
399
|
|
|
* @return array |
400
|
|
|
*/ |
401
|
|
|
function array_set(&$array, $key, $value) |
402
|
|
|
{ |
403
|
|
|
if (is_null($key)) return $array = $value; |
404
|
|
|
|
405
|
|
|
$keys = explode('.', $key); |
406
|
|
|
|
407
|
|
|
while (count($keys) > 1) |
408
|
|
|
{ |
409
|
|
|
$key = array_shift($keys); |
410
|
|
|
|
411
|
|
|
// If the key doesn't exist at this depth, we will just create an empty array |
412
|
|
|
// to hold the next value, allowing us to create the arrays to hold final |
413
|
|
|
// values at the correct depth. Then we'll keep digging into the array. |
414
|
|
|
if ( ! isset($array[$key]) || ! is_array($array[$key])) |
415
|
|
|
{ |
416
|
|
|
$array[$key] = array(); |
417
|
|
|
} |
418
|
|
|
|
419
|
|
|
$array =& $array[$key]; |
420
|
|
|
} |
421
|
|
|
|
422
|
|
|
$array[array_shift($keys)] = $value; |
423
|
|
|
|
424
|
|
|
return $array; |
425
|
|
|
} |
426
|
|
|
} |
427
|
|
|
|
428
|
|
View Code Duplication |
if ( ! function_exists('array_where')) |
|
|
|
|
429
|
|
|
{ |
430
|
|
|
/** |
431
|
|
|
* Filter the array using the given Closure. |
432
|
|
|
* |
433
|
|
|
* @param array $array |
434
|
|
|
* @param \Closure $callback |
435
|
|
|
* @return array |
436
|
|
|
*/ |
437
|
|
|
function array_where($array, Closure $callback) |
438
|
|
|
{ |
439
|
|
|
$filtered = array(); |
440
|
|
|
|
441
|
|
|
foreach ($array as $key => $value) |
442
|
|
|
{ |
443
|
|
|
if (call_user_func($callback, $key, $value)) $filtered[$key] = $value; |
444
|
|
|
} |
445
|
|
|
|
446
|
|
|
return $filtered; |
447
|
|
|
} |
448
|
|
|
} |
449
|
|
|
|
450
|
|
|
if ( ! function_exists('camel_case')) |
451
|
|
|
{ |
452
|
|
|
/** |
453
|
|
|
* Convert a value to camel case. |
454
|
|
|
* |
455
|
|
|
* @param string $value |
456
|
|
|
* @return string |
457
|
|
|
*/ |
458
|
|
|
function camel_case($value) |
459
|
|
|
{ |
460
|
|
|
$camelCache = []; |
461
|
|
|
|
462
|
|
|
if (isset($camelCache[$value])) |
463
|
|
|
{ |
464
|
|
|
return $camelCache[$value]; |
465
|
|
|
} |
466
|
|
|
|
467
|
|
|
return $camelCache[$value] = lcfirst(studly($value)); |
468
|
|
|
} |
469
|
|
|
} |
470
|
|
|
|
471
|
|
|
if ( ! function_exists('class_basename')) |
472
|
|
|
{ |
473
|
|
|
/** |
474
|
|
|
* Get the class "basename" of the given object / class. |
475
|
|
|
* |
476
|
|
|
* @param string|object $class |
477
|
|
|
* @return string |
478
|
|
|
*/ |
479
|
|
|
function class_basename($class) |
480
|
|
|
{ |
481
|
|
|
$class = is_object($class) ? get_class($class) : $class; |
482
|
|
|
|
483
|
|
|
return basename(str_replace('\\', '/', $class)); |
484
|
|
|
} |
485
|
|
|
} |
486
|
|
|
|
487
|
|
|
if ( ! function_exists('class_uses_recursive')) |
488
|
|
|
{ |
489
|
|
|
/** |
490
|
|
|
* Returns all traits used by a class, it's subclasses and trait of their traits |
491
|
|
|
* |
492
|
|
|
* @param string $class |
493
|
|
|
* @return array |
494
|
|
|
*/ |
495
|
|
|
function class_uses_recursive($class) |
496
|
|
|
{ |
497
|
|
|
$results = []; |
498
|
|
|
|
499
|
|
|
foreach (array_merge([$class => $class], class_parents($class)) as $class) |
500
|
|
|
{ |
501
|
|
|
$results += trait_uses_recursive($class); |
502
|
|
|
} |
503
|
|
|
|
504
|
|
|
return array_unique($results); |
505
|
|
|
} |
506
|
|
|
} |
507
|
|
|
|
508
|
|
|
if ( ! function_exists('data_get')) |
509
|
|
|
{ |
510
|
|
|
/** |
511
|
|
|
* Get an item from an array or object using "dot" notation. |
512
|
|
|
* |
513
|
|
|
* @param mixed $target |
514
|
|
|
* @param string $key |
515
|
|
|
* @param mixed $default |
516
|
|
|
* @return mixed |
517
|
|
|
*/ |
518
|
|
|
function data_get($target, $key, $default = null) |
519
|
|
|
{ |
520
|
|
|
if (is_null($key)) return $target; |
521
|
|
|
|
522
|
|
|
foreach (explode('.', $key) as $segment) |
523
|
|
|
{ |
524
|
|
|
if (is_array($target)) |
525
|
|
|
{ |
526
|
|
|
if ( ! array_key_exists($segment, $target)) |
527
|
|
|
{ |
528
|
|
|
return value($default); |
529
|
|
|
} |
530
|
|
|
|
531
|
|
|
$target = $target[$segment]; |
532
|
|
|
} |
533
|
|
|
elseif ($target instanceof ArrayAccess) |
534
|
|
|
{ |
535
|
|
|
if ( ! isset($target[$segment])) |
536
|
|
|
{ |
537
|
|
|
return value($default); |
538
|
|
|
} |
539
|
|
|
|
540
|
|
|
$target = $target[$segment]; |
541
|
|
|
} |
542
|
|
|
elseif (is_object($target)) |
543
|
|
|
{ |
544
|
|
|
if ( ! isset($target->{$segment})) |
545
|
|
|
{ |
546
|
|
|
return value($default); |
547
|
|
|
} |
548
|
|
|
|
549
|
|
|
$target = $target->{$segment}; |
550
|
|
|
} |
551
|
|
|
else |
552
|
|
|
{ |
553
|
|
|
return value($default); |
554
|
|
|
} |
555
|
|
|
} |
556
|
|
|
|
557
|
|
|
return $target; |
558
|
|
|
} |
559
|
|
|
} |
560
|
|
|
|
561
|
|
|
if ( ! function_exists('e')) |
562
|
|
|
{ |
563
|
|
|
/** |
564
|
|
|
* Escape HTML entities in a string. |
565
|
|
|
* |
566
|
|
|
* @param string $value |
567
|
|
|
* @return string |
568
|
|
|
*/ |
569
|
|
|
function e($value) |
570
|
|
|
{ |
571
|
|
|
return htmlentities($value, ENT_QUOTES, 'UTF-8', false); |
572
|
|
|
} |
573
|
|
|
} |
574
|
|
|
|
575
|
|
|
if ( ! function_exists('ends_with')) |
576
|
|
|
{ |
577
|
|
|
/** |
578
|
|
|
* Determine if a given string ends with a given substring. |
579
|
|
|
* |
580
|
|
|
* @param string $haystack |
581
|
|
|
* @param string|array $needles |
582
|
|
|
* @return bool |
583
|
|
|
*/ |
584
|
|
|
function ends_with($haystack, $needles) |
585
|
|
|
{ |
586
|
|
|
foreach ((array) $needles as $needle) |
587
|
|
|
{ |
588
|
|
|
if ((string) $needle === substr($haystack, -strlen($needle))) return true; |
589
|
|
|
} |
590
|
|
|
|
591
|
|
|
return false; |
592
|
|
|
} |
593
|
|
|
} |
594
|
|
|
|
595
|
|
|
if ( ! function_exists('head')) |
596
|
|
|
{ |
597
|
|
|
/** |
598
|
|
|
* Get the first element of an array. Useful for method chaining. |
599
|
|
|
* |
600
|
|
|
* @param array $array |
601
|
|
|
* @return mixed |
602
|
|
|
*/ |
603
|
|
|
function head($array) |
604
|
|
|
{ |
605
|
|
|
return reset($array); |
606
|
|
|
} |
607
|
|
|
} |
608
|
|
|
|
609
|
|
|
if ( ! function_exists('last')) |
610
|
|
|
{ |
611
|
|
|
/** |
612
|
|
|
* Get the last element from an array. |
613
|
|
|
* |
614
|
|
|
* @param array $array |
615
|
|
|
* @return mixed |
616
|
|
|
*/ |
617
|
|
|
function last($array) |
618
|
|
|
{ |
619
|
|
|
return end($array); |
620
|
|
|
} |
621
|
|
|
} |
622
|
|
|
|
623
|
|
|
if ( ! function_exists('object_get')) |
624
|
|
|
{ |
625
|
|
|
/** |
626
|
|
|
* Get an item from an object using "dot" notation. |
627
|
|
|
* |
628
|
|
|
* @param object $object |
629
|
|
|
* @param string $key |
630
|
|
|
* @param mixed $default |
631
|
|
|
* @return mixed |
632
|
|
|
*/ |
633
|
|
|
function object_get($object, $key, $default = null) |
634
|
|
|
{ |
635
|
|
|
if (is_null($key) || trim($key) == '') return $object; |
636
|
|
|
|
637
|
|
|
foreach (explode('.', $key) as $segment) |
638
|
|
|
{ |
639
|
|
|
if ( ! is_object($object) || ! isset($object->{$segment})) |
640
|
|
|
{ |
641
|
|
|
return value($default); |
642
|
|
|
} |
643
|
|
|
|
644
|
|
|
$object = $object->{$segment}; |
645
|
|
|
} |
646
|
|
|
|
647
|
|
|
return $object; |
648
|
|
|
} |
649
|
|
|
} |
650
|
|
|
|
651
|
|
|
if ( ! function_exists('preg_replace_sub')) |
652
|
|
|
{ |
653
|
|
|
/** |
654
|
|
|
* Replace a given pattern with each value in the array in sequentially. |
655
|
|
|
* |
656
|
|
|
* @param string $pattern |
657
|
|
|
* @param array $replacements |
658
|
|
|
* @param string $subject |
659
|
|
|
* @return string |
660
|
|
|
*/ |
661
|
|
|
function preg_replace_sub($pattern, &$replacements, $subject) |
662
|
|
|
{ |
663
|
|
|
return preg_replace_callback($pattern, function() use (&$replacements) |
664
|
|
|
{ |
665
|
|
|
return array_shift($replacements); |
666
|
|
|
|
667
|
|
|
}, $subject); |
668
|
|
|
} |
669
|
|
|
} |
670
|
|
|
|
671
|
|
|
if ( ! function_exists('snake_case')) |
672
|
|
|
{ |
673
|
|
|
/** |
674
|
|
|
* Convert a string to snake case. |
675
|
|
|
* |
676
|
|
|
* @param string $value |
677
|
|
|
* @param string $delimiter |
678
|
|
|
* @return string |
679
|
|
|
*/ |
680
|
|
|
function snake_case($value, $delimiter = '_') |
681
|
|
|
{ |
682
|
3 |
|
$snakeCache = []; |
683
|
3 |
|
$key = $value.$delimiter; |
684
|
|
|
|
685
|
3 |
|
if (isset($snakeCache[$key])) |
686
|
|
|
{ |
687
|
|
|
return $snakeCache[$key]; |
688
|
|
|
} |
689
|
|
|
|
690
|
3 |
|
if ( ! ctype_lower($value)) |
691
|
|
|
{ |
692
|
3 |
|
$value = strtolower(preg_replace('/(.)(?=[A-Z])/', '$1'.$delimiter, $value)); |
693
|
|
|
} |
694
|
|
|
|
695
|
3 |
|
return $snakeCache[$key] = $value; |
696
|
|
|
} |
697
|
|
|
} |
698
|
|
|
|
699
|
|
View Code Duplication |
if ( ! function_exists('starts_with')) |
|
|
|
|
700
|
|
|
{ |
701
|
|
|
/** |
702
|
|
|
* Determine if a given string starts with a given substring. |
703
|
|
|
* |
704
|
|
|
* @param string $haystack |
705
|
|
|
* @param string|array $needles |
706
|
|
|
* @return bool |
707
|
|
|
*/ |
708
|
|
|
function starts_with($haystack, $needles) |
709
|
|
|
{ |
710
|
|
|
foreach ((array) $needles as $needle) |
711
|
|
|
{ |
712
|
|
|
if ($needle != '' && strpos($haystack, $needle) === 0) return true; |
713
|
|
|
} |
714
|
|
|
|
715
|
|
|
return false; |
716
|
|
|
} |
717
|
|
|
} |
718
|
|
|
|
719
|
|
View Code Duplication |
if ( ! function_exists('str_contains')) |
|
|
|
|
720
|
|
|
{ |
721
|
|
|
/** |
722
|
|
|
* Determine if a given string contains a given substring. |
723
|
|
|
* |
724
|
|
|
* @param string $haystack |
725
|
|
|
* @param string|array $needles |
726
|
|
|
* @return bool |
727
|
|
|
*/ |
728
|
|
|
function str_contains($haystack, $needles) |
729
|
|
|
{ |
730
|
|
|
foreach ((array) $needles as $needle) |
731
|
|
|
{ |
732
|
|
|
if ($needle != '' && strpos($haystack, $needle) !== false) return true; |
733
|
|
|
} |
734
|
|
|
|
735
|
|
|
return false; |
736
|
|
|
} |
737
|
|
|
} |
738
|
|
|
|
739
|
|
|
if ( ! function_exists('str_finish')) |
740
|
|
|
{ |
741
|
|
|
/** |
742
|
|
|
* Cap a string with a single instance of a given value. |
743
|
|
|
* |
744
|
|
|
* @param string $value |
745
|
|
|
* @param string $cap |
746
|
|
|
* @return string |
747
|
|
|
*/ |
748
|
|
|
function str_finish($value, $cap) |
749
|
|
|
{ |
750
|
|
|
$quoted = preg_quote($cap, '/'); |
751
|
|
|
|
752
|
|
|
return preg_replace('/(?:'.$quoted.')+$/', '', $value).$cap; |
753
|
|
|
} |
754
|
|
|
} |
755
|
|
|
|
756
|
|
|
if ( ! function_exists('str_is')) |
757
|
|
|
{ |
758
|
|
|
/** |
759
|
|
|
* Determine if a given string matches a given pattern. |
760
|
|
|
* |
761
|
|
|
* @param string $pattern |
762
|
|
|
* @param string $value |
763
|
|
|
* @return bool |
764
|
|
|
*/ |
765
|
|
|
function str_is($pattern, $value) |
766
|
|
|
{ |
767
|
|
|
if ($pattern == $value) return true; |
768
|
|
|
|
769
|
|
|
$pattern = preg_quote($pattern, '#'); |
770
|
|
|
|
771
|
|
|
// Asterisks are translated into zero-or-more regular expression wildcards |
772
|
|
|
// to make it convenient to check if the strings starts with the given |
773
|
|
|
// pattern such as "library/*", making any string check convenient. |
774
|
|
|
$pattern = str_replace('\*', '.*', $pattern).'\z'; |
775
|
|
|
|
776
|
|
|
return (bool) preg_match('#^'.$pattern.'#', $value); |
777
|
|
|
} |
778
|
|
|
} |
779
|
|
|
|
780
|
|
|
if ( ! function_exists('str_limit')) |
781
|
|
|
{ |
782
|
|
|
/** |
783
|
|
|
* Limit the number of characters in a string. |
784
|
|
|
* |
785
|
|
|
* @param string $value |
786
|
|
|
* @param int $limit |
787
|
|
|
* @param string $end |
788
|
|
|
* @return string |
789
|
|
|
*/ |
790
|
|
|
function str_limit($value, $limit = 100, $end = '...') |
791
|
|
|
{ |
792
|
|
|
if (mb_strlen($value) <= $limit) return $value; |
793
|
|
|
|
794
|
|
|
return rtrim(mb_substr($value, 0, $limit, 'UTF-8')).$end; |
795
|
|
|
} |
796
|
|
|
} |
797
|
|
|
|
798
|
|
|
if ( ! function_exists('str_random')) |
799
|
|
|
{ |
800
|
|
|
/** |
801
|
|
|
* Generate a more truly "random" alpha-numeric string. |
802
|
|
|
* |
803
|
|
|
* @param int $length |
804
|
|
|
* @return string |
805
|
|
|
* |
806
|
|
|
* @throws \RuntimeException |
807
|
|
|
*/ |
808
|
|
|
function str_random($length = 16) |
809
|
|
|
{ |
810
|
|
|
if ( ! function_exists('openssl_random_pseudo_bytes')) |
811
|
|
|
{ |
812
|
|
|
throw new RuntimeException('OpenSSL extension is required.'); |
813
|
|
|
} |
814
|
|
|
|
815
|
|
|
$bytes = openssl_random_pseudo_bytes($length * 2); |
816
|
|
|
|
817
|
|
|
if ($bytes === false) |
818
|
|
|
{ |
819
|
|
|
throw new RuntimeException('Unable to generate random string.'); |
820
|
|
|
} |
821
|
|
|
|
822
|
|
|
return substr(str_replace(array('/', '+', '='), '', base64_encode($bytes)), 0, $length); |
823
|
|
|
} |
824
|
|
|
} |
825
|
|
|
|
826
|
|
|
if ( ! function_exists('str_replace_array')) |
827
|
|
|
{ |
828
|
|
|
/** |
829
|
|
|
* Replace a given value in the string sequentially with an array. |
830
|
|
|
* |
831
|
|
|
* @param string $search |
832
|
|
|
* @param array $replace |
833
|
|
|
* @param string $subject |
834
|
|
|
* @return string |
835
|
|
|
*/ |
836
|
|
|
function str_replace_array($search, array $replace, $subject) |
837
|
|
|
{ |
838
|
|
|
foreach ($replace as $value) |
839
|
|
|
{ |
840
|
|
|
$subject = preg_replace('/'.$search.'/', $value, $subject, 1); |
841
|
|
|
} |
842
|
|
|
|
843
|
|
|
return $subject; |
844
|
|
|
} |
845
|
|
|
} |
846
|
|
|
|
847
|
|
View Code Duplication |
if ( ! function_exists('studly_case')) |
|
|
|
|
848
|
|
|
{ |
849
|
|
|
/** |
850
|
|
|
* Convert a value to studly caps case. |
851
|
|
|
* |
852
|
|
|
* @param string $value |
853
|
|
|
* @return string |
854
|
|
|
*/ |
855
|
|
|
function studly_case($value) |
856
|
|
|
{ |
857
|
|
|
$studlyCache = []; |
858
|
|
|
$key = $value; |
859
|
|
|
|
860
|
|
|
if (isset($studlyCache[$key])) |
861
|
|
|
{ |
862
|
|
|
return $studlyCache[$key]; |
863
|
|
|
} |
864
|
|
|
|
865
|
|
|
$value = ucwords(str_replace(array('-', '_'), ' ', $value)); |
866
|
|
|
|
867
|
|
|
return $studlyCache[$key] = str_replace(' ', '', $value); |
868
|
|
|
} |
869
|
|
|
} |
870
|
|
|
|
871
|
|
|
if ( ! function_exists('trait_uses_recursive')) |
872
|
|
|
{ |
873
|
|
|
/** |
874
|
|
|
* Returns all traits used by a trait and its traits |
875
|
|
|
* |
876
|
|
|
* @param string $trait |
877
|
|
|
* @return array |
878
|
|
|
*/ |
879
|
|
|
function trait_uses_recursive($trait) |
880
|
|
|
{ |
881
|
|
|
$traits = class_uses($trait); |
882
|
|
|
|
883
|
|
|
foreach ($traits as $trait) |
884
|
|
|
{ |
885
|
|
|
$traits += trait_uses_recursive($trait); |
886
|
|
|
} |
887
|
|
|
|
888
|
|
|
return $traits; |
889
|
|
|
} |
890
|
|
|
} |
891
|
|
|
|
892
|
|
|
if ( ! function_exists('value')) |
893
|
|
|
{ |
894
|
|
|
/** |
895
|
|
|
* Return the default value of the given value. |
896
|
|
|
* |
897
|
|
|
* @param mixed $value |
898
|
|
|
* @return mixed |
899
|
|
|
*/ |
900
|
|
|
function value($value) |
901
|
|
|
{ |
902
|
|
|
return $value instanceof Closure ? $value() : $value; |
903
|
|
|
} |
904
|
|
|
} |
905
|
|
|
|
906
|
|
|
if ( ! function_exists('with')) |
907
|
|
|
{ |
908
|
|
|
/** |
909
|
|
|
* Return the given object. Useful for chaining. |
910
|
|
|
* |
911
|
|
|
* @param mixed $object |
912
|
|
|
* @return mixed |
913
|
|
|
*/ |
914
|
|
|
function with($object) |
915
|
|
|
{ |
916
|
|
|
return $object; |
917
|
|
|
} |
918
|
|
|
} |
919
|
|
|
|
920
|
|
|
/** |
921
|
|
|
* Helper functions for the helper functions, that can still be used standalone |
922
|
|
|
*/ |
923
|
|
View Code Duplication |
if ( ! function_exists('studly')) |
|
|
|
|
924
|
|
|
{ |
925
|
|
|
/** |
926
|
|
|
* Convert a value to studly caps case. |
927
|
|
|
* |
928
|
|
|
* @param string $value |
929
|
|
|
* @return string |
930
|
|
|
*/ |
931
|
|
|
function studly($value) |
932
|
|
|
{ |
933
|
|
|
$studlyCache = []; |
934
|
|
|
$key = $value; |
935
|
|
|
|
936
|
|
|
if (isset($studlyCache[$key])) |
937
|
|
|
{ |
938
|
|
|
return $studlyCache[$key]; |
939
|
|
|
} |
940
|
|
|
|
941
|
|
|
$value = ucwords(str_replace(array('-', '_'), ' ', $value)); |
942
|
|
|
|
943
|
|
|
return $studlyCache[$key] = str_replace(' ', '', $value); |
944
|
|
|
} |
945
|
|
|
} |
946
|
|
|
|
947
|
|
View Code Duplication |
if ( ! function_exists('get')) |
|
|
|
|
948
|
|
|
{ |
949
|
|
|
/** |
950
|
|
|
* Get an item from an array using "dot" notation. |
951
|
|
|
* |
952
|
|
|
* @param array $array |
953
|
|
|
* @param string $key |
954
|
|
|
* @param mixed $default |
955
|
|
|
* @return mixed |
956
|
|
|
*/ |
957
|
|
|
function get($array, $key, $default = null) |
958
|
|
|
{ |
959
|
|
|
if (is_null($key)) return $array; |
960
|
|
|
|
961
|
|
|
if (isset($array[$key])) return $array[$key]; |
962
|
|
|
|
963
|
|
|
foreach (explode('.', $key) as $segment) |
964
|
|
|
{ |
965
|
|
|
if ( ! is_array($array) || ! array_key_exists($segment, $array)) |
966
|
|
|
{ |
967
|
|
|
return value($default); |
968
|
|
|
} |
969
|
|
|
|
970
|
|
|
$array = $array[$segment]; |
971
|
|
|
} |
972
|
|
|
|
973
|
|
|
return $array; |
974
|
|
|
} |
975
|
|
|
} |
976
|
|
|
|
977
|
|
View Code Duplication |
if ( ! function_exists('set')) |
|
|
|
|
978
|
|
|
{ |
979
|
|
|
/** |
980
|
|
|
* Set an array item to a given value using "dot" notation. |
981
|
|
|
* |
982
|
|
|
* If no key is given to the method, the entire array will be replaced. |
983
|
|
|
* |
984
|
|
|
* @param array $array |
985
|
|
|
* @param string $key |
986
|
|
|
* @param mixed $value |
987
|
|
|
* @return array |
988
|
|
|
*/ |
989
|
|
|
function set(&$array, $key, $value) |
990
|
|
|
{ |
991
|
|
|
if (is_null($key)) return $array = $value; |
992
|
|
|
|
993
|
|
|
$keys = explode('.', $key); |
994
|
|
|
|
995
|
|
|
while (count($keys) > 1) |
996
|
|
|
{ |
997
|
|
|
$key = array_shift($keys); |
998
|
|
|
|
999
|
|
|
// If the key doesn't exist at this depth, we will just create an empty array |
1000
|
|
|
// to hold the next value, allowing us to create the arrays to hold final |
1001
|
|
|
// values at the correct depth. Then we'll keep digging into the array. |
1002
|
|
|
if ( ! isset($array[$key]) || ! is_array($array[$key])) |
1003
|
|
|
{ |
1004
|
|
|
$array[$key] = array(); |
1005
|
|
|
} |
1006
|
|
|
|
1007
|
|
|
$array =& $array[$key]; |
1008
|
|
|
} |
1009
|
|
|
|
1010
|
|
|
$array[array_shift($keys)] = $value; |
1011
|
|
|
|
1012
|
|
|
return $array; |
1013
|
|
|
} |
1014
|
|
|
} |
1015
|
|
|
|
1016
|
|
View Code Duplication |
if ( ! function_exists('dot')) |
|
|
|
|
1017
|
|
|
{ |
1018
|
|
|
/** |
1019
|
|
|
* Flatten a multi-dimensional associative array with dots. |
1020
|
|
|
* |
1021
|
|
|
* @param array $array |
1022
|
|
|
* @param string $prepend |
1023
|
|
|
* @return array |
1024
|
|
|
*/ |
1025
|
|
|
function dot($array, $prepend = '') |
1026
|
|
|
{ |
1027
|
|
|
$results = array(); |
1028
|
|
|
|
1029
|
|
|
foreach ($array as $key => $value) |
1030
|
|
|
{ |
1031
|
|
|
if (is_array($value)) |
1032
|
|
|
{ |
1033
|
|
|
$results = array_merge($results, dot($value, $prepend.$key.'.')); |
1034
|
|
|
} |
1035
|
|
|
else |
1036
|
|
|
{ |
1037
|
|
|
$results[$prepend.$key] = $value; |
1038
|
|
|
} |
1039
|
|
|
} |
1040
|
|
|
|
1041
|
|
|
return $results; |
1042
|
|
|
} |
1043
|
|
|
} |
1044
|
|
|
|
1045
|
|
View Code Duplication |
if ( ! function_exists('first')) |
|
|
|
|
1046
|
|
|
{ |
1047
|
|
|
/** |
1048
|
|
|
* Return the first element in an array passing a given truth test. |
1049
|
|
|
* |
1050
|
|
|
* @param array $array |
1051
|
|
|
* @param \Closure $callback |
1052
|
|
|
* @param mixed $default |
1053
|
|
|
* @return mixed |
1054
|
|
|
*/ |
1055
|
|
|
function first($array, $callback, $default = null) |
1056
|
|
|
{ |
1057
|
|
|
foreach ($array as $key => $value) |
1058
|
|
|
{ |
1059
|
|
|
if (call_user_func($callback, $key, $value)) return $value; |
1060
|
|
|
} |
1061
|
|
|
|
1062
|
|
|
return value($default); |
1063
|
|
|
} |
1064
|
|
|
} |
1065
|
|
|
|
1066
|
|
View Code Duplication |
if ( ! function_exists('forget')) |
|
|
|
|
1067
|
|
|
{ |
1068
|
|
|
/** |
1069
|
|
|
* Remove one or many array items from a given array using "dot" notation. |
1070
|
|
|
* |
1071
|
|
|
* @param array $array |
1072
|
|
|
* @param array|string $keys |
1073
|
|
|
* @return void |
1074
|
|
|
*/ |
1075
|
|
|
function forget(&$array, $keys) |
1076
|
|
|
{ |
1077
|
|
|
$original =& $array; |
1078
|
|
|
|
1079
|
|
|
foreach ((array) $keys as $key) |
1080
|
|
|
{ |
1081
|
|
|
$parts = explode('.', $key); |
1082
|
|
|
|
1083
|
|
|
while (count($parts) > 1) |
1084
|
|
|
{ |
1085
|
|
|
$part = array_shift($parts); |
1086
|
|
|
|
1087
|
|
|
if (isset($array[$part]) && is_array($array[$part])) |
1088
|
|
|
{ |
1089
|
|
|
$array =& $array[$part]; |
1090
|
|
|
} |
1091
|
|
|
} |
1092
|
|
|
|
1093
|
|
|
unset($array[array_shift($parts)]); |
1094
|
|
|
|
1095
|
|
|
// clean up after each pass |
1096
|
|
|
$array =& $original; |
1097
|
|
|
} |
1098
|
|
|
} |
1099
|
|
|
} |
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.