1 | <?php namespace Limoncello\l10n\Messages; |
||
26 | class BundleEncoder implements BundleEncoderInterface |
||
27 | { |
||
28 | /** |
||
29 | * @var array |
||
30 | */ |
||
31 | private $bundles = []; |
||
32 | |||
33 | /** |
||
34 | * @inheritdoc |
||
35 | */ |
||
36 | 6 | public function addBundle(ResourceBundleInterface $bundle): BundleEncoderInterface |
|
37 | { |
||
38 | 6 | $this->bundles[$bundle->getLocale()][$bundle->getNamespace()] = $bundle; |
|
39 | |||
40 | 6 | return $this; |
|
41 | } |
||
42 | |||
43 | /** |
||
44 | * @inheritdoc |
||
45 | */ |
||
46 | 6 | public function getStorageData(string $defaultLocale): array |
|
47 | { |
||
48 | 6 | $defaultNamespaces = $this->getNamespaces($defaultLocale); |
|
49 | |||
50 | 6 | $data = []; |
|
51 | 6 | foreach ($this->getLocales() as $locale) { |
|
52 | 6 | $localizedNamespaces = $this->getNamespaces($locale); |
|
53 | 6 | $combinedNamespaces = $defaultNamespaces + $localizedNamespaces; |
|
54 | 6 | foreach ($combinedNamespaces as $namespace) { |
|
55 | 6 | $bundle = $this->getBundle($locale, $namespace); |
|
56 | 6 | $bundle = $bundle !== null ? $bundle : $this->getBundle($defaultLocale, $namespace); |
|
57 | 6 | $data[$locale][$namespace] = $defaultLocale === $locale ? $this->encodeBundle($bundle) : |
|
58 | 6 | $this->encodeMergedBundles($bundle, $this->getBundle($defaultLocale, $namespace)); |
|
59 | } |
||
60 | } |
||
61 | |||
62 | return [ |
||
63 | 6 | BundleStorage::INDEX_DEFAULT_LOCALE => $defaultLocale, |
|
64 | 6 | BundleStorage::INDEX_DATA => $data |
|
65 | ]; |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * @return array |
||
70 | */ |
||
71 | 6 | protected function getBundles(): array |
|
75 | |||
76 | /** |
||
77 | * @param ResourceBundleInterface $bundle |
||
78 | * |
||
79 | * @return array |
||
80 | */ |
||
81 | 6 | private function encodeBundle(ResourceBundleInterface $bundle): array |
|
90 | |||
91 | /** |
||
92 | * @param string $locale |
||
93 | * @param string $namespace |
||
94 | * |
||
95 | * @return null|ResourceBundleInterface |
||
96 | */ |
||
97 | 6 | private function getBundle(string $locale, string $namespace) |
|
108 | |||
109 | /** |
||
110 | * @param ResourceBundleInterface $localizedBundle |
||
111 | * @param ResourceBundleInterface|null $defaultBundle |
||
112 | * |
||
113 | * @return array |
||
114 | */ |
||
115 | 6 | private function encodeMergedBundles( |
|
116 | ResourceBundleInterface $localizedBundle, |
||
117 | ResourceBundleInterface $defaultBundle = null |
||
118 | ): array { |
||
119 | 6 | if ($defaultBundle === null) { |
|
120 | // there is no default bundle for this localized one |
||
121 | 5 | return $this->encodeBundle($localizedBundle); |
|
122 | } |
||
123 | |||
124 | 2 | $localizedKeys = $localizedBundle->getKeys(); |
|
125 | 2 | $defaultKeys = $defaultBundle->getKeys(); |
|
126 | |||
127 | 2 | $commonKeys = array_intersect($localizedKeys, $defaultKeys); |
|
128 | 2 | $localizedOnlyKeys = array_diff($localizedKeys, $commonKeys); |
|
129 | 2 | $defaultOnlyKeys = array_diff($defaultKeys, $commonKeys); |
|
130 | |||
131 | 2 | $encodedBundle = []; |
|
132 | |||
133 | 2 | foreach ($commonKeys as $key) { |
|
134 | 2 | $encodedBundle[$key] = $this->getBundleValue($localizedBundle, $key); |
|
135 | } |
||
136 | 2 | foreach ($localizedOnlyKeys as $key) { |
|
137 | 1 | $encodedBundle[$key] = $this->getBundleValue($localizedBundle, $key); |
|
138 | } |
||
139 | 2 | foreach ($defaultOnlyKeys as $key) { |
|
140 | 1 | $encodedBundle[$key] = $this->getBundleValue($defaultBundle, $key); |
|
141 | } |
||
142 | |||
143 | 2 | return $encodedBundle; |
|
144 | } |
||
145 | |||
146 | /** |
||
147 | * @return array |
||
148 | */ |
||
149 | 6 | private function getLocales(): array |
|
153 | |||
154 | /** |
||
155 | * @param string $locale |
||
156 | * |
||
157 | * @return bool |
||
158 | */ |
||
159 | 6 | private function hasLocale(string $locale): bool |
|
166 | |||
167 | /** |
||
168 | * @param string $locale |
||
169 | * |
||
170 | * @return string[] |
||
171 | */ |
||
172 | 6 | private function getNamespaces(string $locale): array |
|
178 | |||
179 | /** |
||
180 | * @param string $locale |
||
181 | * |
||
182 | * @return void |
||
183 | */ |
||
184 | 6 | private function assertLocale(string $locale) |
|
188 | |||
189 | /** |
||
190 | * @param string $namespace |
||
191 | * |
||
192 | * @return void |
||
193 | */ |
||
194 | 6 | private function assertNamespace(string $namespace) |
|
198 | |||
199 | /** |
||
200 | * @param ResourceBundleInterface $bundle |
||
201 | * @param string $key |
||
202 | * |
||
203 | * @return string[] |
||
204 | */ |
||
205 | 6 | private function getBundleValue(ResourceBundleInterface $bundle, $key): array |
|
212 | } |
||
213 |