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
34 * @var null|array The domains affected by this operation
39 * This array stores 'all', 'new' and 'obsolete' messages for all valid domains.
41 * The data structure of this array is as follows:
44 * 'domain 1' => array(
45 * 'all' => array(...),
46 * 'new' => array(...),
47 * 'obsolete' => array(...)
49 * 'domain 2' => array(
50 * 'all' => array(...),
51 * 'new' => array(...),
52 * 'obsolete' => array(...)
58 * @var array The array that stores 'all', 'new' and 'obsolete' messages
63 * @throws LogicException
65 public function __construct(MessageCatalogueInterface $source, MessageCatalogueInterface $target)
67 if ($source->getLocale() !== $target->getLocale()) {
68 throw new LogicException('Operated catalogues must belong to the same locale.');
71 $this->source = $source;
72 $this->target = $target;
73 $this->result = new MessageCatalogue($source->getLocale());
74 $this->messages = array();
80 public function getDomains()
82 if (null === $this->domains) {
83 $this->domains = array_values(array_unique(array_merge($this->source->getDomains(), $this->target->getDomains())));
86 return $this->domains;
92 public function getMessages($domain)
94 if (!in_array($domain, $this->getDomains())) {
95 throw new InvalidArgumentException(sprintf('Invalid domain: %s.', $domain));
98 if (!isset($this->messages[$domain]['all'])) {
99 $this->processDomain($domain);
102 return $this->messages[$domain]['all'];
108 public function getNewMessages($domain)
110 if (!in_array($domain, $this->getDomains())) {
111 throw new InvalidArgumentException(sprintf('Invalid domain: %s.', $domain));
114 if (!isset($this->messages[$domain]['new'])) {
115 $this->processDomain($domain);
118 return $this->messages[$domain]['new'];
124 public function getObsoleteMessages($domain)
126 if (!in_array($domain, $this->getDomains())) {
127 throw new InvalidArgumentException(sprintf('Invalid domain: %s.', $domain));
130 if (!isset($this->messages[$domain]['obsolete'])) {
131 $this->processDomain($domain);
134 return $this->messages[$domain]['obsolete'];
140 public function getResult()
142 foreach ($this->getDomains() as $domain) {
143 if (!isset($this->messages[$domain])) {
144 $this->processDomain($domain);
148 return $this->result;
152 * Performs operation on source and target catalogues for the given domain and
153 * stores the results.
155 * @param string $domain The domain which the operation will be performed for
157 abstract protected function processDomain($domain);