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