AbstractOperation.php 4.5 KB

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