|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Ark4ne\JsonApi\Support; |
| 4 | + |
| 5 | +use Illuminate\Contracts\Support\Arrayable; |
| 6 | +use Illuminate\Support\Arr as SupportArr; |
| 7 | +use Illuminate\Support\Collection; |
| 8 | + |
| 9 | +class Arr |
| 10 | +{ |
| 11 | + /** |
| 12 | + * @template TKey as array-key |
| 13 | + * @template TValue |
| 14 | + * |
| 15 | + * @param iterable<TKey, TValue> $iterator |
| 16 | + * |
| 17 | + * @return array<TKey, TValue> |
| 18 | + */ |
| 19 | + public static function toArray(iterable $iterator): array |
| 20 | + { |
| 21 | + return (new Collection($iterator))->map(function ($value) { |
| 22 | + if ($value instanceof Arrayable) { |
| 23 | + $value = $value->toArray(); |
| 24 | + } |
| 25 | + |
| 26 | + return is_array($value) ? self::toArray($value) : $value; |
| 27 | + })->all(); |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * @template TKey as array-key |
| 32 | + * @template UKey as array-key |
| 33 | + * @template TValue |
| 34 | + * @template UValue |
| 35 | + * |
| 36 | + * @param iterable<TKey, TValue> $base |
| 37 | + * @param iterable<UKey, UValue> $with |
| 38 | + * |
| 39 | + * @return array<TKey | UKey, TValue | UValue> |
| 40 | + */ |
| 41 | + public static function merge(iterable $base, iterable $with): array |
| 42 | + { |
| 43 | + $base = self::toArray($base); |
| 44 | + |
| 45 | + foreach (self::toArray($with) as $key => $value) { |
| 46 | + $base[$key] = array_merge_recursive( |
| 47 | + $base[$key] ?? [], |
| 48 | + $value |
| 49 | + ); |
| 50 | + } |
| 51 | + |
| 52 | + return self::uniqueRecursive($base); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * @template TKey as array-key |
| 57 | + * @template TValue |
| 58 | + * |
| 59 | + * @param iterable<TKey, TValue> $with |
| 60 | + * |
| 61 | + * @return array<TKey, TValue> |
| 62 | + */ |
| 63 | + public static function wash(iterable $with): array |
| 64 | + { |
| 65 | + return array_filter(self::uniqueRecursive(self::toArray($with))); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * @template TKey as array-key |
| 70 | + * @template TValue |
| 71 | + * |
| 72 | + * @param array<TKey, TValue> $value |
| 73 | + * |
| 74 | + * @return array<TKey, TValue> |
| 75 | + */ |
| 76 | + private static function uniqueRecursive(array $value): array |
| 77 | + { |
| 78 | + return array_map( |
| 79 | + static fn($value) => is_array($value) |
| 80 | + ? self::uniqueRecursive(self::uniqueKeyPreserved($value)) |
| 81 | + : $value, |
| 82 | + $value |
| 83 | + ); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * @template TKey as array-key |
| 88 | + * @template TValue |
| 89 | + * |
| 90 | + * @param array<TKey, TValue> $value |
| 91 | + * |
| 92 | + * @return array<TKey, TValue> |
| 93 | + */ |
| 94 | + private static function uniqueKeyPreserved(array $value): array |
| 95 | + { |
| 96 | + if (SupportArr::isAssoc($value)) { |
| 97 | + $entries = array_map(static fn($k, $v) => [$k, $v], array_keys($value), array_values($value)); |
| 98 | + $entries = array_values(array_unique($entries, SORT_REGULAR)); |
| 99 | + return array_combine( |
| 100 | + array_column($entries, 0), |
| 101 | + array_column($entries, 1) |
| 102 | + ); |
| 103 | + } |
| 104 | + |
| 105 | + return array_values(array_unique($value, SORT_REGULAR)); |
| 106 | + } |
| 107 | +} |
0 commit comments