bb501c14a465c3965b789264573058ab47ee09f2
[yaffs-website] / vendor / symfony / translation / Catalogue / AbstractOperation.php
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 use Symfony\Component\Translation\Exception\InvalidArgumentException;
17 use Symfony\Component\Translation\Exception\LogicException;
18
19 /**
20  * Base catalogues binary operation class.
21  *
22  * A catalogue binary operation performs operation on
23  * source (the left argument) and target (the right argument) catalogues.
24  *
25  * @author Jean-François Simon <contact@jfsimon.fr>
26  */
27 abstract class AbstractOperation implements OperationInterface
28 {
29     /**
30      * @var MessageCatalogueInterface The source catalogue
31      */
32     protected $source;
33
34     /**
35      * @var MessageCatalogueInterface The target catalogue
36      */
37     protected $target;
38
39     /**
40      * @var MessageCatalogue The result catalogue
41      */
42     protected $result;
43
44     /**
45      * @var null|array The domains affected by this operation
46      */
47     private $domains;
48
49     /**
50      * This array stores 'all', 'new' and 'obsolete' messages for all valid domains.
51      *
52      * The data structure of this array is as follows:
53      * ```php
54      * array(
55      *     'domain 1' => array(
56      *         'all' => array(...),
57      *         'new' => array(...),
58      *         'obsolete' => array(...)
59      *     ),
60      *     'domain 2' => array(
61      *         'all' => array(...),
62      *         'new' => array(...),
63      *         'obsolete' => array(...)
64      *     ),
65      *     ...
66      * )
67      * ```
68      *
69      * @var array The array that stores 'all', 'new' and 'obsolete' messages
70      */
71     protected $messages;
72
73     /**
74      * @param MessageCatalogueInterface $source The source catalogue
75      * @param MessageCatalogueInterface $target The target catalogue
76      *
77      * @throws LogicException
78      */
79     public function __construct(MessageCatalogueInterface $source, MessageCatalogueInterface $target)
80     {
81         if ($source->getLocale() !== $target->getLocale()) {
82             throw new LogicException('Operated catalogues must belong to the same locale.');
83         }
84
85         $this->source = $source;
86         $this->target = $target;
87         $this->result = new MessageCatalogue($source->getLocale());
88         $this->messages = array();
89     }
90
91     /**
92      * {@inheritdoc}
93      */
94     public function getDomains()
95     {
96         if (null === $this->domains) {
97             $this->domains = array_values(array_unique(array_merge($this->source->getDomains(), $this->target->getDomains())));
98         }
99
100         return $this->domains;
101     }
102
103     /**
104      * {@inheritdoc}
105      */
106     public function getMessages($domain)
107     {
108         if (!in_array($domain, $this->getDomains())) {
109             throw new InvalidArgumentException(sprintf('Invalid domain: %s.', $domain));
110         }
111
112         if (!isset($this->messages[$domain]['all'])) {
113             $this->processDomain($domain);
114         }
115
116         return $this->messages[$domain]['all'];
117     }
118
119     /**
120      * {@inheritdoc}
121      */
122     public function getNewMessages($domain)
123     {
124         if (!in_array($domain, $this->getDomains())) {
125             throw new InvalidArgumentException(sprintf('Invalid domain: %s.', $domain));
126         }
127
128         if (!isset($this->messages[$domain]['new'])) {
129             $this->processDomain($domain);
130         }
131
132         return $this->messages[$domain]['new'];
133     }
134
135     /**
136      * {@inheritdoc}
137      */
138     public function getObsoleteMessages($domain)
139     {
140         if (!in_array($domain, $this->getDomains())) {
141             throw new InvalidArgumentException(sprintf('Invalid domain: %s.', $domain));
142         }
143
144         if (!isset($this->messages[$domain]['obsolete'])) {
145             $this->processDomain($domain);
146         }
147
148         return $this->messages[$domain]['obsolete'];
149     }
150
151     /**
152      * {@inheritdoc}
153      */
154     public function getResult()
155     {
156         foreach ($this->getDomains() as $domain) {
157             if (!isset($this->messages[$domain])) {
158                 $this->processDomain($domain);
159             }
160         }
161
162         return $this->result;
163     }
164
165     /**
166      * Performs operation on source and target catalogues for the given domain and
167      * stores the results.
168      *
169      * @param string $domain The domain which the operation will be performed for
170      */
171     abstract protected function processDomain($domain);
172 }