Просмотр файла vendor/symfony/translation/Catalogue/AbstractOperation.php

Размер файла: 4.52Kb
  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\Catalogue;
  13.  
  14. use Symfony\Component\Translation\MessageCatalogue;
  15. use Symfony\Component\Translation\MessageCatalogueInterface;
  16. use Symfony\Component\Translation\Exception\InvalidArgumentException;
  17. use Symfony\Component\Translation\Exception\LogicException;
  18.  
  19. /**
  20. * Base catalogues binary operation class.
  21. *
  22. * A catalogue binary operation performs operation on
  23. * source (the left argument) and target (the right argument) catalogues.
  24. *
  25. * @author Jean-François Simon <contact@jfsimon.fr>
  26. */
  27. abstract class AbstractOperation implements OperationInterface
  28. {
  29. /**
  30. * @var MessageCatalogueInterface The source catalogue
  31. */
  32. protected $source;
  33.  
  34. /**
  35. * @var MessageCatalogueInterface The target catalogue
  36. */
  37. protected $target;
  38.  
  39. /**
  40. * @var MessageCatalogue The result catalogue
  41. */
  42. protected $result;
  43.  
  44. /**
  45. * @var null|array The domains affected by this operation
  46. */
  47. private $domains;
  48.  
  49. /**
  50. * This array stores 'all', 'new' and 'obsolete' messages for all valid domains.
  51. *
  52. * The data structure of this array is as follows:
  53. * ```php
  54. * array(
  55. * 'domain 1' => array(
  56. * 'all' => array(...),
  57. * 'new' => array(...),
  58. * 'obsolete' => array(...)
  59. * ),
  60. * 'domain 2' => array(
  61. * 'all' => array(...),
  62. * 'new' => array(...),
  63. * 'obsolete' => array(...)
  64. * ),
  65. * ...
  66. * )
  67. * ```
  68. *
  69. * @var array The array that stores 'all', 'new' and 'obsolete' messages
  70. */
  71. protected $messages;
  72.  
  73. /**
  74. * @param MessageCatalogueInterface $source The source catalogue
  75. * @param MessageCatalogueInterface $target The target catalogue
  76. *
  77. * @throws LogicException
  78. */
  79. public function __construct(MessageCatalogueInterface $source, MessageCatalogueInterface $target)
  80. {
  81. if ($source->getLocale() !== $target->getLocale()) {
  82. throw new LogicException('Operated catalogues must belong to the same locale.');
  83. }
  84.  
  85. $this->source = $source;
  86. $this->target = $target;
  87. $this->result = new MessageCatalogue($source->getLocale());
  88. $this->domains = null;
  89. $this->messages = array();
  90. }
  91.  
  92. /**
  93. * {@inheritdoc}
  94. */
  95. public function getDomains()
  96. {
  97. if (null === $this->domains) {
  98. $this->domains = array_values(array_unique(array_merge($this->source->getDomains(), $this->target->getDomains())));
  99. }
  100.  
  101. return $this->domains;
  102. }
  103.  
  104. /**
  105. * {@inheritdoc}
  106. */
  107. public function getMessages($domain)
  108. {
  109. if (!in_array($domain, $this->getDomains())) {
  110. throw new InvalidArgumentException(sprintf('Invalid domain: %s.', $domain));
  111. }
  112.  
  113. if (!isset($this->messages[$domain]['all'])) {
  114. $this->processDomain($domain);
  115. }
  116.  
  117. return $this->messages[$domain]['all'];
  118. }
  119.  
  120. /**
  121. * {@inheritdoc}
  122. */
  123. public function getNewMessages($domain)
  124. {
  125. if (!in_array($domain, $this->getDomains())) {
  126. throw new InvalidArgumentException(sprintf('Invalid domain: %s.', $domain));
  127. }
  128.  
  129. if (!isset($this->messages[$domain]['new'])) {
  130. $this->processDomain($domain);
  131. }
  132.  
  133. return $this->messages[$domain]['new'];
  134. }
  135.  
  136. /**
  137. * {@inheritdoc}
  138. */
  139. public function getObsoleteMessages($domain)
  140. {
  141. if (!in_array($domain, $this->getDomains())) {
  142. throw new InvalidArgumentException(sprintf('Invalid domain: %s.', $domain));
  143. }
  144.  
  145. if (!isset($this->messages[$domain]['obsolete'])) {
  146. $this->processDomain($domain);
  147. }
  148.  
  149. return $this->messages[$domain]['obsolete'];
  150. }
  151.  
  152. /**
  153. * {@inheritdoc}
  154. */
  155. public function getResult()
  156. {
  157. foreach ($this->getDomains() as $domain) {
  158. if (!isset($this->messages[$domain])) {
  159. $this->processDomain($domain);
  160. }
  161. }
  162.  
  163. return $this->result;
  164. }
  165.  
  166. /**
  167. * Performs operation on source and target catalogues for the given domain and
  168. * stores the results.
  169. *
  170. * @param string $domain The domain which the operation will be performed for
  171. */
  172. abstract protected function processDomain($domain);
  173. }