composer package updates
[openemr.git] / vendor / symfony / translation / MessageCatalogue.php
blobc82b73e19903990be3ad24f34ef01c657330bcfb
1 <?php
3 /*
4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Translation;
14 use Symfony\Component\Config\Resource\ResourceInterface;
15 use Symfony\Component\Translation\Exception\LogicException;
17 /**
18 * MessageCatalogue.
20 * @author Fabien Potencier <fabien@symfony.com>
22 class MessageCatalogue implements MessageCatalogueInterface, MetadataAwareInterface
24 private $messages = array();
25 private $metadata = array();
26 private $resources = array();
27 private $locale;
28 private $fallbackCatalogue;
29 private $parent;
31 /**
32 * Constructor.
34 * @param string $locale The locale
35 * @param array $messages An array of messages classified by domain
37 public function __construct($locale, array $messages = array())
39 $this->locale = $locale;
40 $this->messages = $messages;
43 /**
44 * {@inheritdoc}
46 public function getLocale()
48 return $this->locale;
51 /**
52 * {@inheritdoc}
54 public function getDomains()
56 return array_keys($this->messages);
59 /**
60 * {@inheritdoc}
62 public function all($domain = null)
64 if (null === $domain) {
65 return $this->messages;
68 return isset($this->messages[$domain]) ? $this->messages[$domain] : array();
71 /**
72 * {@inheritdoc}
74 public function set($id, $translation, $domain = 'messages')
76 $this->add(array($id => $translation), $domain);
79 /**
80 * {@inheritdoc}
82 public function has($id, $domain = 'messages')
84 if (isset($this->messages[$domain][$id])) {
85 return true;
88 if (null !== $this->fallbackCatalogue) {
89 return $this->fallbackCatalogue->has($id, $domain);
92 return false;
95 /**
96 * {@inheritdoc}
98 public function defines($id, $domain = 'messages')
100 return isset($this->messages[$domain][$id]);
104 * {@inheritdoc}
106 public function get($id, $domain = 'messages')
108 if (isset($this->messages[$domain][$id])) {
109 return $this->messages[$domain][$id];
112 if (null !== $this->fallbackCatalogue) {
113 return $this->fallbackCatalogue->get($id, $domain);
116 return $id;
120 * {@inheritdoc}
122 public function replace($messages, $domain = 'messages')
124 $this->messages[$domain] = array();
126 $this->add($messages, $domain);
130 * {@inheritdoc}
132 public function add($messages, $domain = 'messages')
134 if (!isset($this->messages[$domain])) {
135 $this->messages[$domain] = $messages;
136 } else {
137 $this->messages[$domain] = array_replace($this->messages[$domain], $messages);
142 * {@inheritdoc}
144 public function addCatalogue(MessageCatalogueInterface $catalogue)
146 if ($catalogue->getLocale() !== $this->locale) {
147 throw new LogicException(sprintf('Cannot add a catalogue for locale "%s" as the current locale for this catalogue is "%s"', $catalogue->getLocale(), $this->locale));
150 foreach ($catalogue->all() as $domain => $messages) {
151 $this->add($messages, $domain);
154 foreach ($catalogue->getResources() as $resource) {
155 $this->addResource($resource);
158 if ($catalogue instanceof MetadataAwareInterface) {
159 $metadata = $catalogue->getMetadata('', '');
160 $this->addMetadata($metadata);
165 * {@inheritdoc}
167 public function addFallbackCatalogue(MessageCatalogueInterface $catalogue)
169 // detect circular references
170 $c = $catalogue;
171 while ($c = $c->getFallbackCatalogue()) {
172 if ($c->getLocale() === $this->getLocale()) {
173 throw new LogicException(sprintf('Circular reference detected when adding a fallback catalogue for locale "%s".', $catalogue->getLocale()));
177 $c = $this;
178 do {
179 if ($c->getLocale() === $catalogue->getLocale()) {
180 throw new LogicException(sprintf('Circular reference detected when adding a fallback catalogue for locale "%s".', $catalogue->getLocale()));
183 foreach ($catalogue->getResources() as $resource) {
184 $c->addResource($resource);
186 } while ($c = $c->parent);
188 $catalogue->parent = $this;
189 $this->fallbackCatalogue = $catalogue;
191 foreach ($catalogue->getResources() as $resource) {
192 $this->addResource($resource);
197 * {@inheritdoc}
199 public function getFallbackCatalogue()
201 return $this->fallbackCatalogue;
205 * {@inheritdoc}
207 public function getResources()
209 return array_values($this->resources);
213 * {@inheritdoc}
215 public function addResource(ResourceInterface $resource)
217 $this->resources[$resource->__toString()] = $resource;
221 * {@inheritdoc}
223 public function getMetadata($key = '', $domain = 'messages')
225 if ('' == $domain) {
226 return $this->metadata;
229 if (isset($this->metadata[$domain])) {
230 if ('' == $key) {
231 return $this->metadata[$domain];
234 if (isset($this->metadata[$domain][$key])) {
235 return $this->metadata[$domain][$key];
241 * {@inheritdoc}
243 public function setMetadata($key, $value, $domain = 'messages')
245 $this->metadata[$domain][$key] = $value;
249 * {@inheritdoc}
251 public function deleteMetadata($key = '', $domain = 'messages')
253 if ('' == $domain) {
254 $this->metadata = array();
255 } elseif ('' == $key) {
256 unset($this->metadata[$domain]);
257 } else {
258 unset($this->metadata[$domain][$key]);
263 * Adds current values with the new values.
265 * @param array $values Values to add
267 private function addMetadata(array $values)
269 foreach ($values as $domain => $keys) {
270 foreach ($keys as $key => $value) {
271 $this->setMetadata($key, $value, $domain);