Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / vendor / symfony / translation / MessageCatalogue.php
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
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 <fabien@symfony.com>
19  */
20 class MessageCatalogue implements MessageCatalogueInterface, MetadataAwareInterface
21 {
22     private $messages = array();
23     private $metadata = array();
24     private $resources = array();
25     private $locale;
26     private $fallbackCatalogue;
27     private $parent;
28
29     /**
30      * @param string $locale   The locale
31      * @param array  $messages An array of messages classified by domain
32      */
33     public function __construct($locale, array $messages = array())
34     {
35         $this->locale = $locale;
36         $this->messages = $messages;
37     }
38
39     /**
40      * {@inheritdoc}
41      */
42     public function getLocale()
43     {
44         return $this->locale;
45     }
46
47     /**
48      * {@inheritdoc}
49      */
50     public function getDomains()
51     {
52         return array_keys($this->messages);
53     }
54
55     /**
56      * {@inheritdoc}
57      */
58     public function all($domain = null)
59     {
60         if (null === $domain) {
61             return $this->messages;
62         }
63
64         return isset($this->messages[$domain]) ? $this->messages[$domain] : array();
65     }
66
67     /**
68      * {@inheritdoc}
69      */
70     public function set($id, $translation, $domain = 'messages')
71     {
72         $this->add(array($id => $translation), $domain);
73     }
74
75     /**
76      * {@inheritdoc}
77      */
78     public function has($id, $domain = 'messages')
79     {
80         if (isset($this->messages[$domain][$id])) {
81             return true;
82         }
83
84         if (null !== $this->fallbackCatalogue) {
85             return $this->fallbackCatalogue->has($id, $domain);
86         }
87
88         return false;
89     }
90
91     /**
92      * {@inheritdoc}
93      */
94     public function defines($id, $domain = 'messages')
95     {
96         return isset($this->messages[$domain][$id]);
97     }
98
99     /**
100      * {@inheritdoc}
101      */
102     public function get($id, $domain = 'messages')
103     {
104         if (isset($this->messages[$domain][$id])) {
105             return $this->messages[$domain][$id];
106         }
107
108         if (null !== $this->fallbackCatalogue) {
109             return $this->fallbackCatalogue->get($id, $domain);
110         }
111
112         return $id;
113     }
114
115     /**
116      * {@inheritdoc}
117      */
118     public function replace($messages, $domain = 'messages')
119     {
120         $this->messages[$domain] = array();
121
122         $this->add($messages, $domain);
123     }
124
125     /**
126      * {@inheritdoc}
127      */
128     public function add($messages, $domain = 'messages')
129     {
130         if (!isset($this->messages[$domain])) {
131             $this->messages[$domain] = $messages;
132         } else {
133             $this->messages[$domain] = array_replace($this->messages[$domain], $messages);
134         }
135     }
136
137     /**
138      * {@inheritdoc}
139      */
140     public function addCatalogue(MessageCatalogueInterface $catalogue)
141     {
142         if ($catalogue->getLocale() !== $this->locale) {
143             throw new LogicException(sprintf('Cannot add a catalogue for locale "%s" as the current locale for this catalogue is "%s"', $catalogue->getLocale(), $this->locale));
144         }
145
146         foreach ($catalogue->all() as $domain => $messages) {
147             $this->add($messages, $domain);
148         }
149
150         foreach ($catalogue->getResources() as $resource) {
151             $this->addResource($resource);
152         }
153
154         if ($catalogue instanceof MetadataAwareInterface) {
155             $metadata = $catalogue->getMetadata('', '');
156             $this->addMetadata($metadata);
157         }
158     }
159
160     /**
161      * {@inheritdoc}
162      */
163     public function addFallbackCatalogue(MessageCatalogueInterface $catalogue)
164     {
165         // detect circular references
166         $c = $catalogue;
167         while ($c = $c->getFallbackCatalogue()) {
168             if ($c->getLocale() === $this->getLocale()) {
169                 throw new LogicException(sprintf('Circular reference detected when adding a fallback catalogue for locale "%s".', $catalogue->getLocale()));
170             }
171         }
172
173         $c = $this;
174         do {
175             if ($c->getLocale() === $catalogue->getLocale()) {
176                 throw new LogicException(sprintf('Circular reference detected when adding a fallback catalogue for locale "%s".', $catalogue->getLocale()));
177             }
178
179             foreach ($catalogue->getResources() as $resource) {
180                 $c->addResource($resource);
181             }
182         } while ($c = $c->parent);
183
184         $catalogue->parent = $this;
185         $this->fallbackCatalogue = $catalogue;
186
187         foreach ($catalogue->getResources() as $resource) {
188             $this->addResource($resource);
189         }
190     }
191
192     /**
193      * {@inheritdoc}
194      */
195     public function getFallbackCatalogue()
196     {
197         return $this->fallbackCatalogue;
198     }
199
200     /**
201      * {@inheritdoc}
202      */
203     public function getResources()
204     {
205         return array_values($this->resources);
206     }
207
208     /**
209      * {@inheritdoc}
210      */
211     public function addResource(ResourceInterface $resource)
212     {
213         $this->resources[$resource->__toString()] = $resource;
214     }
215
216     /**
217      * {@inheritdoc}
218      */
219     public function getMetadata($key = '', $domain = 'messages')
220     {
221         if ('' == $domain) {
222             return $this->metadata;
223         }
224
225         if (isset($this->metadata[$domain])) {
226             if ('' == $key) {
227                 return $this->metadata[$domain];
228             }
229
230             if (isset($this->metadata[$domain][$key])) {
231                 return $this->metadata[$domain][$key];
232             }
233         }
234     }
235
236     /**
237      * {@inheritdoc}
238      */
239     public function setMetadata($key, $value, $domain = 'messages')
240     {
241         $this->metadata[$domain][$key] = $value;
242     }
243
244     /**
245      * {@inheritdoc}
246      */
247     public function deleteMetadata($key = '', $domain = 'messages')
248     {
249         if ('' == $domain) {
250             $this->metadata = array();
251         } elseif ('' == $key) {
252             unset($this->metadata[$domain]);
253         } else {
254             unset($this->metadata[$domain][$key]);
255         }
256     }
257
258     /**
259      * Adds current values with the new values.
260      *
261      * @param array $values Values to add
262      */
263     private function addMetadata(array $values)
264     {
265         foreach ($values as $domain => $keys) {
266             foreach ($keys as $key => $value) {
267                 $this->setMetadata($key, $value, $domain);
268             }
269         }
270     }
271 }