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