菜谱项目

AbstractOperation.php 4.1KB

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