-
Notifications
You must be signed in to change notification settings - Fork 10
/
Hash.php
1275 lines (1146 loc) · 42.2 KB
/
Hash.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
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
declare(strict_types=1);
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @since 2.2.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Utility;
use ArrayAccess;
use InvalidArgumentException;
use RuntimeException;
/**
* Library of array functions for manipulating and extracting data
* from arrays or 'sets' of data.
*
* `Hash` provides an improved interface, more consistent and
* predictable set of features over `Set`. While it lacks the spotty
* support for pseudo Xpath, its more fully featured dot notation provides
* similar features in a more consistent implementation.
*
* @link https://book.cakephp.org/4/en/core-libraries/hash.html
*/
class Hash
{
/**
* Get a single value specified by $path out of $data.
* Does not support the full dot notation feature set,
* but is faster for simple read operations.
*
* @param array|\ArrayAccess $data Array of data or object implementing
* \ArrayAccess interface to operate on.
* @param string|int|string[]|null $path The path being searched for. Either a dot
* separated string, or an array of path segments.
* @param mixed $default The return value when the path does not exist
* @throws \InvalidArgumentException
* @return mixed The value fetched from the array, or null.
* @link https://book.cakephp.org/4/en/core-libraries/hash.html#Cake\Utility\Hash::get
*/
public static function get($data, $path, $default = null)
{
if (!(is_array($data) || $data instanceof ArrayAccess)) {
throw new InvalidArgumentException(
'Invalid data type, must be an array or \ArrayAccess instance.'
);
}
if (empty($data) || $path === null) {
return $default;
}
if (is_string($path) || is_numeric($path)) {
$parts = explode('.', (string)$path);
} else {
if (!is_array($path)) {
throw new InvalidArgumentException(sprintf(
'Invalid Parameter %s, should be dot separated path or array.',
$path
));
}
$parts = $path;
}
switch (count($parts)) {
case 1:
return $data[$parts[0]] ?? $default;
case 2:
return $data[$parts[0]][$parts[1]] ?? $default;
case 3:
return $data[$parts[0]][$parts[1]][$parts[2]] ?? $default;
default:
foreach ($parts as $key) {
if ((is_array($data) || $data instanceof ArrayAccess) && isset($data[$key])) {
$data = $data[$key];
} else {
return $default;
}
}
}
return $data;
}
/**
* Gets the values from an array matching the $path expression.
* The path expression is a dot separated expression, that can contain a set
* of patterns and expressions:
*
* - `{n}` Matches any numeric key, or integer.
* - `{s}` Matches any string key.
* - `{*}` Matches any value.
* - `Foo` Matches any key with the exact same value.
*
* There are a number of attribute operators:
*
* - `=`, `!=` Equality.
* - `>`, `<`, `>=`, `<=` Value comparison.
* - `=/.../` Regular expression pattern match.
*
* Given a set of User array data, from a `$usersTable->find('all')` call:
*
* - `1.User.name` Get the name of the user at index 1.
* - `{n}.User.name` Get the name of every user in the set of users.
* - `{n}.User[id].name` Get the name of every user with an id key.
* - `{n}.User[id>=2].name` Get the name of every user with an id key greater than or equal to 2.
* - `{n}.User[username=/^paul/]` Get User elements with username matching `^paul`.
* - `{n}.User[id=1].name` Get the Users name with id matching `1`.
*
* @param array|\ArrayAccess $data The data to extract from.
* @param string $path The path to extract.
* @return array|\ArrayAccess An array of the extracted values. Returns an empty array
* if there are no matches.
* @link https://book.cakephp.org/4/en/core-libraries/hash.html#Cake\Utility\Hash::extract
*/
public static function extract($data, string $path)
{
if (!(is_array($data) || $data instanceof ArrayAccess)) {
throw new InvalidArgumentException(
'Invalid data type, must be an array or \ArrayAccess instance.'
);
}
if (empty($path)) {
return $data;
}
// Simple paths.
if (!preg_match('/[{\[]/', $path)) {
$data = static::get($data, $path);
if ($data !== null && !(is_array($data) || $data instanceof ArrayAccess)) {
return [$data];
}
return $data !== null ? (array)$data : [];
}
if (strpos($path, '[') === false) {
$tokens = explode('.', $path);
} else {
$tokens = Text::tokenize($path, '.', '[', ']');
}
$_key = '__set_item__';
$context = [$_key => [$data]];
foreach ($tokens as $token) {
$next = [];
[$token, $conditions] = self::_splitConditions($token);
foreach ($context[$_key] as $item) {
if (is_object($item) && method_exists($item, 'toArray')) {
/** @var \Cake\Datasource\EntityInterface $item */
$item = $item->toArray();
}
foreach ((array)$item as $k => $v) {
if (static::_matchToken($k, $token)) {
$next[] = $v;
}
}
}
// Filter for attributes.
if ($conditions) {
$filter = [];
foreach ($next as $item) {
if (
(
is_array($item) ||
$item instanceof ArrayAccess
) &&
static::_matches($item, $conditions)
) {
$filter[] = $item;
}
}
$next = $filter;
}
$context = [$_key => $next];
}
return $context[$_key];
}
/**
* Split token conditions
*
* @param string $token the token being splitted.
* @return array [token, conditions] with token splitted
*/
protected static function _splitConditions(string $token): array
{
$conditions = false;
$position = strpos($token, '[');
if ($position !== false) {
$conditions = substr($token, $position);
$token = substr($token, 0, $position);
}
return [$token, $conditions];
}
/**
* Check a key against a token.
*
* @param mixed $key The key in the array being searched.
* @param string $token The token being matched.
* @return bool
*/
protected static function _matchToken($key, string $token): bool
{
switch ($token) {
case '{n}':
return is_numeric($key);
case '{s}':
return is_string($key);
case '{*}':
return true;
default:
return is_numeric($token) ? ($key == $token) : $key === $token;
}
}
/**
* Checks whether or not $data matches the attribute patterns
*
* @param array|\ArrayAccess $data Array of data to match.
* @param string $selector The patterns to match.
* @return bool Fitness of expression.
*/
protected static function _matches($data, string $selector): bool
{
preg_match_all(
'/(\[ (?P<attr>[^=><!]+?) (\s* (?P<op>[><!]?[=]|[><]) \s* (?P<val>(?:\/.*?\/ | [^\]]+)) )? \])/x',
$selector,
$conditions,
PREG_SET_ORDER
);
foreach ($conditions as $cond) {
$attr = $cond['attr'];
$op = $cond['op'] ?? null;
$val = $cond['val'] ?? null;
// Presence test.
if (empty($op) && empty($val) && !isset($data[$attr])) {
return false;
}
if (is_array($data)) {
$attrPresent = array_key_exists($attr, $data);
} else {
$attrPresent = $data->offsetExists($attr);
}
// Empty attribute = fail.
if (!$attrPresent) {
return false;
}
$prop = '';
if (isset($data[$attr])) {
$prop = $data[$attr];
}
$isBool = is_bool($prop);
if ($isBool && is_numeric($val)) {
$prop = $prop ? '1' : '0';
} elseif ($isBool) {
$prop = $prop ? 'true' : 'false';
} elseif (is_numeric($prop)) {
$prop = (string)$prop;
}
// Pattern matches and other operators.
if ($op === '=' && $val && $val[0] === '/') {
if (!preg_match($val, $prop)) {
return false;
}
// phpcs:disable
} elseif (
($op === '=' && $prop != $val) ||
($op === '!=' && $prop == $val) ||
($op === '>' && $prop <= $val) ||
($op === '<' && $prop >= $val) ||
($op === '>=' && $prop < $val) ||
($op === '<=' && $prop > $val)
// phpcs:enable
) {
return false;
}
}
return true;
}
/**
* Insert $values into an array with the given $path. You can use
* `{n}` and `{s}` elements to insert $data multiple times.
*
* @param array $data The data to insert into.
* @param string $path The path to insert at.
* @param mixed $values The values to insert.
* @return array The data with $values inserted.
* @link https://book.cakephp.org/4/en/core-libraries/hash.html#Cake\Utility\Hash::insert
*/
public static function insert(array $data, string $path, $values = null): array
{
$noTokens = strpos($path, '[') === false;
if ($noTokens && strpos($path, '.') === false) {
$data[$path] = $values;
return $data;
}
if ($noTokens) {
$tokens = explode('.', $path);
} else {
$tokens = Text::tokenize($path, '.', '[', ']');
}
if ($noTokens && strpos($path, '{') === false) {
return static::_simpleOp('insert', $data, $tokens, $values);
}
$token = array_shift($tokens);
$nextPath = implode('.', $tokens);
[$token, $conditions] = static::_splitConditions($token);
foreach ($data as $k => $v) {
if (static::_matchToken($k, $token)) {
if (!$conditions || static::_matches($v, $conditions)) {
$data[$k] = $nextPath
? static::insert($v, $nextPath, $values)
: array_merge($v, (array)$values);
}
}
}
return $data;
}
/**
* Perform a simple insert/remove operation.
*
* @param string $op The operation to do.
* @param array $data The data to operate on.
* @param string[] $path The path to work on.
* @param mixed $values The values to insert when doing inserts.
* @return array data.
*/
protected static function _simpleOp(string $op, array $data, array $path, $values = null): array
{
$_list = &$data;
$count = count($path);
$last = $count - 1;
foreach ($path as $i => $key) {
if ($op === 'insert') {
if ($i === $last) {
$_list[$key] = $values;
return $data;
}
if (!isset($_list[$key])) {
$_list[$key] = [];
}
$_list = &$_list[$key];
if (!is_array($_list)) {
$_list = [];
}
} elseif ($op === 'remove') {
if ($i === $last) {
if (is_array($_list)) {
unset($_list[$key]);
}
return $data;
}
if (!isset($_list[$key])) {
return $data;
}
$_list = &$_list[$key];
}
}
return $data;
}
/**
* Remove data matching $path from the $data array.
* You can use `{n}` and `{s}` to remove multiple elements
* from $data.
*
* @param array $data The data to operate on
* @param string $path A path expression to use to remove.
* @return array The modified array.
* @link https://book.cakephp.org/4/en/core-libraries/hash.html#Cake\Utility\Hash::remove
*/
public static function remove(array $data, string $path): array
{
$noTokens = strpos($path, '[') === false;
$noExpansion = strpos($path, '{') === false;
if ($noExpansion && $noTokens && strpos($path, '.') === false) {
unset($data[$path]);
return $data;
}
$tokens = $noTokens ? explode('.', $path) : Text::tokenize($path, '.', '[', ']');
if ($noExpansion && $noTokens) {
return static::_simpleOp('remove', $data, $tokens);
}
$token = array_shift($tokens);
$nextPath = implode('.', $tokens);
[$token, $conditions] = self::_splitConditions($token);
foreach ($data as $k => $v) {
$match = static::_matchToken($k, $token);
if ($match && is_array($v)) {
if ($conditions) {
if (static::_matches($v, $conditions)) {
if ($nextPath !== '') {
$data[$k] = static::remove($v, $nextPath);
} else {
unset($data[$k]);
}
}
} else {
$data[$k] = static::remove($v, $nextPath);
}
if (empty($data[$k])) {
unset($data[$k]);
}
} elseif ($match && $nextPath === '') {
unset($data[$k]);
}
}
return $data;
}
/**
* Creates an associative array using `$keyPath` as the path to build its keys, and optionally
* `$valuePath` as path to get the values. If `$valuePath` is not specified, all values will be initialized
* to null (useful for Hash::merge). You can optionally group the values by what is obtained when
* following the path specified in `$groupPath`.
*
* @param array $data Array from where to extract keys and values
* @param string|string[]|null $keyPath A dot-separated string.
* @param string|string[]|null $valuePath A dot-separated string.
* @param string|null $groupPath A dot-separated string.
* @return array Combined array
* @link https://book.cakephp.org/4/en/core-libraries/hash.html#Cake\Utility\Hash::combine
* @throws \RuntimeException When keys and values count is unequal.
*/
public static function combine(array $data, $keyPath, $valuePath = null, ?string $groupPath = null): array
{
if (empty($data)) {
return [];
}
if (is_array($keyPath)) {
$format = array_shift($keyPath);
/** @var array $keys */
$keys = static::format($data, $keyPath, $format);
} elseif ($keyPath === null) {
$keys = $keyPath;
} else {
/** @var array $keys */
$keys = static::extract($data, $keyPath);
}
if ($keyPath !== null && empty($keys)) {
return [];
}
$vals = null;
if (!empty($valuePath) && is_array($valuePath)) {
$format = array_shift($valuePath);
/** @var array $vals */
$vals = static::format($data, $valuePath, $format);
} elseif (!empty($valuePath)) {
/** @var array $vals */
$vals = static::extract($data, $valuePath);
}
if (empty($vals)) {
$vals = array_fill(0, $keys === null ? count($data) : count($keys), null);
}
if (is_array($keys) && count($keys) !== count($vals)) {
throw new RuntimeException(
'Hash::combine() needs an equal number of keys + values.'
);
}
if ($groupPath !== null) {
$group = static::extract($data, $groupPath);
if (!empty($group)) {
$c = is_array($keys) ? count($keys) : count($vals);
$out = [];
for ($i = 0; $i < $c; $i++) {
if (!isset($group[$i])) {
$group[$i] = 0;
}
if (!isset($out[$group[$i]])) {
$out[$group[$i]] = [];
}
if ($keys === null) {
$out[$group[$i]][] = $vals[$i];
} else {
$out[$group[$i]][$keys[$i]] = $vals[$i];
}
}
return $out;
}
}
if (empty($vals)) {
return [];
}
return array_combine($keys ?? range(0, count($vals) - 1), $vals);
}
/**
* Returns a formatted series of values extracted from `$data`, using
* `$format` as the format and `$paths` as the values to extract.
*
* Usage:
*
* ```
* $result = Hash::format($users, ['{n}.User.id', '{n}.User.name'], '%s : %s');
* ```
*
* The `$format` string can use any format options that `vsprintf()` and `sprintf()` do.
*
* @param array $data Source array from which to extract the data
* @param string[] $paths An array containing one or more Hash::extract()-style key paths
* @param string $format Format string into which values will be inserted, see sprintf()
* @return string[]|null An array of strings extracted from `$path` and formatted with `$format`
* @link https://book.cakephp.org/4/en/core-libraries/hash.html#Cake\Utility\Hash::format
* @see sprintf()
* @see \Cake\Utility\Hash::extract()
*/
public static function format(array $data, array $paths, string $format): ?array
{
$extracted = [];
$count = count($paths);
if (!$count) {
return null;
}
for ($i = 0; $i < $count; $i++) {
$extracted[] = static::extract($data, $paths[$i]);
}
$out = [];
/** @var array<mixed, array> $data */
$data = $extracted;
$count = count($data[0]);
$countTwo = count($data);
for ($j = 0; $j < $count; $j++) {
$args = [];
for ($i = 0; $i < $countTwo; $i++) {
if (array_key_exists($j, $data[$i])) {
$args[] = $data[$i][$j];
}
}
$out[] = vsprintf($format, $args);
}
return $out;
}
/**
* Determines if one array contains the exact keys and values of another.
*
* @param array $data The data to search through.
* @param array $needle The values to file in $data
* @return bool true If $data contains $needle, false otherwise
* @link https://book.cakephp.org/4/en/core-libraries/hash.html#Cake\Utility\Hash::contains
*/
public static function contains(array $data, array $needle): bool
{
if (empty($data) || empty($needle)) {
return false;
}
$stack = [];
while (!empty($needle)) {
$key = key($needle);
$val = $needle[$key];
unset($needle[$key]);
if (array_key_exists($key, $data) && is_array($val)) {
$next = $data[$key];
unset($data[$key]);
if (!empty($val)) {
$stack[] = [$val, $next];
}
} elseif (!array_key_exists($key, $data) || $data[$key] != $val) {
return false;
}
if (empty($needle) && !empty($stack)) {
[$needle, $data] = array_pop($stack);
}
}
return true;
}
/**
* Test whether or not a given path exists in $data.
* This method uses the same path syntax as Hash::extract()
*
* Checking for paths that could target more than one element will
* make sure that at least one matching element exists.
*
* @param array $data The data to check.
* @param string $path The path to check for.
* @return bool Existence of path.
* @see \Cake\Utility\Hash::extract()
* @link https://book.cakephp.org/4/en/core-libraries/hash.html#Cake\Utility\Hash::check
*/
public static function check(array $data, string $path): bool
{
$results = static::extract($data, $path);
if (!is_array($results)) {
return false;
}
return count($results) > 0;
}
/**
* Recursively filters a data set.
*
* @param array $data Either an array to filter, or value when in callback
* @param callable|array $callback A function to filter the data with. Defaults to
* `static::_filter()` Which strips out all non-zero empty values.
* @return array Filtered array
* @link https://book.cakephp.org/4/en/core-libraries/hash.html#Cake\Utility\Hash::filter
*/
public static function filter(array $data, $callback = ['self', '_filter']): array
{
foreach ($data as $k => $v) {
if (is_array($v)) {
$data[$k] = static::filter($v, $callback);
}
}
return array_filter($data, $callback);
}
/**
* Callback function for filtering.
*
* @param mixed $var Array to filter.
* @return bool
*/
protected static function _filter($var): bool
{
return $var === 0 || $var === 0.0 || $var === '0' || !empty($var);
}
/**
* Collapses a multi-dimensional array into a single dimension, using a delimited array path for
* each array element's key, i.e. [['Foo' => ['Bar' => 'Far']]] becomes
* ['0.Foo.Bar' => 'Far'].)
*
* @param array $data Array to flatten
* @param string $separator String used to separate array key elements in a path, defaults to '.'
* @return array
* @link https://book.cakephp.org/4/en/core-libraries/hash.html#Cake\Utility\Hash::flatten
*/
public static function flatten(array $data, string $separator = '.'): array
{
$result = [];
$stack = [];
$path = '';
reset($data);
while (!empty($data)) {
$key = key($data);
$element = $data[$key];
unset($data[$key]);
if (is_array($element) && !empty($element)) {
if (!empty($data)) {
$stack[] = [$data, $path];
}
$data = $element;
reset($data);
$path .= $key . $separator;
} else {
$result[$path . $key] = $element;
}
if (empty($data) && !empty($stack)) {
[$data, $path] = array_pop($stack);
reset($data);
}
}
return $result;
}
/**
* Expands a flat array to a nested array.
*
* For example, unflattens an array that was collapsed with `Hash::flatten()`
* into a multi-dimensional array. So, `['0.Foo.Bar' => 'Far']` becomes
* `[['Foo' => ['Bar' => 'Far']]]`.
*
* @param array $data Flattened array
* @param string $separator The delimiter used
* @return array
* @link https://book.cakephp.org/4/en/core-libraries/hash.html#Cake\Utility\Hash::expand
*/
public static function expand(array $data, string $separator = '.'): array
{
$result = [];
foreach ($data as $flat => $value) {
$keys = explode($separator, (string)$flat);
$keys = array_reverse($keys);
$child = [
$keys[0] => $value,
];
array_shift($keys);
foreach ($keys as $k) {
$child = [
$k => $child,
];
}
$stack = [[$child, &$result]];
static::_merge($stack, $result);
}
return $result;
}
/**
* This function can be thought of as a hybrid between PHP's `array_merge` and `array_merge_recursive`.
*
* The difference between this method and the built-in ones, is that if an array key contains another array, then
* Hash::merge() will behave in a recursive fashion (unlike `array_merge`). But it will not act recursively for
* keys that contain scalar values (unlike `array_merge_recursive`).
*
* This function will work with an unlimited amount of arguments and typecasts non-array parameters into arrays.
*
* @param array $data Array to be merged
* @param mixed $merge Array to merge with. The argument and all trailing arguments will be array cast when merged
* @return array Merged array
* @link https://book.cakephp.org/4/en/core-libraries/hash.html#Cake\Utility\Hash::merge
*/
public static function merge(array $data, $merge): array
{
$args = array_slice(func_get_args(), 1);
$return = $data;
$stack = [];
foreach ($args as &$curArg) {
$stack[] = [(array)$curArg, &$return];
}
unset($curArg);
static::_merge($stack, $return);
return $return;
}
/**
* Merge helper function to reduce duplicated code between merge() and expand().
*
* @param array $stack The stack of operations to work with.
* @param array $return The return value to operate on.
* @return void
*/
protected static function _merge(array $stack, array &$return): void
{
while (!empty($stack)) {
foreach ($stack as $curKey => &$curMerge) {
foreach ($curMerge[0] as $key => &$val) {
if (!is_array($curMerge[1])) {
continue;
}
if (
!empty($curMerge[1][$key])
&& (array)$curMerge[1][$key] === $curMerge[1][$key]
&& (array)$val === $val
) {
// Recurse into the current merge data as it is an array.
$stack[] = [&$val, &$curMerge[1][$key]];
} elseif ((int)$key === $key && isset($curMerge[1][$key])) {
$curMerge[1][] = $val;
} else {
$curMerge[1][$key] = $val;
}
}
unset($stack[$curKey]);
}
unset($curMerge);
}
}
/**
* Checks to see if all the values in the array are numeric
*
* @param array $data The array to check.
* @return bool true if values are numeric, false otherwise
* @link https://book.cakephp.org/4/en/core-libraries/hash.html#Cake\Utility\Hash::numeric
*/
public static function numeric(array $data): bool
{
if (empty($data)) {
return false;
}
return $data === array_filter($data, 'is_numeric');
}
/**
* Counts the dimensions of an array.
* Only considers the dimension of the first element in the array.
*
* If you have an un-even or heterogeneous array, consider using Hash::maxDimensions()
* to get the dimensions of the array.
*
* @param array $data Array to count dimensions on
* @return int The number of dimensions in $data
* @link https://book.cakephp.org/4/en/core-libraries/hash.html#Cake\Utility\Hash::dimensions
*/
public static function dimensions(array $data): int
{
if (empty($data)) {
return 0;
}
reset($data);
$depth = 1;
while ($elem = array_shift($data)) {
if (is_array($elem)) {
$depth++;
$data = $elem;
} else {
break;
}
}
return $depth;
}
/**
* Counts the dimensions of *all* array elements. Useful for finding the maximum
* number of dimensions in a mixed array.
*
* @param array $data Array to count dimensions on
* @return int The maximum number of dimensions in $data
* @link https://book.cakephp.org/4/en/core-libraries/hash.html#Cake\Utility\Hash::maxDimensions
*/
public static function maxDimensions(array $data): int
{
$depth = [];
if (!empty($data)) {
foreach ($data as $value) {
if (is_array($value)) {
$depth[] = static::maxDimensions($value) + 1;
} else {
$depth[] = 1;
}
}
}
return empty($depth) ? 0 : max($depth);
}
/**
* Map a callback across all elements in a set.
* Can be provided a path to only modify slices of the set.
*
* @param array $data The data to map over, and extract data out of.
* @param string $path The path to extract for mapping over.
* @param callable $function The function to call on each extracted value.
* @return array An array of the modified values.
* @link https://book.cakephp.org/4/en/core-libraries/hash.html#Cake\Utility\Hash::map
*/
public static function map(array $data, string $path, callable $function): array
{
$values = (array)static::extract($data, $path);
return array_map($function, $values);
}
/**
* Reduce a set of extracted values using `$function`.
*
* @param array $data The data to reduce.
* @param string $path The path to extract from $data.
* @param callable $function The function to call on each extracted value.
* @return mixed The reduced value.
* @link https://book.cakephp.org/4/en/core-libraries/hash.html#Cake\Utility\Hash::reduce
*/
public static function reduce(array $data, string $path, callable $function)
{
$values = (array)static::extract($data, $path);
return array_reduce($values, $function);
}
/**
* Apply a callback to a set of extracted values using `$function`.
* The function will get the extracted values as the first argument.
*
* ### Example
*
* You can easily count the results of an extract using apply().
* For example to count the comments on an Article:
*
* ```
* $count = Hash::apply($data, 'Article.Comment.{n}', 'count');
* ```
*
* You could also use a function like `array_sum` to sum the results.
*
* ```
* $total = Hash::apply($data, '{n}.Item.price', 'array_sum');
* ```
*
* @param array $data The data to reduce.
* @param string $path The path to extract from $data.
* @param callable $function The function to call on each extracted value.
* @return mixed The results of the applied method.
* @link https://book.cakephp.org/4/en/core-libraries/hash.html#Cake\Utility\Hash::apply
*/
public static function apply(array $data, string $path, callable $function)
{
$values = (array)static::extract($data, $path);
return $function($values);
}
/**
* Sorts an array by any value, determined by a Set-compatible path
*
* ### Sort directions
*
* - `asc` or \SORT_ASC Sort ascending.
* - `desc` or \SORT_DESC Sort descending.
*
* ### Sort types
*
* - `regular` For regular sorting (don't change types)
* - `numeric` Compare values numerically
* - `string` Compare values as strings
* - `locale` Compare items as strings, based on the current locale
* - `natural` Compare items as strings using "natural ordering" in a human friendly way
* Will sort foo10 below foo2 as an example.
*
* To do case insensitive sorting, pass the type as an array as follows:
*
* ```
* Hash::sort($data, 'some.attribute', 'asc', ['type' => 'regular', 'ignoreCase' => true]);
* ```
*
* When using the array form, `type` defaults to 'regular'. The `ignoreCase` option
* defaults to `false`.
*
* @param array $data An array of data to sort
* @param string $path A Set-compatible path to the array value
* @param string|int $dir See directions above. Defaults to 'asc'.
* @param array|string $type See direction types above. Defaults to 'regular'.
* @return array Sorted array of data
* @link https://book.cakephp.org/4/en/core-libraries/hash.html#Cake\Utility\Hash::sort
*/
public static function sort(array $data, string $path, $dir = 'asc', $type = 'regular'): array
{
if (empty($data)) {
return [];
}
$originalKeys = array_keys($data);
$numeric = is_numeric(implode('', $originalKeys));
if ($numeric) {
$data = array_values($data);