Debian Codegraph @

lib/3rdParty/composer/symfony/translation/Catalogue/AbstractOperation.php

raw symbols SCIP
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <[email protected]>
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\Exception\InvalidArgumentException;
15 use Symfony\Component\Translation\Exception\LogicException;
16 use Symfony\Component\Translation\MessageCatalogue;
17 use Symfony\Component\Translation\MessageCatalogueInterface;
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 <[email protected]>
26 */
27 abstract class AbstractOperation implements OperationInterface
28 {
29 public const OBSOLETE_BATCH = 'obsolete';
30 public const NEW_BATCH = 'new';
31 public const ALL_BATCH = 'all';
32
33 protected MessageCatalogue $result;
34
35 /**
36 * This array stores 'all', 'new' and 'obsolete' messages for all valid domains.
37 *
38 * The data structure of this array is as follows:
39 *
40 * [
41 * 'domain 1' => [
42 * 'all' => [...],
43 * 'new' => [...],
44 * 'obsolete' => [...]
45 * ],
46 * 'domain 2' => [
47 * 'all' => [...],
48 * 'new' => [...],
49 * 'obsolete' => [...]
50 * ],
51 * ...
52 * ]
53 *
54 * @var array The array that stores 'all', 'new' and 'obsolete' messages
55 */
56 protected array $messages;
57
58 private array $domains;
59
60 /**
61 * @throws LogicException
62 */
63 public function __construct(
64 protected MessageCatalogueInterface $source,
65 protected 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->result = new MessageCatalogue($source->getLocale());
72 $this->messages = [];
73 }
74
75 public function getDomains(): array
76 {
77 if (!isset($this->domains)) {
78 $domains = [];
79 foreach ([$this->source, $this->target] as $catalogue) {
80 foreach ($catalogue->getDomains() as $domain) {
81 $domains[$domain] = $domain;
82
83 if ($catalogue->all($domainIcu = $domain.MessageCatalogueInterface::INTL_DOMAIN_SUFFIX)) {
84 $domains[$domainIcu] = $domainIcu;
85 }
86 }
87 }
88
89 $this->domains = array_values($domains);
90 }
91
92 return $this->domains;
93 }
94
95 public function getMessages(string $domain): array
96 {
97 if (!\in_array($domain, $this->getDomains(), true)) {
98 throw new InvalidArgumentException(\sprintf('Invalid domain: "%s".', $domain));
99 }
100
101 if (!isset($this->messages[$domain][self::ALL_BATCH])) {
102 $this->processDomain($domain);
103 }
104
105 return $this->messages[$domain][self::ALL_BATCH];
106 }
107
108 public function getNewMessages(string $domain): array
109 {
110 if (!\in_array($domain, $this->getDomains(), true)) {
111 throw new InvalidArgumentException(\sprintf('Invalid domain: "%s".', $domain));
112 }
113
114 if (!isset($this->messages[$domain][self::NEW_BATCH])) {
115 $this->processDomain($domain);
116 }
117
118 return $this->messages[$domain][self::NEW_BATCH];
119 }
120
121 public function getObsoleteMessages(string $domain): array
122 {
123 if (!\in_array($domain, $this->getDomains(), true)) {
124 throw new InvalidArgumentException(\sprintf('Invalid domain: "%s".', $domain));
125 }
126
127 if (!isset($this->messages[$domain][self::OBSOLETE_BATCH])) {
128 $this->processDomain($domain);
129 }
130
131 return $this->messages[$domain][self::OBSOLETE_BATCH];
132 }
133
134 public function getResult(): MessageCatalogueInterface
135 {
136 foreach ($this->getDomains() as $domain) {
137 if (!isset($this->messages[$domain])) {
138 $this->processDomain($domain);
139 }
140 }
141
142 return $this->result;
143 }
144
145 /**
146 * @param self::*_BATCH $batch
147 */
148 public function moveMessagesToIntlDomainsIfPossible(string $batch = self::ALL_BATCH): void
149 {
150 // If MessageFormatter class does not exists, intl domains are not supported.
151 if (!class_exists(\MessageFormatter::class)) {
152 return;
153 }
154
155 foreach ($this->getDomains() as $domain) {
156 $intlDomain = $domain.MessageCatalogueInterface::INTL_DOMAIN_SUFFIX;
157 $messages = match ($batch) {
158 self::OBSOLETE_BATCH => $this->getObsoleteMessages($domain),
159 self::NEW_BATCH => $this->getNewMessages($domain),
160 self::ALL_BATCH => $this->getMessages($domain),
161 default => throw new \InvalidArgumentException(\sprintf('$batch argument must be one of ["%s", "%s", "%s"].', self::ALL_BATCH, self::NEW_BATCH, self::OBSOLETE_BATCH)),
162 };
163
164 if (!$messages || (!$this->source->all($intlDomain) && $this->source->all($domain))) {
165 continue;
166 }
167
168 $result = $this->getResult();
169 $allIntlMessages = $result->all($intlDomain);
170 $currentMessages = array_diff_key($messages, $result->all($domain));
171 $result->replace($currentMessages, $domain);
172 $result->replace($allIntlMessages + $messages, $intlDomain);
173 }
174 }
175
176 /**
177 * Performs operation on source and target catalogues for the given domain and
178 * stores the results.
179 *
180 * @param string $domain The domain which the operation will be performed for
181 */
182 abstract protected function processDomain(string $domain): void;
183 }