]>
Commit | Line | Data |
---|---|---|
4f5b44bd NL |
1 | <?php |
2 | ||
3 | /* | |
4 | * This file is part of the Symfony package. | |
5 | * | |
6 | * (c) Fabien Potencier <fabien@symfony.com> | |
7 | * | |
8 | * For the full copyright and license information, please view the LICENSE | |
9 | * file that was distributed with this source code. | |
10 | */ | |
11 | ||
12 | namespace Symfony\Component\Translation\Catalogue; | |
13 | ||
14 | use Symfony\Component\Translation\MessageCatalogue; | |
15 | use Symfony\Component\Translation\MessageCatalogueInterface; | |
16 | ||
17 | /** | |
18 | * Base catalogues binary operation class. | |
19 | * | |
20 | * @author Jean-François Simon <contact@jfsimon.fr> | |
21 | */ | |
22 | abstract class AbstractOperation implements OperationInterface | |
23 | { | |
24 | /** | |
25 | * @var MessageCatalogueInterface | |
26 | */ | |
27 | protected $source; | |
28 | ||
29 | /** | |
30 | * @var MessageCatalogueInterface | |
31 | */ | |
32 | protected $target; | |
33 | ||
34 | /** | |
35 | * @var MessageCatalogue | |
36 | */ | |
37 | protected $result; | |
38 | ||
39 | /** | |
40 | * @var null|array | |
41 | */ | |
42 | private $domains; | |
43 | ||
44 | /** | |
45 | * @var array | |
46 | */ | |
47 | protected $messages; | |
48 | ||
49 | /** | |
50 | * @param MessageCatalogueInterface $source | |
51 | * @param MessageCatalogueInterface $target | |
52 | * | |
53 | * @throws \LogicException | |
54 | */ | |
55 | public function __construct(MessageCatalogueInterface $source, MessageCatalogueInterface $target) | |
56 | { | |
57 | if ($source->getLocale() !== $target->getLocale()) { | |
58 | throw new \LogicException('Operated catalogues must belong to the same locale.'); | |
59 | } | |
60 | ||
61 | $this->source = $source; | |
62 | $this->target = $target; | |
63 | $this->result = new MessageCatalogue($source->getLocale()); | |
64 | $this->domains = null; | |
65 | $this->messages = array(); | |
66 | } | |
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 | ||
77 | return $this->domains; | |
78 | } | |
79 | ||
80 | /** | |
81 | * {@inheritdoc} | |
82 | */ | |
83 | public function getMessages($domain) | |
84 | { | |
85 | if (!in_array($domain, $this->getDomains())) { | |
86 | throw new \InvalidArgumentException(sprintf('Invalid domain: %s.', $domain)); | |
87 | } | |
88 | ||
89 | if (!isset($this->messages[$domain]['all'])) { | |
90 | $this->processDomain($domain); | |
91 | } | |
92 | ||
93 | return $this->messages[$domain]['all']; | |
94 | } | |
95 | ||
96 | /** | |
97 | * {@inheritdoc} | |
98 | */ | |
99 | public function getNewMessages($domain) | |
100 | { | |
101 | if (!in_array($domain, $this->getDomains())) { | |
102 | throw new \InvalidArgumentException(sprintf('Invalid domain: %s.', $domain)); | |
103 | } | |
104 | ||
105 | if (!isset($this->messages[$domain]['new'])) { | |
106 | $this->processDomain($domain); | |
107 | } | |
108 | ||
109 | return $this->messages[$domain]['new']; | |
110 | } | |
111 | ||
112 | /** | |
113 | * {@inheritdoc} | |
114 | */ | |
115 | public function getObsoleteMessages($domain) | |
116 | { | |
117 | if (!in_array($domain, $this->getDomains())) { | |
118 | throw new \InvalidArgumentException(sprintf('Invalid domain: %s.', $domain)); | |
119 | } | |
120 | ||
121 | if (!isset($this->messages[$domain]['obsolete'])) { | |
122 | $this->processDomain($domain); | |
123 | } | |
124 | ||
125 | return $this->messages[$domain]['obsolete']; | |
126 | } | |
127 | ||
128 | /** | |
129 | * {@inheritdoc} | |
130 | */ | |
131 | public function getResult() | |
132 | { | |
133 | foreach ($this->getDomains() as $domain) { | |
134 | if (!isset($this->messages[$domain])) { | |
135 | $this->processDomain($domain); | |
136 | } | |
137 | } | |
138 | ||
139 | return $this->result; | |
140 | } | |
141 | ||
142 | /** | |
143 | * @param string $domain | |
144 | */ | |
145 | abstract protected function processDomain($domain); | |
146 | } |