Debian Codegraph @

lib/3rdParty/composer/symfony/translation/MessageCatalogue.php

raw symbols SCIP
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <[email protected]>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Symfony\Component\Translation;
13
14 use Symfony\Component\Config\Resource\ResourceInterface;
15 use Symfony\Component\Translation\Exception\LogicException;
16
17 /**
18 * @author Fabien Potencier <[email protected]>
19 */
20 class MessageCatalogue implements MessageCatalogueInterface, MetadataAwareInterface, CatalogueMetadataAwareInterface
21 {
22 private array $metadata = [];
23 private array $catalogueMetadata = [];
24 private array $resources = [];
25 private ?MessageCatalogueInterface $fallbackCatalogue = null;
26 private ?self $parent = null;
27
28 /**
29 * @param array $messages An array of messages classified by domain
30 */
31 public function __construct(
32 private string $locale,
33 private array $messages = [],
34 ) {
35 }
36
37 public function getLocale(): string
38 {
39 return $this->locale;
40 }
41
42 public function getDomains(): array
43 {
44 $domains = [];
45
46 foreach ($this->messages as $domain => $messages) {
47 if (str_ends_with($domain, self::INTL_DOMAIN_SUFFIX)) {
48 $domain = substr($domain, 0, -\strlen(self::INTL_DOMAIN_SUFFIX));
49 }
50 $domains[$domain] = $domain;
51 }
52
53 return array_values($domains);
54 }
55
56 public function all(?string $domain = null): array
57 {
58 if (null !== $domain) {
59 // skip messages merge if intl-icu requested explicitly
60 if (str_ends_with($domain, self::INTL_DOMAIN_SUFFIX)) {
61 return $this->messages[$domain] ?? [];
62 }
63
64 return ($this->messages[$domain.self::INTL_DOMAIN_SUFFIX] ?? []) + ($this->messages[$domain] ?? []);
65 }
66
67 $allMessages = [];
68
69 foreach ($this->messages as $domain => $messages) {
70 if (str_ends_with($domain, self::INTL_DOMAIN_SUFFIX)) {
71 $domain = substr($domain, 0, -\strlen(self::INTL_DOMAIN_SUFFIX));
72 $allMessages[$domain] = $messages + ($allMessages[$domain] ?? []);
73 } else {
74 $allMessages[$domain] = ($allMessages[$domain] ?? []) + $messages;
75 }
76 }
77
78 return $allMessages;
79 }
80
81 public function set(string $id, string $translation, string $domain = 'messages'): void
82 {
83 $this->add([$id => $translation], $domain);
84 }
85
86 public function has(string $id, string $domain = 'messages'): bool
87 {
88 if (isset($this->messages[$domain][$id]) || isset($this->messages[$domain.self::INTL_DOMAIN_SUFFIX][$id])) {
89 return true;
90 }
91
92 if (null !== $this->fallbackCatalogue) {
93 return $this->fallbackCatalogue->has($id, $domain);
94 }
95
96 return false;
97 }
98
99 public function defines(string $id, string $domain = 'messages'): bool
100 {
101 return isset($this->messages[$domain][$id]) || isset($this->messages[$domain.self::INTL_DOMAIN_SUFFIX][$id]);
102 }
103
104 public function get(string $id, string $domain = 'messages'): string
105 {
106 if (isset($this->messages[$domain.self::INTL_DOMAIN_SUFFIX][$id])) {
107 return $this->messages[$domain.self::INTL_DOMAIN_SUFFIX][$id];
108 }
109
110 if (isset($this->messages[$domain][$id])) {
111 return $this->messages[$domain][$id];
112 }
113
114 if (null !== $this->fallbackCatalogue) {
115 return $this->fallbackCatalogue->get($id, $domain);
116 }
117
118 return $id;
119 }
120
121 public function replace(array $messages, string $domain = 'messages'): void
122 {
123 unset($this->messages[$domain], $this->messages[$domain.self::INTL_DOMAIN_SUFFIX]);
124
125 $this->add($messages, $domain);
126 }
127
128 public function add(array $messages, string $domain = 'messages'): void
129 {
130 $altDomain = str_ends_with($domain, self::INTL_DOMAIN_SUFFIX) ? substr($domain, 0, -\strlen(self::INTL_DOMAIN_SUFFIX)) : $domain.self::INTL_DOMAIN_SUFFIX;
131 foreach ($messages as $id => $message) {
132 unset($this->messages[$altDomain][$id]);
133 $this->messages[$domain][$id] = $message;
134 }
135
136 if ([] === ($this->messages[$altDomain] ?? null)) {
137 unset($this->messages[$altDomain]);
138 }
139 }
140
141 public function addCatalogue(MessageCatalogueInterface $catalogue): void
142 {
143 if ($catalogue->getLocale() !== $this->locale) {
144 throw new LogicException(\sprintf('Cannot add a catalogue for locale "%s" as the current locale for this catalogue is "%s".', $catalogue->getLocale(), $this->locale));
145 }
146
147 foreach ($catalogue->all() as $domain => $messages) {
148 if ($intlMessages = $catalogue->all($domain.self::INTL_DOMAIN_SUFFIX)) {
149 $this->add($intlMessages, $domain.self::INTL_DOMAIN_SUFFIX);
150 $messages = array_diff_key($messages, $intlMessages);
151 }
152 $this->add($messages, $domain);
153 }
154
155 foreach ($catalogue->getResources() as $resource) {
156 $this->addResource($resource);
157 }
158
159 if ($catalogue instanceof MetadataAwareInterface) {
160 $metadata = $catalogue->getMetadata('', '');
161 $this->addMetadata($metadata);
162 }
163
164 if ($catalogue instanceof CatalogueMetadataAwareInterface) {
165 $catalogueMetadata = $catalogue->getCatalogueMetadata('', '');
166 $this->addCatalogueMetadata($catalogueMetadata);
167 }
168 }
169
170 public function addFallbackCatalogue(MessageCatalogueInterface $catalogue): void
171 {
172 // detect circular references
173 $c = $catalogue;
174 while ($c = $c->getFallbackCatalogue()) {
175 if ($c->getLocale() === $this->getLocale()) {
176 throw new LogicException(\sprintf('Circular reference detected when adding a fallback catalogue for locale "%s".', $catalogue->getLocale()));
177 }
178 }
179
180 $c = $this;
181 do {
182 if ($c->getLocale() === $catalogue->getLocale()) {
183 throw new LogicException(\sprintf('Circular reference detected when adding a fallback catalogue for locale "%s".', $catalogue->getLocale()));
184 }
185
186 foreach ($catalogue->getResources() as $resource) {
187 $c->addResource($resource);
188 }
189 } while ($c = $c->parent);
190
191 $catalogue->parent = $this;
192 $this->fallbackCatalogue = $catalogue;
193
194 foreach ($catalogue->getResources() as $resource) {
195 $this->addResource($resource);
196 }
197 }
198
199 public function getFallbackCatalogue(): ?MessageCatalogueInterface
200 {
201 return $this->fallbackCatalogue;
202 }
203
204 public function getResources(): array
205 {
206 return array_values($this->resources);
207 }
208
209 public function addResource(ResourceInterface $resource): void
210 {
211 $this->resources[$resource->__toString()] = $resource;
212 }
213
214 public function getMetadata(string $key = '', string $domain = 'messages'): mixed
215 {
216 if ('' == $domain) {
217 return $this->metadata;
218 }
219
220 if (isset($this->metadata[$domain.self::INTL_DOMAIN_SUFFIX])) {
221 if ('' === $key) {
222 return $this->metadata[$domain.self::INTL_DOMAIN_SUFFIX];
223 }
224
225 if (isset($this->metadata[$domain.self::INTL_DOMAIN_SUFFIX][$key])) {
226 return $this->metadata[$domain.self::INTL_DOMAIN_SUFFIX][$key];
227 }
228 }
229
230 if (isset($this->metadata[$domain])) {
231 if ('' == $key) {
232 return $this->metadata[$domain];
233 }
234
235 if (isset($this->metadata[$domain][$key])) {
236 return $this->metadata[$domain][$key];
237 }
238 }
239
240 return null;
241 }
242
243 public function setMetadata(string $key, mixed $value, string $domain = 'messages'): void
244 {
245 $this->metadata[$domain][$key] = $value;
246 }
247
248 public function deleteMetadata(string $key = '', string $domain = 'messages'): void
249 {
250 if ('' == $domain) {
251 $this->metadata = [];
252 } elseif ('' == $key) {
253 unset($this->metadata[$domain]);
254 } else {
255 unset($this->metadata[$domain][$key]);
256 }
257 }
258
259 public function getCatalogueMetadata(string $key = '', string $domain = 'messages'): mixed
260 {
261 if (!$domain) {
262 return $this->catalogueMetadata;
263 }
264
265 if (isset($this->catalogueMetadata[$domain])) {
266 if (!$key) {
267 return $this->catalogueMetadata[$domain];
268 }
269
270 if (isset($this->catalogueMetadata[$domain][$key])) {
271 return $this->catalogueMetadata[$domain][$key];
272 }
273 }
274
275 return null;
276 }
277
278 public function setCatalogueMetadata(string $key, mixed $value, string $domain = 'messages'): void
279 {
280 $this->catalogueMetadata[$domain][$key] = $value;
281 }
282
283 public function deleteCatalogueMetadata(string $key = '', string $domain = 'messages'): void
284 {
285 if (!$domain) {
286 $this->catalogueMetadata = [];
287 } elseif (!$key) {
288 unset($this->catalogueMetadata[$domain]);
289 } else {
290 unset($this->catalogueMetadata[$domain][$key]);
291 }
292 }
293
294 /**
295 * Adds current values with the new values.
296 *
297 * @param array $values Values to add
298 */
299 private function addMetadata(array $values): void
300 {
301 foreach ($values as $domain => $keys) {
302 foreach ($keys as $key => $value) {
303 $this->setMetadata($key, $value, $domain);
304 }
305 }
306 }
307
308 private function addCatalogueMetadata(array $values): void
309 {
310 foreach ($values as $domain => $keys) {
311 foreach ($keys as $key => $value) {
312 $this->setCatalogueMetadata($key, $value, $domain);
313 }
314 }
315 }
316 }