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