No Description

AbstractOperation.php 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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->domains = null;
  79. $this->messages = array();
  80. }
  81. /**
  82. * {@inheritdoc}
  83. */
  84. public function getDomains()
  85. {
  86. if (null === $this->domains) {
  87. $this->domains = array_values(array_unique(array_merge($this->source->getDomains(), $this->target->getDomains())));
  88. }
  89. return $this->domains;
  90. }
  91. /**
  92. * {@inheritdoc}
  93. */
  94. public function getMessages($domain)
  95. {
  96. if (!in_array($domain, $this->getDomains())) {
  97. throw new InvalidArgumentException(sprintf('Invalid domain: %s.', $domain));
  98. }
  99. if (!isset($this->messages[$domain]['all'])) {
  100. $this->processDomain($domain);
  101. }
  102. return $this->messages[$domain]['all'];
  103. }
  104. /**
  105. * {@inheritdoc}
  106. */
  107. public function getNewMessages($domain)
  108. {
  109. if (!in_array($domain, $this->getDomains())) {
  110. throw new InvalidArgumentException(sprintf('Invalid domain: %s.', $domain));
  111. }
  112. if (!isset($this->messages[$domain]['new'])) {
  113. $this->processDomain($domain);
  114. }
  115. return $this->messages[$domain]['new'];
  116. }
  117. /**
  118. * {@inheritdoc}
  119. */
  120. public function getObsoleteMessages($domain)
  121. {
  122. if (!in_array($domain, $this->getDomains())) {
  123. throw new InvalidArgumentException(sprintf('Invalid domain: %s.', $domain));
  124. }
  125. if (!isset($this->messages[$domain]['obsolete'])) {
  126. $this->processDomain($domain);
  127. }
  128. return $this->messages[$domain]['obsolete'];
  129. }
  130. /**
  131. * {@inheritdoc}
  132. */
  133. public function getResult()
  134. {
  135. foreach ($this->getDomains() as $domain) {
  136. if (!isset($this->messages[$domain])) {
  137. $this->processDomain($domain);
  138. }
  139. }
  140. return $this->result;
  141. }
  142. /**
  143. * Performs operation on source and target catalogues for the given domain and
  144. * stores the results.
  145. *
  146. * @param string $domain The domain which the operation will be performed for
  147. */
  148. abstract protected function processDomain($domain);
  149. }