AbstractOperation.php 4.1 KB

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