/src/Symfony/Component/Translation/MessageCatalogue.php

https://github.com/Faianca/symfony · PHP · 224 lines · 103 code · 28 blank · 93 comment · 8 complexity · a7f4070a9898ab8dfb8bb89a48817426 MD5 · raw file

  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Translation;
  11. use Symfony\Component\Config\Resource\ResourceInterface;
  12. /**
  13. * MessageCatalogue.
  14. *
  15. * @author Fabien Potencier <fabien@symfony.com>
  16. *
  17. * @api
  18. */
  19. class MessageCatalogue implements MessageCatalogueInterface
  20. {
  21. private $messages = array();
  22. private $locale;
  23. private $resources;
  24. private $fallbackCatalogue;
  25. /**
  26. * Constructor.
  27. *
  28. * @param string $locale The locale
  29. * @param array $messages An array of messages classified by domain
  30. *
  31. * @api
  32. */
  33. public function __construct($locale, array $messages = array())
  34. {
  35. $this->locale = $locale;
  36. $this->messages = $messages;
  37. $this->resources = array();
  38. }
  39. /**
  40. * {@inheritdoc}
  41. *
  42. * @api
  43. */
  44. public function getLocale()
  45. {
  46. return $this->locale;
  47. }
  48. /**
  49. * {@inheritdoc}
  50. *
  51. * @api
  52. */
  53. public function getDomains()
  54. {
  55. return array_keys($this->messages);
  56. }
  57. /**
  58. * {@inheritdoc}
  59. *
  60. * @api
  61. */
  62. public function all($domain = null)
  63. {
  64. if (null === $domain) {
  65. return $this->messages;
  66. }
  67. return isset($this->messages[$domain]) ? $this->messages[$domain] : array();
  68. }
  69. /**
  70. * {@inheritdoc}
  71. *
  72. * @api
  73. */
  74. public function set($id, $translation, $domain = 'messages')
  75. {
  76. $this->add(array($id => $translation), $domain);
  77. }
  78. /**
  79. * {@inheritdoc}
  80. *
  81. * @api
  82. */
  83. public function has($id, $domain = 'messages')
  84. {
  85. if (isset($this->messages[$domain][$id])) {
  86. return true;
  87. }
  88. if (null !== $this->fallbackCatalogue) {
  89. return $this->fallbackCatalogue->has($id, $domain);
  90. }
  91. return false;
  92. }
  93. /**
  94. * {@inheritdoc}
  95. */
  96. public function defines($id, $domain = 'messages')
  97. {
  98. return isset($this->messages[$domain][$id]);
  99. }
  100. /**
  101. * {@inheritdoc}
  102. *
  103. * @api
  104. */
  105. public function get($id, $domain = 'messages')
  106. {
  107. if (isset($this->messages[$domain][$id])) {
  108. return $this->messages[$domain][$id];
  109. }
  110. if (null !== $this->fallbackCatalogue) {
  111. return $this->fallbackCatalogue->get($id, $domain);
  112. }
  113. return $id;
  114. }
  115. /**
  116. * {@inheritdoc}
  117. *
  118. * @api
  119. */
  120. public function replace($messages, $domain = 'messages')
  121. {
  122. $this->messages[$domain] = array();
  123. $this->add($messages, $domain);
  124. }
  125. /**
  126. * {@inheritdoc}
  127. *
  128. * @api
  129. */
  130. public function add($messages, $domain = 'messages')
  131. {
  132. if (!isset($this->messages[$domain])) {
  133. $this->messages[$domain] = $messages;
  134. } else {
  135. $this->messages[$domain] = array_replace($this->messages[$domain], $messages);
  136. }
  137. }
  138. /**
  139. * {@inheritdoc}
  140. *
  141. * @api
  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. foreach ($catalogue->all() as $domain => $messages) {
  149. $this->add($messages, $domain);
  150. }
  151. foreach ($catalogue->getResources() as $resource) {
  152. $this->addResource($resource);
  153. }
  154. }
  155. /**
  156. * {@inheritdoc}
  157. *
  158. * @api
  159. */
  160. public function addFallbackCatalogue(MessageCatalogueInterface $catalogue)
  161. {
  162. $this->fallbackCatalogue = $catalogue;
  163. foreach ($catalogue->getResources() as $resource) {
  164. $this->addResource($resource);
  165. }
  166. }
  167. /**
  168. * Gets the fallback catalogue.
  169. *
  170. * @return MessageCatalogueInterface A MessageCatalogueInterface instance
  171. *
  172. * @api
  173. */
  174. public function getFallbackCatalogue()
  175. {
  176. return $this->fallbackCatalogue;
  177. }
  178. /**
  179. * {@inheritdoc}
  180. *
  181. * @api
  182. */
  183. public function getResources()
  184. {
  185. return array_values(array_unique($this->resources));
  186. }
  187. /**
  188. * {@inheritdoc}
  189. *
  190. * @api
  191. */
  192. public function addResource(ResourceInterface $resource)
  193. {
  194. $this->resources[] = $resource;
  195. }
  196. }