-
-
Notifications
You must be signed in to change notification settings - Fork 168
/
A.php
585 lines (535 loc) · 14.7 KB
/
A.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
<?php
namespace Kirby\Toolkit;
use Exception;
/**
* A set of handy methods to simplify array handling
* and make it more consistent.
*
* @package Kirby Toolkit
* @author Bastian Allgeier <bastian@getkirby.com>
* @link http://getkirby.com
* @copyright Bastian Allgeier
* @license MIT
*/
class A
{
/**
* Appends the given array
*
* @param array $array
* @param array $append
* @return array
*/
public static function append(array $array, array $append): array
{
return $array + $append;
}
/**
* Gets an element of an array by key
*
* <code>
* $array = [
* 'cat' => 'miao',
* 'dog' => 'wuff',
* 'bird' => 'tweet'
* ];
*
* echo A::get($array, 'cat');
* // output: 'miao'
*
* echo A::get($array, 'elephant', 'shut up');
* // output: 'shut up'
*
* $catAndDog = A::get($array, ['cat', 'dog']);
* // result: ['cat' => 'miao', 'dog' => 'wuff'];
* </code>
*
* @param array $array The source array
* @param mixed $key The key to look for
* @param mixed $default Optional default value, which should be
* returned if no element has been found
* @return mixed
*/
public static function get(array $array, $key, $default = null)
{
// return the entire array if the key is null
if ($key === null) {
return $array;
}
// get an array of keys
if (is_array($key) === true) {
$result = [];
foreach ($key as $k) {
$result[$k] = static::get($array, $k, $default);
}
return $result;
}
if (isset($array[$key]) === true) {
return $array[$key];
}
// support dot notation
if (strpos($key, '.') !== false) {
$keys = explode('.', $key);
foreach ($keys as $innerKey) {
if (isset($array[$innerKey]) === false) {
return $default;
}
$array = $array[$innerKey];
}
return $array;
}
return $default;
}
/**
* @return string
*/
public static function join($value, $separator = ', ')
{
if (is_string($value) === true) {
return $value;
}
return implode($separator, $value);
}
const MERGE_OVERWRITE = 0;
const MERGE_APPEND = 1;
const MERGE_REPLACE = 2;
/**
* Merges arrays recursively
*
* @param array $array1
* @param array $array2
* @param boolean $mode Behavior for elements with numeric keys;
* A::MERGE_APPEND: elements are appended, keys are reset;
* A::MERGE_OVERWRITE: elements are overwritten, keys are preserved
* A::MERGE_REPLACE: non-associative arrays are completely replaced
* @return array
*/
public static function merge($array1, $array2, $mode = A::MERGE_APPEND)
{
$merged = $array1;
if (static::isAssociative($array1) === false && $mode === static::MERGE_REPLACE) {
return $array2;
}
foreach ($array2 as $key => $value) {
// append to the merged array, don't overwrite numeric keys
if (is_int($key) === true && $mode == static::MERGE_APPEND) {
$merged[] = $value;
// recursively merge the two array values
} elseif (is_array($value) === true && isset($merged[$key]) === true && is_array($merged[$key]) === true) {
$merged[$key] = static::merge($merged[$key], $value, $mode);
// simply overwrite with the value from the second array
} else {
$merged[$key] = $value;
}
}
if ($mode == static::MERGE_APPEND) {
// the keys don't make sense anymore, reset them
// array_merge() is the simplest way to renumber
// arrays that have both numeric and string keys;
// besides the keys, nothing changes here
$merged = array_merge($merged, []);
}
return $merged;
}
/**
* Plucks a single column from an array
*
* <code>
* $array[] = [
* 'id' => 1,
* 'username' => 'homer',
* ];
*
* $array[] = [
* 'id' => 2,
* 'username' => 'marge',
* ];
*
* $array[] = [
* 'id' => 3,
* 'username' => 'lisa',
* ];
*
* var_dump(A::pluck($array, 'username'));
* // result: ['homer', 'marge', 'lisa'];
* </code>
*
* @param array $array The source array
* @param string $key The key name of the column to extract
* @return array The result array with all values
* from that column.
*/
public static function pluck(array $array, string $key)
{
$output = [];
foreach ($array as $a) {
if (isset($a[$key]) === true) {
$output[] = $a[$key];
}
}
return $output;
}
/**
* Prepends the given array
*
* @param array $array
* @param array $prepend
* @return array
*/
public static function prepend(array $array, array $prepend): array
{
return $prepend + $array;
}
/**
* Shuffles an array and keeps the keys
*
* <code>
* $array = [
* 'cat' => 'miao',
* 'dog' => 'wuff',
* 'bird' => 'tweet'
* ];
*
* $shuffled = A::shuffle($array);
* // output: [
* // 'dog' => 'wuff',
* // 'cat' => 'miao',
* // 'bird' => 'tweet'
* // ];
* </code>
*
* @param array $array The source array
* @return array The shuffled result array
*/
public static function shuffle(array $array): array
{
$keys = array_keys($array);
$new = [];
shuffle($keys);
// resort the array
foreach ($keys as $key) {
$new[$key] = $array[$key];
}
return $new;
}
/**
* Returns the first element of an array
*
* <code>
* $array = [
* 'cat' => 'miao',
* 'dog' => 'wuff',
* 'bird' => 'tweet'
* ];
*
* $first = A::first($array);
* // first: 'miao'
* </code>
*
* @param array $array The source array
* @return mixed The first element
*/
public static function first(array $array)
{
return array_shift($array);
}
/**
* Returns the last element of an array
*
* <code>
* $array = [
* 'cat' => 'miao',
* 'dog' => 'wuff',
* 'bird' => 'tweet'
* ];
*
* $last = A::last($array);
* // last: 'tweet'
* </code>
*
* @param array $array The source array
* @return mixed The last element
*/
public static function last(array $array)
{
return array_pop($array);
}
/**
* Fills an array up with additional elements to certain amount.
*
* <code>
* $array = [
* 'cat' => 'miao',
* 'dog' => 'wuff',
* 'bird' => 'tweet'
* ];
*
* $result = A::fill($array, 5, 'elephant');
*
* // result: [
* // 'cat',
* // 'dog',
* // 'bird',
* // 'elephant',
* // 'elephant',
* // ];
* </code>
*
* @param array $array The source array
* @param int $limit The number of elements the array should
* contain after filling it up.
* @param mixed $fill The element, which should be used to
* fill the array
* @return array The filled-up result array
*/
public static function fill(array $array, int $limit, $fill = 'placeholder'): array
{
if (count($array) < $limit) {
$diff = $limit - count($array);
for ($x = 0; $x < $diff; $x++) {
$array[] = $fill;
}
}
return $array;
}
/**
* Move an array item to a new index
*
* @param array $array
* @param int $from
* @param int $to
* @return array
*/
public static function move(array $array, int $from, int $to): array
{
$total = count($array);
if ($from >= $total || $from < 0) {
throw new Exception('Invalid "from" index');
}
if ($to >= $total || $to < 0) {
throw new Exception('Invalid "to" index');
}
// remove the item from the array
$item = array_splice($array, $from, 1);
// inject it at the new position
array_splice($array, $to, 0, $item);
return $array;
}
/**
* Checks for missing elements in an array
*
* This is very handy to check for missing
* user values in a request for example.
*
* <code>
* $array = [
* 'cat' => 'miao',
* 'dog' => 'wuff',
* 'bird' => 'tweet'
* ];
*
* $required = ['cat', 'elephant'];
*
* $missng = A::missing($array, $required);
* // missing: [
* // 'elephant'
* // ];
* </code>
*
* @param array $array The source array
* @param array $required An array of required keys
* @return array An array of missing fields. If this
* is empty, nothing is missing.
*/
public static function missing(array $array, array $required = []): array
{
$missing = [];
foreach ($required as $r) {
if (isset($array[$r]) === false) {
$missing[] = $r;
}
}
return $missing;
}
/**
* Sorts a multi-dimensional array by a certain column
*
* <code>
* $array[0] = [
* 'id' => 1,
* 'username' => 'mike',
* ];
*
* $array[1] = [
* 'id' => 2,
* 'username' => 'peter',
* ];
*
* $array[3] = [
* 'id' => 3,
* 'username' => 'john',
* ];
*
* $sorted = A::sort($array, 'username ASC');
* // Array
* // (
* // [0] => Array
* // (
* // [id] => 3
* // [username] => john
* // )
* // [1] => Array
* // (
* // [id] => 1
* // [username] => mike
* // )
* // [2] => Array
* // (
* // [id] => 2
* // [username] => peter
* // )
* // )
*
* </code>
*
* @param array $array The source array
* @param string $field The name of the column
* @param string $direction desc (descending) or asc (ascending)
* @param const $method A PHP sort method flag or 'natural' for
* natural sorting, which is not supported in
* PHP by sort flags
* @return array The sorted array
*/
public static function sort(array $array, string $field, string $direction = 'desc', $method = SORT_REGULAR): array
{
$direction = strtolower($direction) == 'desc' ? SORT_DESC : SORT_ASC;
$helper = [];
$result = [];
// build the helper array
foreach ($array as $key => $row) {
$helper[$key] = $row[$field];
}
// natural sorting
if ($direction === SORT_DESC) {
arsort($helper, $method);
} else {
asort($helper, $method);
}
// rebuild the original array
foreach ($helper as $key => $val) {
$result[$key] = $array[$key];
}
return $result;
}
/**
* Checks wether an array is associative or not
*
* <code>
* $array = ['a', 'b', 'c'];
*
* A::isAssociative($array);
* // returns: false
*
* $array = ['a' => 'a', 'b' => 'b', 'c' => 'c'];
*
* A::isAssociative($array);
* // returns: true
* </code>
*
* @param array $array The array to analyze
* @return boolean true: The array is associative false: It's not
*/
public static function isAssociative(array $array): bool
{
return ctype_digit(implode(null, array_keys($array))) === false;
}
/**
* Returns the average value of an array
*
* @param array $array The source array
* @param int $decimals The number of decimals to return
* @return float The average value
*/
public static function average(array $array, int $decimals = 0): float
{
return round((array_sum($array) / sizeof($array)), $decimals);
}
/**
* Merges arrays recursively
*
* <code>
* $defaults = [
* 'username' => 'admin',
* 'password' => 'admin',
* ];
*
* $options = A::extend($defaults, ['password' => 'super-secret']);
* // returns: [
* // 'username' => 'admin',
* // 'password' => 'super-secret'
* // ];
* </code>
*
* @return array
*/
public static function extend(...$arrays): array
{
return array_merge_recursive(...$arrays);
}
/**
* Update an array with a second array
* The second array can contain callbacks as values,
* which will get the original values as argument
*
* <code>
* $user = [
* 'username' => 'homer',
* 'email' => 'homer@simpsons.com'
* ];
*
* // simple updates
* A::update($user, [
* 'username' => 'homer j. simpson'
* ]);
*
* // with callback
* A::update($user, [
* 'username' => function ($username) {
* return $username . ' j. simpson'
* }
* ]);
* </code>
*
* @param array $array
* @param array $update
* @return array
*/
public static function update(array $array, array $update): array
{
foreach ($update as $key => $value) {
if (is_a($value, 'Closure') === true) {
$array[$key] = call_user_func($value, static::get($array, $key));
} else {
$array[$key] = $value;
}
}
return $array;
}
/**
* Wraps the given value in an array
* if it's not an array yet.
*
* @param mixed|null $array
* @return array
*/
public static function wrap($array = null): array
{
if ($array === null) {
return [];
} elseif (is_array($array) === false) {
return [$array];
} else {
return $array;
}
}
}