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