|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace Utilitte\Php; |
| 4 | + |
| 5 | +use InvalidArgumentException; |
| 6 | +use LogicException; |
| 7 | +use Traversable; |
| 8 | +use Utilitte\Php\ValueObject\ArraySynchronized; |
| 9 | + |
| 10 | +class Arrays |
| 11 | +{ |
| 12 | + |
| 13 | + /** |
| 14 | + * @param mixed[] $defaults |
| 15 | + * @param mixed[] $array |
| 16 | + * @return mixed[] |
| 17 | + */ |
| 18 | + public static function defaults(array $defaults, iterable $array): array |
| 19 | + { |
| 20 | + foreach ($array as $key => $value) { |
| 21 | + if (!array_key_exists($key, $defaults)) { |
| 22 | + throw new LogicException( |
| 23 | + sprintf('Key %s is not allowed in array', $key) |
| 24 | + ); |
| 25 | + } |
| 26 | + |
| 27 | + $defaults[$key] = $value; |
| 28 | + } |
| 29 | + |
| 30 | + return $defaults; |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * @param mixed[] $array |
| 35 | + * @return mixed |
| 36 | + */ |
| 37 | + public static function firstValue(array $array) |
| 38 | + { |
| 39 | + $key = array_key_first($array); |
| 40 | + |
| 41 | + if ($key === null) { |
| 42 | + throw new InvalidArgumentException('Given array is empty'); |
| 43 | + } |
| 44 | + |
| 45 | + return $array[$key]; |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * @param mixed[] $previous |
| 50 | + * @param mixed[] $current |
| 51 | + */ |
| 52 | + public static function synchronize( |
| 53 | + iterable $previous, |
| 54 | + iterable $current, |
| 55 | + ?callable $comparator = null |
| 56 | + ): ArraySynchronized |
| 57 | + { |
| 58 | + $comparator ??= [self::class, 'strictComparator']; |
| 59 | + |
| 60 | + $added = self::iterableToArray($current); |
| 61 | + $removed = self::iterableToArray($previous); |
| 62 | + $result = $added; |
| 63 | + |
| 64 | + foreach ($removed as $key => $value) { |
| 65 | + foreach ($added as $key1 => $value1) { |
| 66 | + if ($comparator($value, $value1)) { |
| 67 | + unset($removed[$key]); |
| 68 | + unset($added[$key1]); |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + return new ArraySynchronized($added, $removed, $result); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * @param mixed[] $iterable |
| 78 | + * @return mixed[] |
| 79 | + */ |
| 80 | + public static function iterableToArray(iterable $iterable): array |
| 81 | + { |
| 82 | + return $iterable instanceof Traversable ? iterator_to_array($iterable) : (array) $iterable; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * @param mixed[] $values |
| 87 | + * @param string|int $key |
| 88 | + * @param string|int $value |
| 89 | + * @return mixed[] |
| 90 | + */ |
| 91 | + public static function twoDimensionArrayToAssociativeArray(array $values, $key, $value): array |
| 92 | + { |
| 93 | + $return = []; |
| 94 | + |
| 95 | + foreach ($values as $itemKey => $item) { |
| 96 | + if (!array_key_exists($key, $item)) { |
| 97 | + throw new InvalidArgumentException( |
| 98 | + sprintf('Key "key" %s not exists in array[%s]', (string) $key, (string) $itemKey) |
| 99 | + ); |
| 100 | + } |
| 101 | + |
| 102 | + if (!array_key_exists($value, $item)) { |
| 103 | + throw new InvalidArgumentException( |
| 104 | + sprintf('Key "value" %s not exists in array[%s]', (string) $value, (string) $itemKey) |
| 105 | + ); |
| 106 | + } |
| 107 | + |
| 108 | + $return[$item[$key]] = $item[$value]; |
| 109 | + } |
| 110 | + |
| 111 | + return $return; |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * @param mixed $first |
| 116 | + * @param mixed $second |
| 117 | + * @phpcsSuppress SlevomatCodingStandard.Classes.UnusedPrivateElements.UnusedMethod |
| 118 | + */ |
| 119 | + private static function strictComparator($first, $second): bool |
| 120 | + { |
| 121 | + return $first === $second; |
| 122 | + } |
| 123 | + |
| 124 | +} |
0 commit comments