AbstractOperation.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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\Exception\InvalidArgumentException;
  12. use Symfony\Component\Translation\Exception\LogicException;
  13. use Symfony\Component\Translation\MessageCatalogue;
  14. use Symfony\Component\Translation\MessageCatalogueInterface;
  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. protected $source;
  26. protected $target;
  27. protected $result;
  28. /**
  29. * @var array|null The domains affected by this operation
  30. */
  31. private $domains;
  32. /**
  33. * This array stores 'all', 'new' and 'obsolete' messages for all valid domains.
  34. *
  35. * The data structure of this array is as follows:
  36. *
  37. * [
  38. * 'domain 1' => [
  39. * 'all' => [...],
  40. * 'new' => [...],
  41. * 'obsolete' => [...]
  42. * ],
  43. * 'domain 2' => [
  44. * 'all' => [...],
  45. * 'new' => [...],
  46. * 'obsolete' => [...]
  47. * ],
  48. * ...
  49. * ]
  50. *
  51. * @var array The array that stores 'all', 'new' and 'obsolete' messages
  52. */
  53. protected $messages;
  54. /**
  55. * @throws LogicException
  56. */
  57. public function __construct(MessageCatalogueInterface $source, MessageCatalogueInterface $target)
  58. {
  59. if ($source->getLocale() !== $target->getLocale()) {
  60. throw new LogicException('Operated catalogues must belong to the same locale.');
  61. }
  62. $this->source = $source;
  63. $this->target = $target;
  64. $this->result = new MessageCatalogue($source->getLocale());
  65. $this->messages = [];
  66. }
  67. /**
  68. * {@inheritdoc}
  69. */
  70. public function getDomains()
  71. {
  72. if (null === $this->domains) {
  73. $this->domains = array_values(array_unique(array_merge($this->source->getDomains(), $this->target->getDomains())));
  74. }
  75. return $this->domains;
  76. }
  77. /**
  78. * {@inheritdoc}
  79. */
  80. public function getMessages(string $domain)
  81. {
  82. if (!\in_array($domain, $this->getDomains())) {
  83. throw new InvalidArgumentException(sprintf('Invalid domain: %s.', $domain));
  84. }
  85. if (!isset($this->messages[$domain]['all'])) {
  86. $this->processDomain($domain);
  87. }
  88. return $this->messages[$domain]['all'];
  89. }
  90. /**
  91. * {@inheritdoc}
  92. */
  93. public function getNewMessages(string $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]['new'])) {
  99. $this->processDomain($domain);
  100. }
  101. return $this->messages[$domain]['new'];
  102. }
  103. /**
  104. * {@inheritdoc}
  105. */
  106. public function getObsoleteMessages(string $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]['obsolete'])) {
  112. $this->processDomain($domain);
  113. }
  114. return $this->messages[$domain]['obsolete'];
  115. }
  116. /**
  117. * {@inheritdoc}
  118. */
  119. public function getResult()
  120. {
  121. foreach ($this->getDomains() as $domain) {
  122. if (!isset($this->messages[$domain])) {
  123. $this->processDomain($domain);
  124. }
  125. }
  126. return $this->result;
  127. }
  128. /**
  129. * Performs operation on source and target catalogues for the given domain and
  130. * stores the results.
  131. *
  132. * @param string $domain The domain which the operation will be performed for
  133. */
  134. abstract protected function processDomain(string $domain);
  135. }