4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Translation\Catalogue;
14 use Symfony\Component\Translation\MessageCatalogue;
15 use Symfony\Component\Translation\MessageCatalogueInterface;
16 use Symfony\Component\Translation\Exception\InvalidArgumentException;
17 use Symfony\Component\Translation\Exception\LogicException;
20 * Base catalogues binary operation class.
22 * A catalogue binary operation performs operation on
23 * source (the left argument) and target (the right argument) catalogues.
25 * @author Jean-François Simon <contact@jfsimon.fr>
27 abstract class AbstractOperation implements OperationInterface
30 * @var MessageCatalogueInterface The source catalogue
35 * @var MessageCatalogueInterface The target catalogue
40 * @var MessageCatalogue The result catalogue
45 * @var null|array The domains affected by this operation
50 * This array stores 'all', 'new' and 'obsolete' messages for all valid domains.
52 * The data structure of this array is as follows:
55 * 'domain 1' => array(
56 * 'all' => array(...),
57 * 'new' => array(...),
58 * 'obsolete' => array(...)
60 * 'domain 2' => array(
61 * 'all' => array(...),
62 * 'new' => array(...),
63 * 'obsolete' => array(...)
69 * @var array The array that stores 'all', 'new' and 'obsolete' messages
74 * @param MessageCatalogueInterface $source The source catalogue
75 * @param MessageCatalogueInterface $target The target catalogue
77 * @throws LogicException
79 public function __construct(MessageCatalogueInterface $source, MessageCatalogueInterface $target)
81 if ($source->getLocale() !== $target->getLocale()) {
82 throw new LogicException('Operated catalogues must belong to the same locale.');
85 $this->source = $source;
86 $this->target = $target;
87 $this->result = new MessageCatalogue($source->getLocale());
88 $this->messages = array();
94 public function getDomains()
96 if (null === $this->domains) {
97 $this->domains = array_values(array_unique(array_merge($this->source->getDomains(), $this->target->getDomains())));
100 return $this->domains;
106 public function getMessages($domain)
108 if (!in_array($domain, $this->getDomains())) {
109 throw new InvalidArgumentException(sprintf('Invalid domain: %s.', $domain));
112 if (!isset($this->messages[$domain]['all'])) {
113 $this->processDomain($domain);
116 return $this->messages[$domain]['all'];
122 public function getNewMessages($domain)
124 if (!in_array($domain, $this->getDomains())) {
125 throw new InvalidArgumentException(sprintf('Invalid domain: %s.', $domain));
128 if (!isset($this->messages[$domain]['new'])) {
129 $this->processDomain($domain);
132 return $this->messages[$domain]['new'];
138 public function getObsoleteMessages($domain)
140 if (!in_array($domain, $this->getDomains())) {
141 throw new InvalidArgumentException(sprintf('Invalid domain: %s.', $domain));
144 if (!isset($this->messages[$domain]['obsolete'])) {
145 $this->processDomain($domain);
148 return $this->messages[$domain]['obsolete'];
154 public function getResult()
156 foreach ($this->getDomains() as $domain) {
157 if (!isset($this->messages[$domain])) {
158 $this->processDomain($domain);
162 return $this->result;
166 * Performs operation on source and target catalogues for the given domain and
167 * stores the results.
169 * @param string $domain The domain which the operation will be performed for
171 abstract protected function processDomain($domain);