1: | <?php declare(strict_types=1); |
2: | |
3: | namespace Salient\Utility; |
4: | |
5: | use Salient\Contract\Core\Jsonable; |
6: | use ArrayAccess; |
7: | use OutOfRangeException; |
8: | use Stringable; |
9: | |
10: | |
11: | |
12: | |
13: | |
14: | |
15: | final class Arr extends AbstractUtility |
16: | { |
17: | public const SORT_REGULAR = \SORT_REGULAR; |
18: | public const SORT_NUMERIC = \SORT_NUMERIC; |
19: | public const SORT_STRING = \SORT_STRING; |
20: | public const SORT_LOCALE_STRING = \SORT_LOCALE_STRING; |
21: | public const SORT_NATURAL = \SORT_NATURAL; |
22: | public const SORT_FLAG_CASE = \SORT_FLAG_CASE; |
23: | |
24: | |
25: | |
26: | |
27: | |
28: | |
29: | |
30: | public static function pluck(iterable $array, string $valueKey, ?string $keyKey = null): array |
31: | { |
32: | foreach ($array as $item) { |
33: | $value = self::get($item, $valueKey); |
34: | if ($keyKey === null) { |
35: | $plucked[] = $value; |
36: | continue; |
37: | } |
38: | $key = self::get($item, $keyKey); |
39: | $plucked[$key] = $value; |
40: | } |
41: | return $plucked ?? []; |
42: | } |
43: | |
44: | |
45: | |
46: | |
47: | |
48: | |
49: | |
50: | |
51: | |
52: | |
53: | public static function get(array $array, string $key, $default = null) |
54: | { |
55: | foreach (explode('.', $key) as $part) { |
56: | if (is_array($array) && array_key_exists($part, $array)) { |
57: | $array = $array[$part]; |
58: | continue; |
59: | } |
60: | if (func_num_args() < 3) { |
61: | throw new OutOfRangeException(sprintf( |
62: | 'Array key not found: %s', |
63: | $key, |
64: | )); |
65: | } |
66: | return $default; |
67: | } |
68: | return $array; |
69: | } |
70: | |
71: | |
72: | |
73: | |
74: | |
75: | |
76: | public static function has(array $array, string $key): bool |
77: | { |
78: | foreach (explode('.', $key) as $part) { |
79: | if (!is_array($array) || !array_key_exists($part, $array)) { |
80: | return false; |
81: | } |
82: | $array = $array[$part]; |
83: | } |
84: | return true; |
85: | } |
86: | |
87: | |
88: | |
89: | |
90: | |
91: | |
92: | |
93: | |
94: | |
95: | public static function first(array $array) |
96: | { |
97: | if ($array === []) { |
98: | return null; |
99: | } |
100: | return reset($array); |
101: | } |
102: | |
103: | |
104: | |
105: | |
106: | |
107: | |
108: | |
109: | |
110: | |
111: | public static function last(array $array) |
112: | { |
113: | if ($array === []) { |
114: | return null; |
115: | } |
116: | return end($array); |
117: | } |
118: | |
119: | |
120: | |
121: | |
122: | |
123: | |
124: | |
125: | |
126: | |
127: | |
128: | |
129: | |
130: | public static function keyOf(array $array, $value) |
131: | { |
132: | $key = array_search($value, $array, true); |
133: | if ($key === false) { |
134: | throw new OutOfRangeException('Value not found in array'); |
135: | } |
136: | |
137: | return $key; |
138: | } |
139: | |
140: | |
141: | |
142: | |
143: | |
144: | |
145: | |
146: | |
147: | |
148: | |
149: | |
150: | |
151: | public static function shift(array $array, &$shifted = null): array |
152: | { |
153: | $shifted = array_shift($array); |
154: | return $array; |
155: | } |
156: | |
157: | |
158: | |
159: | |
160: | |
161: | |
162: | |
163: | |
164: | |
165: | |
166: | |
167: | public static function unshift(array $array, ...$values): array |
168: | { |
169: | array_unshift($array, ...$values); |
170: | return $array; |
171: | } |
172: | |
173: | |
174: | |
175: | |
176: | |
177: | |
178: | |
179: | |
180: | |
181: | |
182: | |
183: | |
184: | public static function pop(array $array, &$popped = null): array |
185: | { |
186: | $popped = array_pop($array); |
187: | return $array; |
188: | } |
189: | |
190: | |
191: | |
192: | |
193: | |
194: | |
195: | |
196: | |
197: | |
198: | |
199: | |
200: | public static function push(array $array, ...$values): array |
201: | { |
202: | array_push($array, ...$values); |
203: | return $array; |
204: | } |
205: | |
206: | |
207: | |
208: | |
209: | |
210: | |
211: | |
212: | |
213: | |
214: | |
215: | |
216: | |
217: | public static function extend(array $array, ...$values): array |
218: | { |
219: | return array_merge($array, array_diff($values, $array)); |
220: | } |
221: | |
222: | |
223: | |
224: | |
225: | |
226: | |
227: | |
228: | |
229: | |
230: | |
231: | |
232: | |
233: | public static function set(array $array, $key, $value): array |
234: | { |
235: | $array[$key] = $value; |
236: | return $array; |
237: | } |
238: | |
239: | |
240: | |
241: | |
242: | |
243: | |
244: | |
245: | |
246: | |
247: | |
248: | |
249: | |
250: | public static function setIf(array $array, $key, $value): array |
251: | { |
252: | if (!array_key_exists($key, $array)) { |
253: | $array[$key] = $value; |
254: | } |
255: | return $array; |
256: | } |
257: | |
258: | |
259: | |
260: | |
261: | |
262: | |
263: | |
264: | |
265: | |
266: | |
267: | |
268: | public static function unset(array $array, $key): array |
269: | { |
270: | unset($array[$key]); |
271: | return $array; |
272: | } |
273: | |
274: | |
275: | |
276: | |
277: | |
278: | |
279: | |
280: | |
281: | |
282: | |
283: | |
284: | public static function sort( |
285: | array $array, |
286: | bool $preserveKeys = false, |
287: | $callbackOrFlags = \SORT_REGULAR |
288: | ): array { |
289: | if (is_callable($callbackOrFlags)) { |
290: | if ($preserveKeys) { |
291: | uasort($array, $callbackOrFlags); |
292: | return $array; |
293: | } |
294: | usort($array, $callbackOrFlags); |
295: | return $array; |
296: | } |
297: | if ($preserveKeys) { |
298: | asort($array, $callbackOrFlags); |
299: | return $array; |
300: | } |
301: | sort($array, $callbackOrFlags); |
302: | return $array; |
303: | } |
304: | |
305: | |
306: | |
307: | |
308: | |
309: | |
310: | |
311: | |
312: | |
313: | |
314: | |
315: | public static function sortDesc( |
316: | array $array, |
317: | bool $preserveKeys = false, |
318: | int $flags = \SORT_REGULAR |
319: | ): array { |
320: | if ($preserveKeys) { |
321: | arsort($array, $flags); |
322: | return $array; |
323: | } |
324: | rsort($array, $flags); |
325: | return $array; |
326: | } |
327: | |
328: | |
329: | |
330: | |
331: | |
332: | |
333: | |
334: | |
335: | |
336: | |
337: | |
338: | public static function sortByKey( |
339: | array $array, |
340: | $callbackOrFlags = \SORT_REGULAR |
341: | ): array { |
342: | if (is_callable($callbackOrFlags)) { |
343: | uksort($array, $callbackOrFlags); |
344: | return $array; |
345: | } |
346: | ksort($array, $callbackOrFlags); |
347: | return $array; |
348: | } |
349: | |
350: | |
351: | |
352: | |
353: | |
354: | |
355: | |
356: | |
357: | |
358: | |
359: | |
360: | public static function sortByKeyDesc( |
361: | array $array, |
362: | int $flags = \SORT_REGULAR |
363: | ): array { |
364: | krsort($array, $flags); |
365: | return $array; |
366: | } |
367: | |
368: | |
369: | |
370: | |
371: | |
372: | |
373: | |
374: | |
375: | |
376: | |
377: | public static function unique( |
378: | iterable $array, |
379: | bool $preserveKeys = false |
380: | ): array { |
381: | $unique = []; |
382: | foreach ($array as $key => $value) { |
383: | if (in_array($value, $unique, true)) { |
384: | continue; |
385: | } |
386: | if ($preserveKeys) { |
387: | $unique[self::getKey($key, $i)] = $value; |
388: | } else { |
389: | $unique[] = $value; |
390: | } |
391: | } |
392: | return $unique; |
393: | } |
394: | |
395: | |
396: | |
397: | |
398: | |
399: | |
400: | |
401: | |
402: | public static function isList($value, bool $orEmpty = false): bool |
403: | { |
404: | if (!is_array($value)) { |
405: | return false; |
406: | } |
407: | if (!$value) { |
408: | return $orEmpty; |
409: | } |
410: | $i = 0; |
411: | foreach ($value as $key => $value) { |
412: | if ($i++ !== $key) { |
413: | return false; |
414: | } |
415: | } |
416: | return true; |
417: | } |
418: | |
419: | |
420: | |
421: | |
422: | |
423: | |
424: | |
425: | public static function isIndexed($value, bool $orEmpty = false): bool |
426: | { |
427: | if (!is_array($value)) { |
428: | return false; |
429: | } |
430: | if (!$value) { |
431: | return $orEmpty; |
432: | } |
433: | foreach ($value as $key => $value) { |
434: | if (!is_int($key)) { |
435: | return false; |
436: | } |
437: | } |
438: | return true; |
439: | } |
440: | |
441: | |
442: | |
443: | |
444: | |
445: | |
446: | |
447: | public static function ofArrayKey($value, bool $orEmpty = false): bool |
448: | { |
449: | return self::ofInt($value, $orEmpty) || self::ofString($value); |
450: | } |
451: | |
452: | |
453: | |
454: | |
455: | |
456: | |
457: | |
458: | public static function ofInt($value, bool $orEmpty = false): bool |
459: | { |
460: | return self::doIsArrayOf('is_int', $value, $orEmpty); |
461: | } |
462: | |
463: | |
464: | |
465: | |
466: | |
467: | |
468: | |
469: | public static function ofString($value, bool $orEmpty = false): bool |
470: | { |
471: | return self::doIsArrayOf('is_string', $value, $orEmpty); |
472: | } |
473: | |
474: | |
475: | |
476: | |
477: | |
478: | |
479: | |
480: | |
481: | |
482: | |
483: | public static function of($value, string $class, bool $orEmpty = false): bool |
484: | { |
485: | return self::doIsArrayOf('is_a', $value, $orEmpty, $class); |
486: | } |
487: | |
488: | |
489: | |
490: | |
491: | |
492: | |
493: | private static function doIsArrayOf(string $func, $value, bool $orEmpty, ...$args): bool |
494: | { |
495: | if (!is_array($value)) { |
496: | return false; |
497: | } |
498: | if (!$value) { |
499: | return $orEmpty; |
500: | } |
501: | foreach ($value as $item) { |
502: | if (!$func($item, ...$args)) { |
503: | return false; |
504: | } |
505: | } |
506: | return true; |
507: | } |
508: | |
509: | |
510: | |
511: | |
512: | |
513: | |
514: | |
515: | |
516: | public static function sameValues(array $array1, array $array2, array ...$arrays): bool |
517: | { |
518: | $last = null; |
519: | foreach ([$array1, $array2, ...$arrays] as $array) { |
520: | usort($array, fn($a, $b) => gettype($a) <=> gettype($b) ?: $a <=> $b); |
521: | if ($last !== null && $last !== $array) { |
522: | return false; |
523: | } |
524: | $last = $array; |
525: | } |
526: | return true; |
527: | } |
528: | |
529: | |
530: | |
531: | |
532: | |
533: | |
534: | |
535: | |
536: | public static function same(array $array1, array $array2, array ...$arrays): bool |
537: | { |
538: | $last = null; |
539: | foreach ([$array1, $array2, ...$arrays] as $array) { |
540: | ksort($array, \SORT_STRING); |
541: | if ($last !== null && $last !== $array) { |
542: | return false; |
543: | } |
544: | $last = $array; |
545: | } |
546: | return true; |
547: | } |
548: | |
549: | |
550: | |
551: | |
552: | |
553: | |
554: | |
555: | |
556: | |
557: | |
558: | public static function whereNotNull(iterable $array): array |
559: | { |
560: | foreach ($array as $key => $value) { |
561: | if ($value === null) { |
562: | continue; |
563: | } |
564: | $filtered[self::getKey($key, $i)] = $value; |
565: | } |
566: | return $filtered ?? []; |
567: | } |
568: | |
569: | |
570: | |
571: | |
572: | |
573: | |
574: | |
575: | |
576: | |
577: | |
578: | public static function whereNotEmpty(iterable $array): array |
579: | { |
580: | foreach ($array as $key => $value) { |
581: | if ($value === null || (string) $value === '') { |
582: | continue; |
583: | } |
584: | $filtered[self::getKey($key, $i)] = $value; |
585: | } |
586: | return $filtered ?? []; |
587: | } |
588: | |
589: | |
590: | |
591: | |
592: | |
593: | |
594: | |
595: | |
596: | |
597: | public static function implode( |
598: | string $separator, |
599: | iterable $array, |
600: | ?string $characters = null |
601: | ): string { |
602: | foreach ($array as $value) { |
603: | $value = (string) $value; |
604: | if ($characters !== '') { |
605: | $value = $characters === null |
606: | ? trim($value) |
607: | : trim($value, $characters); |
608: | } |
609: | if ($value === '') { |
610: | continue; |
611: | } |
612: | $filtered[] = $value; |
613: | } |
614: | return implode($separator, $filtered ?? []); |
615: | } |
616: | |
617: | |
618: | |
619: | |
620: | |
621: | |
622: | |
623: | |
624: | |
625: | |
626: | |
627: | |
628: | public static function trim( |
629: | iterable $array, |
630: | ?string $characters = null, |
631: | bool $removeEmpty = true, |
632: | bool $nullEmpty = false |
633: | ): array { |
634: | foreach ($array as $key => $value) { |
635: | $value = (string) $value; |
636: | if ($characters !== '') { |
637: | $value = $characters === null |
638: | ? trim($value) |
639: | : trim($value, $characters); |
640: | } |
641: | if ($removeEmpty) { |
642: | if ($value !== '') { |
643: | $trimmed[] = $value; |
644: | } |
645: | continue; |
646: | } |
647: | $trimmed[self::getKey($key, $i)] = $nullEmpty && $value === '' |
648: | ? null |
649: | : $value; |
650: | } |
651: | return $trimmed ?? []; |
652: | } |
653: | |
654: | |
655: | |
656: | |
657: | |
658: | |
659: | |
660: | |
661: | |
662: | |
663: | public static function lower(iterable $array): array |
664: | { |
665: | foreach ($array as $key => $value) { |
666: | $lower[self::getKey($key, $i)] = Str::lower((string) $value); |
667: | } |
668: | return $lower ?? []; |
669: | } |
670: | |
671: | |
672: | |
673: | |
674: | |
675: | |
676: | |
677: | |
678: | |
679: | |
680: | public static function upper(iterable $array): array |
681: | { |
682: | foreach ($array as $key => $value) { |
683: | $upper[self::getKey($key, $i)] = Str::upper((string) $value); |
684: | } |
685: | return $upper ?? []; |
686: | } |
687: | |
688: | |
689: | |
690: | |
691: | |
692: | |
693: | |
694: | |
695: | |
696: | |
697: | public static function snakeCase(iterable $array): array |
698: | { |
699: | foreach ($array as $key => $value) { |
700: | $snakeCase[self::getKey($key, $i)] = Str::snake((string) $value); |
701: | } |
702: | return $snakeCase ?? []; |
703: | } |
704: | |
705: | |
706: | |
707: | |
708: | |
709: | |
710: | |
711: | |
712: | |
713: | |
714: | |
715: | |
716: | |
717: | |
718: | |
719: | |
720: | public static function toScalars(iterable $array, $null = null): array |
721: | { |
722: | foreach ($array as $key => $value) { |
723: | if ($value === null) { |
724: | $value = $null; |
725: | } elseif (!is_scalar($value)) { |
726: | if (Test::isStringable($value)) { |
727: | $value = (string) $value; |
728: | } elseif ($value instanceof Jsonable) { |
729: | $value = $value->toJson(Json::ENCODE_FLAGS); |
730: | } else { |
731: | $value = Json::stringify($value); |
732: | } |
733: | } |
734: | $scalars[self::getKey($key, $i)] = $value; |
735: | } |
736: | return $scalars ?? []; |
737: | } |
738: | |
739: | |
740: | |
741: | |
742: | |
743: | |
744: | |
745: | |
746: | |
747: | |
748: | |
749: | |
750: | |
751: | |
752: | |
753: | public static function toStrings(iterable $array, ?string $null = null): array |
754: | { |
755: | foreach ($array as $key => $value) { |
756: | if ($value === null) { |
757: | $value = $null; |
758: | } elseif (is_scalar($value) || Test::isStringable($value)) { |
759: | $value = (string) $value; |
760: | } elseif ($value instanceof Jsonable) { |
761: | $value = $value->toJson(Json::ENCODE_FLAGS); |
762: | } else { |
763: | $value = Json::stringify($value); |
764: | } |
765: | $strings[self::getKey($key, $i)] = $value; |
766: | } |
767: | return $strings ?? []; |
768: | } |
769: | |
770: | |
771: | |
772: | |
773: | |
774: | |
775: | |
776: | |
777: | |
778: | |
779: | public static function offsetOfKey(array $array, $key): int |
780: | { |
781: | $offset = array_flip(array_keys($array))[$key] ?? null; |
782: | if ($offset === null) { |
783: | throw new OutOfRangeException(sprintf('Array key not found: %s', $key)); |
784: | } |
785: | return $offset; |
786: | } |
787: | |
788: | |
789: | |
790: | |
791: | |
792: | |
793: | |
794: | |
795: | |
796: | |
797: | |
798: | |
799: | public static function rename(array $array, $key, $newKey): array |
800: | { |
801: | if (!array_key_exists($key, $array)) { |
802: | throw new OutOfRangeException(sprintf('Array key not found: %s', $key)); |
803: | } |
804: | if ($key === $newKey) { |
805: | return $array; |
806: | } |
807: | return self::spliceByKey($array, $key, 1, [$newKey => $array[$key]]); |
808: | } |
809: | |
810: | |
811: | |
812: | |
813: | |
814: | |
815: | |
816: | |
817: | |
818: | |
819: | |
820: | |
821: | |
822: | public static function splice( |
823: | array $array, |
824: | int $offset, |
825: | ?int $length = null, |
826: | $replacement = [], |
827: | ?array &$replaced = null |
828: | ): array { |
829: | |
830: | if ($length === null) { |
831: | $length = count($array); |
832: | } |
833: | $replaced = array_splice($array, $offset, $length, $replacement); |
834: | return $array; |
835: | } |
836: | |
837: | |
838: | |
839: | |
840: | |
841: | |
842: | |
843: | |
844: | |
845: | |
846: | |
847: | |
848: | |
849: | |
850: | public static function spliceByKey( |
851: | array $array, |
852: | $key, |
853: | ?int $length = null, |
854: | array $replacement = [], |
855: | ?array &$replaced = null |
856: | ): array { |
857: | $keys = array_keys($array); |
858: | $offset = array_flip($keys)[$key] ?? null; |
859: | if ($offset === null) { |
860: | throw new OutOfRangeException(sprintf('Array key not found: %s', $key)); |
861: | } |
862: | |
863: | if ($length === null) { |
864: | $length = count($array); |
865: | } |
866: | $replaced = array_combine( |
867: | array_splice($keys, $offset, $length, array_keys($replacement)), |
868: | array_splice($array, $offset, $length, $replacement), |
869: | ); |
870: | return array_combine($keys, $array); |
871: | } |
872: | |
873: | |
874: | |
875: | |
876: | |
877: | |
878: | |
879: | |
880: | |
881: | |
882: | |
883: | public static function toIndex(iterable $array, $value = true): array |
884: | { |
885: | if (is_array($array)) { |
886: | return array_fill_keys($array, $value); |
887: | } |
888: | foreach ($array as $key) { |
889: | $index[$key] = $value; |
890: | } |
891: | return $index ?? []; |
892: | } |
893: | |
894: | |
895: | |
896: | |
897: | |
898: | |
899: | |
900: | |
901: | |
902: | |
903: | public static function toMap(iterable $array, $key): array |
904: | { |
905: | foreach ($array as $item) { |
906: | $map[ |
907: | is_array($item) || $item instanceof ArrayAccess |
908: | ? $item[$key] |
909: | : $item->$key |
910: | ] = $item; |
911: | } |
912: | return $map ?? []; |
913: | } |
914: | |
915: | |
916: | |
917: | |
918: | |
919: | |
920: | |
921: | |
922: | |
923: | |
924: | |
925: | |
926: | |
927: | |
928: | |
929: | |
930: | |
931: | |
932: | public static function with(iterable $array, callable $callback, $value) |
933: | { |
934: | foreach ($array as $arrKey => $arrValue) { |
935: | $value = $callback($value, $arrValue, $arrKey); |
936: | } |
937: | return $value; |
938: | } |
939: | |
940: | |
941: | |
942: | |
943: | |
944: | |
945: | |
946: | |
947: | |
948: | public static function flatten(iterable $array, int $limit = -1): array |
949: | { |
950: | do { |
951: | $flattened = []; |
952: | $fromIterable = false; |
953: | foreach ($array as $value) { |
954: | if (!is_iterable($value) || !$limit) { |
955: | $flattened[] = $value; |
956: | continue; |
957: | } |
958: | $fromIterable = true; |
959: | foreach ($value as $value) { |
960: | $flattened[] = $value; |
961: | } |
962: | } |
963: | $limit--; |
964: | } while ($fromIterable && $limit && ($array = $flattened)); |
965: | |
966: | return $flattened; |
967: | } |
968: | |
969: | |
970: | |
971: | |
972: | |
973: | |
974: | |
975: | |
976: | |
977: | public static function wrap($value): array |
978: | { |
979: | if ($value === null) { |
980: | return []; |
981: | } |
982: | return is_array($value) ? $value : [$value]; |
983: | } |
984: | |
985: | |
986: | |
987: | |
988: | |
989: | |
990: | |
991: | |
992: | |
993: | public static function wrapList($value): array |
994: | { |
995: | if ($value === null) { |
996: | return []; |
997: | } |
998: | return self::isList($value, true) ? $value : [$value]; |
999: | } |
1000: | |
1001: | |
1002: | |
1003: | |
1004: | |
1005: | |
1006: | |
1007: | |
1008: | |
1009: | public static function unwrap($value, int $limit = -1) |
1010: | { |
1011: | while ( |
1012: | $limit |
1013: | && is_array($value) |
1014: | && count($value) === 1 |
1015: | && array_key_first($value) === 0 |
1016: | ) { |
1017: | $value = $value[0]; |
1018: | $limit--; |
1019: | } |
1020: | return $value; |
1021: | } |
1022: | |
1023: | |
1024: | |
1025: | |
1026: | |
1027: | |
1028: | |
1029: | private static function getKey($key, ?int &$i = null) |
1030: | { |
1031: | if (is_int($key)) { |
1032: | if ($i === null || $key > $i) { |
1033: | $i = $key; |
1034: | } |
1035: | return $key; |
1036: | } |
1037: | if (is_string($key)) { |
1038: | return $key; |
1039: | } |
1040: | if ($i === null) { |
1041: | return $i = 0; |
1042: | } |
1043: | return ++$i; |
1044: | } |
1045: | } |
1046: | |