Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
95.45% |
42 / 44 |
|
50.00% |
2 / 4 |
CRAP | |
0.00% |
0 / 1 |
| TranslationStringSet | |
95.45% |
42 / 44 |
|
50.00% |
2 / 4 |
13 | |
0.00% |
0 / 1 |
| offsetGet | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| toArray | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
1 | |||
| toArrayWithSimplifications | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
3 | |||
| toNestedArray | |
95.65% |
22 / 23 |
|
0.00% |
0 / 1 |
8 | |||
| 1 | <?php |
| 2 | namespace Apie\Core\Translator\Lists; |
| 3 | |
| 4 | use Apie\Core\Lists\ItemSet; |
| 5 | use Apie\Core\Translator\ValueObjects\AbstractTranslation; |
| 6 | use Apie\Core\Translator\ValueObjects\TranslationString; |
| 7 | |
| 8 | /** |
| 9 | * @extends ItemSet<TranslationString|AbstractTranslation> |
| 10 | */ |
| 11 | final class TranslationStringSet extends ItemSet |
| 12 | { |
| 13 | protected bool $mutable = false; |
| 14 | |
| 15 | public function offsetGet(mixed $offset): TranslationString|AbstractTranslation |
| 16 | { |
| 17 | return parent::offsetGet($offset); |
| 18 | } |
| 19 | |
| 20 | public function toArray(): array |
| 21 | { |
| 22 | $list = parent::toArray(); |
| 23 | |
| 24 | usort( |
| 25 | $list, |
| 26 | function (TranslationString|AbstractTranslation $a, TranslationString|AbstractTranslation $b) { |
| 27 | return $a->getSpecifity() <=> $b->getSpecifity(); |
| 28 | } |
| 29 | ); |
| 30 | |
| 31 | return $list; |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * @return array<int, TranslationString|AbstractTranslation> |
| 36 | */ |
| 37 | public function toArrayWithSimplifications(): array |
| 38 | { |
| 39 | $done = []; |
| 40 | $result = []; |
| 41 | $todo = $this->toArray(); |
| 42 | |
| 43 | while (!empty($todo)) { |
| 44 | $item = array_pop($todo); |
| 45 | $key = $item->toNative(); |
| 46 | if (!empty($done[$key])) { |
| 47 | continue; |
| 48 | } |
| 49 | $done[$key] = true; |
| 50 | $result[] = $item; |
| 51 | $todo = [...$todo, ...$item->getSimplifications()]; |
| 52 | } |
| 53 | |
| 54 | return $result; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * @return array<array-key, mixed> |
| 59 | */ |
| 60 | public function toNestedArray(): array |
| 61 | { |
| 62 | /** @var array<array-key, mixed> $result */ |
| 63 | $result = []; |
| 64 | |
| 65 | foreach ($this->toArrayWithSimplifications() as $translationString) { |
| 66 | /** @var TranslationString|AbstractTranslation $translationString */ |
| 67 | $key = $translationString->toNative(); |
| 68 | $value = $translationString->getFallbackText(); |
| 69 | $parts = explode('.', $key); |
| 70 | $current = &$result; |
| 71 | |
| 72 | foreach ($parts as $index => $part) { |
| 73 | $isLast = $index === array_key_last($parts); |
| 74 | |
| 75 | if ($isLast) { |
| 76 | if (!isset($current[$part])) { |
| 77 | $current[$part] = $value; |
| 78 | } elseif (is_array($current[$part])) { |
| 79 | // Node already became an array earlier. |
| 80 | $current[$part][''] = $value; |
| 81 | } else { |
| 82 | // Duplicate key; overwrite or throw, depending on your needs. |
| 83 | $current[$part] = $value; |
| 84 | } |
| 85 | } else { |
| 86 | if (!isset($current[$part])) { |
| 87 | $current[$part] = []; |
| 88 | } elseif (!is_array($current[$part])) { |
| 89 | // Convert existing scalar into an array. |
| 90 | $current[$part] = [ |
| 91 | '' => $current[$part], |
| 92 | ]; |
| 93 | } |
| 94 | |
| 95 | $current = &$current[$part]; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | unset($current); |
| 100 | } |
| 101 | |
| 102 | return $result; |
| 103 | } |
| 104 | } |