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 * @author Jean-François Simon <contact@jfsimon.fr>
22 abstract class AbstractOperation
implements OperationInterface
25 * @var MessageCatalogueInterface
30 * @var MessageCatalogueInterface
35 * @var MessageCatalogue
50 * @param MessageCatalogueInterface $source
51 * @param MessageCatalogueInterface $target
53 * @throws \LogicException
55 public function __construct(MessageCatalogueInterface
$source, MessageCatalogueInterface
$target)
57 if ($source->getLocale() !== $target->getLocale()) {
58 throw new \
LogicException('Operated catalogues must belong to the same locale.');
61 $this->source
= $source;
62 $this->target
= $target;
63 $this->result
= new MessageCatalogue($source->getLocale());
64 $this->domains
= null;
65 $this->messages
= array();
71 public function getDomains()
73 if (null === $this->domains
) {
74 $this->domains
= array_values(array_unique(array_merge($this->source
->getDomains(), $this->target
->getDomains())));
77 return $this->domains
;
83 public function getMessages($domain)
85 if (!in_array($domain, $this->getDomains())) {
86 throw new \
InvalidArgumentException(sprintf('Invalid domain: %s.', $domain));
89 if (!isset($this->messages
[$domain]['all'])) {
90 $this->processDomain($domain);
93 return $this->messages
[$domain]['all'];
99 public function getNewMessages($domain)
101 if (!in_array($domain, $this->getDomains())) {
102 throw new \
InvalidArgumentException(sprintf('Invalid domain: %s.', $domain));
105 if (!isset($this->messages
[$domain]['new'])) {
106 $this->processDomain($domain);
109 return $this->messages
[$domain]['new'];
115 public function getObsoleteMessages($domain)
117 if (!in_array($domain, $this->getDomains())) {
118 throw new \
InvalidArgumentException(sprintf('Invalid domain: %s.', $domain));
121 if (!isset($this->messages
[$domain]['obsolete'])) {
122 $this->processDomain($domain);
125 return $this->messages
[$domain]['obsolete'];
131 public function getResult()
133 foreach ($this->getDomains() as $domain) {
134 if (!isset($this->messages
[$domain])) {
135 $this->processDomain($domain);
139 return $this->result
;
143 * @param string $domain
145 abstract protected function processDomain($domain);