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;
18 * Base catalogues binary operation class.
20 * A catalogue binary operation performs operation on
21 * source (the left argument) and target (the right argument) catalogues.
23 * @author Jean-François Simon <contact@jfsimon.fr>
25 abstract class AbstractOperation implements OperationInterface
28 * @var MessageCatalogueInterface The source catalogue
33 * @var MessageCatalogueInterface The target catalogue
38 * @var MessageCatalogue The result catalogue
43 * @var null|array The domains affected by this operation
48 * This array stores 'all', 'new' and 'obsolete' messages for all valid domains.
50 * The data structure of this array is as follows:
53 * 'domain 1' => array(
54 * 'all' => array(...),
55 * 'new' => array(...),
56 * 'obsolete' => array(...)
58 * 'domain 2' => array(
59 * 'all' => array(...),
60 * 'new' => array(...),
61 * 'obsolete' => array(...)
67 * @var array The array that stores 'all', 'new' and 'obsolete' messages
72 * @param MessageCatalogueInterface $source The source catalogue
73 * @param MessageCatalogueInterface $target The target catalogue
75 * @throws \LogicException
77 public function __construct(MessageCatalogueInterface $source, MessageCatalogueInterface $target)
79 if ($source->getLocale() !== $target->getLocale()) {
80 throw new \LogicException('Operated catalogues must belong to the same locale.');
83 $this->source = $source;
84 $this->target = $target;
85 $this->result = new MessageCatalogue($source->getLocale());
86 $this->messages = array();
92 public function getDomains()
94 if (null === $this->domains) {
95 $this->domains = array_values(array_unique(array_merge($this->source->getDomains(), $this->target->getDomains())));
98 return $this->domains;
104 public function getMessages($domain)
106 if (!in_array($domain, $this->getDomains())) {
107 throw new \InvalidArgumentException(sprintf('Invalid domain: %s.', $domain));
110 if (!isset($this->messages[$domain]['all'])) {
111 $this->processDomain($domain);
114 return $this->messages[$domain]['all'];
120 public function getNewMessages($domain)
122 if (!in_array($domain, $this->getDomains())) {
123 throw new \InvalidArgumentException(sprintf('Invalid domain: %s.', $domain));
126 if (!isset($this->messages[$domain]['new'])) {
127 $this->processDomain($domain);
130 return $this->messages[$domain]['new'];
136 public function getObsoleteMessages($domain)
138 if (!in_array($domain, $this->getDomains())) {
139 throw new \InvalidArgumentException(sprintf('Invalid domain: %s.', $domain));
142 if (!isset($this->messages[$domain]['obsolete'])) {
143 $this->processDomain($domain);
146 return $this->messages[$domain]['obsolete'];
152 public function getResult()
154 foreach ($this->getDomains() as $domain) {
155 if (!isset($this->messages[$domain])) {
156 $this->processDomain($domain);
160 return $this->result;
164 * Performs operation on source and target catalogues for the given domain and
165 * stores the results.
167 * @param string $domain The domain which the operation will be performed for
169 abstract protected function processDomain($domain);