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