PluginProbe
Independent Analytics – WordPress Analytics Plugin / 2.9.2
Independent Analytics – WordPress Analytics Plugin v2.9.2
2.15.5 2.15.4 2.15.3 2.15.2 2.15.1 2.15.0 2.14.10 trunk 1.1 1.10 1.10.1 1.11 1.12 1.13 1.14 1.15 1.16 1.17 1.17.1 1.17.2 1.17.3 1.17.4 1.18 1.18.1 1.19.0 All 120 releases
independent-analytics / vendor / symfony / translation / MessageCatalogue.php
MessageCatalogue.php
267 lines 8.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11 namespace IAWPSCOPED\Symfony\Component\Translation;
12
13 use IAWPSCOPED\Symfony\Component\Config\Resource\ResourceInterface;
14 use IAWPSCOPED\Symfony\Component\Translation\Exception\LogicException;
15 /**
16 * @author Fabien Potencier <fabien@symfony.com>
17 * @internal
18 */
19 class MessageCatalogue implements MessageCatalogueInterface, MetadataAwareInterface
20 {
21 private $messages = [];
22 private $metadata = [];
23 private $resources = [];
24 private $locale;
25 private $fallbackCatalogue;
26 private $parent;
27 /**
28 * @param array $messages An array of messages classified by domain
29 */
30 public function __construct(string $locale, array $messages = [])
31 {
32 $this->locale = $locale;
33 $this->messages = $messages;
34 }
35 /**
36 * {@inheritdoc}
37 */
38 public function getLocale()
39 {
40 return $this->locale;
41 }
42 /**
43 * {@inheritdoc}
44 */
45 public function getDomains()
46 {
47 $domains = [];
48 foreach ($this->messages as $domain => $messages) {
49 if (\str_ends_with($domain, self::INTL_DOMAIN_SUFFIX)) {
50 $domain = \substr($domain, 0, -\strlen(self::INTL_DOMAIN_SUFFIX));
51 }
52 $domains[$domain] = $domain;
53 }
54 return \array_values($domains);
55 }
56 /**
57 * {@inheritdoc}
58 */
59 public function all(?string $domain = null)
60 {
61 if (null !== $domain) {
62 // skip messages merge if intl-icu requested explicitly
63 if (\str_ends_with($domain, self::INTL_DOMAIN_SUFFIX)) {
64 return $this->messages[$domain] ?? [];
65 }
66 return ($this->messages[$domain . self::INTL_DOMAIN_SUFFIX] ?? []) + ($this->messages[$domain] ?? []);
67 }
68 $allMessages = [];
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 return $allMessages;
78 }
79 /**
80 * {@inheritdoc}
81 */
82 public function set(string $id, string $translation, string $domain = 'messages')
83 {
84 $this->add([$id => $translation], $domain);
85 }
86 /**
87 * {@inheritdoc}
88 */
89 public function has(string $id, string $domain = 'messages')
90 {
91 if (isset($this->messages[$domain][$id]) || isset($this->messages[$domain . self::INTL_DOMAIN_SUFFIX][$id])) {
92 return \true;
93 }
94 if (null !== $this->fallbackCatalogue) {
95 return $this->fallbackCatalogue->has($id, $domain);
96 }
97 return \false;
98 }
99 /**
100 * {@inheritdoc}
101 */
102 public function defines(string $id, string $domain = 'messages')
103 {
104 return isset($this->messages[$domain][$id]) || isset($this->messages[$domain . self::INTL_DOMAIN_SUFFIX][$id]);
105 }
106 /**
107 * {@inheritdoc}
108 */
109 public function get(string $id, string $domain = 'messages')
110 {
111 if (isset($this->messages[$domain . self::INTL_DOMAIN_SUFFIX][$id])) {
112 return $this->messages[$domain . self::INTL_DOMAIN_SUFFIX][$id];
113 }
114 if (isset($this->messages[$domain][$id])) {
115 return $this->messages[$domain][$id];
116 }
117 if (null !== $this->fallbackCatalogue) {
118 return $this->fallbackCatalogue->get($id, $domain);
119 }
120 return $id;
121 }
122 /**
123 * {@inheritdoc}
124 */
125 public function replace(array $messages, string $domain = 'messages')
126 {
127 unset($this->messages[$domain], $this->messages[$domain . self::INTL_DOMAIN_SUFFIX]);
128 $this->add($messages, $domain);
129 }
130 /**
131 * {@inheritdoc}
132 */
133 public function add(array $messages, string $domain = 'messages')
134 {
135 $altDomain = \str_ends_with($domain, self::INTL_DOMAIN_SUFFIX) ? \substr($domain, 0, -\strlen(self::INTL_DOMAIN_SUFFIX)) : $domain . self::INTL_DOMAIN_SUFFIX;
136 foreach ($messages as $id => $message) {
137 unset($this->messages[$altDomain][$id]);
138 $this->messages[$domain][$id] = $message;
139 }
140 if ([] === ($this->messages[$altDomain] ?? null)) {
141 unset($this->messages[$altDomain]);
142 }
143 }
144 /**
145 * {@inheritdoc}
146 */
147 public function addCatalogue(MessageCatalogueInterface $catalogue)
148 {
149 if ($catalogue->getLocale() !== $this->locale) {
150 throw new LogicException(\sprintf('Cannot add a catalogue for locale "%s" as the current locale for this catalogue is "%s".', $catalogue->getLocale(), $this->locale));
151 }
152 foreach ($catalogue->all() as $domain => $messages) {
153 if ($intlMessages = $catalogue->all($domain . self::INTL_DOMAIN_SUFFIX)) {
154 $this->add($intlMessages, $domain . self::INTL_DOMAIN_SUFFIX);
155 $messages = \array_diff_key($messages, $intlMessages);
156 }
157 $this->add($messages, $domain);
158 }
159 foreach ($catalogue->getResources() as $resource) {
160 $this->addResource($resource);
161 }
162 if ($catalogue instanceof MetadataAwareInterface) {
163 $metadata = $catalogue->getMetadata('', '');
164 $this->addMetadata($metadata);
165 }
166 }
167 /**
168 * {@inheritdoc}
169 */
170 public function addFallbackCatalogue(MessageCatalogueInterface $catalogue)
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 $c = $this;
180 do {
181 if ($c->getLocale() === $catalogue->getLocale()) {
182 throw new LogicException(\sprintf('Circular reference detected when adding a fallback catalogue for locale "%s".', $catalogue->getLocale()));
183 }
184 foreach ($catalogue->getResources() as $resource) {
185 $c->addResource($resource);
186 }
187 } while ($c = $c->parent);
188 $catalogue->parent = $this;
189 $this->fallbackCatalogue = $catalogue;
190 foreach ($catalogue->getResources() as $resource) {
191 $this->addResource($resource);
192 }
193 }
194 /**
195 * {@inheritdoc}
196 */
197 public function getFallbackCatalogue()
198 {
199 return $this->fallbackCatalogue;
200 }
201 /**
202 * {@inheritdoc}
203 */
204 public function getResources()
205 {
206 return \array_values($this->resources);
207 }
208 /**
209 * {@inheritdoc}
210 */
211 public function addResource(ResourceInterface $resource)
212 {
213 $this->resources[$resource->__toString()] = $resource;
214 }
215 /**
216 * {@inheritdoc}
217 */
218 public function getMetadata(string $key = '', string $domain = 'messages')
219 {
220 if ('' == $domain) {
221 return $this->metadata;
222 }
223 if (isset($this->metadata[$domain])) {
224 if ('' == $key) {
225 return $this->metadata[$domain];
226 }
227 if (isset($this->metadata[$domain][$key])) {
228 return $this->metadata[$domain][$key];
229 }
230 }
231 return null;
232 }
233 /**
234 * {@inheritdoc}
235 */
236 public function setMetadata(string $key, $value, string $domain = 'messages')
237 {
238 $this->metadata[$domain][$key] = $value;
239 }
240 /**
241 * {@inheritdoc}
242 */
243 public function deleteMetadata(string $key = '', string $domain = 'messages')
244 {
245 if ('' == $domain) {
246 $this->metadata = [];
247 } elseif ('' == $key) {
248 unset($this->metadata[$domain]);
249 } else {
250 unset($this->metadata[$domain][$key]);
251 }
252 }
253 /**
254 * Adds current values with the new values.
255 *
256 * @param array $values Values to add
257 */
258 private function addMetadata(array $values)
259 {
260 foreach ($values as $domain => $keys) {
261 foreach ($keys as $key => $value) {
262 $this->setMetadata($key, $value, $domain);
263 }
264 }
265 }
266 }
267