PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 6.3.0
Defender Security – Malware Scanner, Login Security & Firewall v6.3.0
6.3.0 6.2.3 6.2.4 6.2.0 6.2.1 6.2.2 6.1.0 5.3.1 5.4.0 5.4.1 5.5.0 5.5.1 5.6.0 5.6.1 5.6.2 5.7.0 5.7.1 5.7.2 5.8.0 5.8.1 5.9.0 6.0.0 6.0.1 3.0.1 3.1.0 All 141 releases
defender-security / vendor / gettext / gettext / src / Translations.php

Translations.php in Defender Security – Malware Scanner, Login Security & Firewall 6.3.0, at vendor/gettext/gettext/src/Translations.php

212 lines 5.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 declare(strict_types = 1);
3
4 namespace Gettext;
5
6 use ArrayIterator;
7 use Countable;
8 use Gettext\Languages\Language;
9 use InvalidArgumentException;
10 use IteratorAggregate;
11 use ReturnTypeWillChange;
12
13 /**
14 * Class to manage a collection of translations under the same domain.
15 */
16 class Translations implements Countable, IteratorAggregate
17 {
18 protected $description;
19 protected $translations = [];
20 protected $headers;
21 protected $flags;
22
23 public static function create(?string $domain = null, ?string $language = null): Translations
24 {
25 $translations = new static();
26
27 if (isset($domain)) {
28 $translations->setDomain($domain);
29 }
30
31 if (isset($language)) {
32 $translations->setLanguage($language);
33 }
34
35 return $translations;
36 }
37
38 protected function __construct()
39 {
40 $this->headers = new Headers();
41 $this->flags = new Flags();
42 }
43
44 public function __clone()
45 {
46 foreach ($this->translations as $id => $translation) {
47 $this->translations[$id] = clone $translation;
48 }
49
50 $this->headers = clone $this->headers;
51 }
52
53 public function setDescription(?string $description): self
54 {
55 $this->description = $description;
56
57 return $this;
58 }
59
60 public function getDescription(): ?string
61 {
62 return $this->description;
63 }
64
65 public function getFlags(): Flags
66 {
67 return $this->flags;
68 }
69
70 public function toArray(): array
71 {
72 return [
73 'description' => $this->description,
74 'headers' => $this->headers->toArray(),
75 'flags' => $this->flags->toArray(),
76 'translations' => array_map(
77 function ($translation) {
78 return $translation->toArray();
79 },
80 array_values($this->translations)
81 ),
82 ];
83 }
84
85 #[ReturnTypeWillChange]
86 public function getIterator()
87 {
88 return new ArrayIterator($this->translations);
89 }
90
91 public function getTranslations(): array
92 {
93 return $this->translations;
94 }
95
96 public function count(): int
97 {
98 return count($this->translations);
99 }
100
101 public function getHeaders(): Headers
102 {
103 return $this->headers;
104 }
105
106 public function add(Translation $translation): self
107 {
108 $id = $translation->getId();
109
110 $this->translations[$id] = $translation;
111
112 return $this;
113 }
114
115 public function addOrMerge(Translation $translation, int $mergeStrategy = 0): Translation
116 {
117 $id = $translation->getId();
118
119 if (isset($this->translations[$id])) {
120 return $this->translations[$id] = $this->translations[$id]->mergeWith($translation, $mergeStrategy);
121 }
122
123 return $this->translations[$id] = $translation;
124 }
125
126 public function remove(Translation $translation): self
127 {
128 unset($this->translations[$translation->getId()]);
129
130 return $this;
131 }
132
133 public function setDomain(string $domain): self
134 {
135 $this->getHeaders()->setDomain($domain);
136
137 return $this;
138 }
139
140 public function getDomain(): ?string
141 {
142 return $this->getHeaders()->getDomain();
143 }
144
145 public function setLanguage(string $language): self
146 {
147 $info = Language::getById($language);
148
149 if (empty($info)) {
150 throw new InvalidArgumentException(sprintf('The language "%s" is not valid', $language));
151 }
152
153 $this->getHeaders()
154 ->setLanguage($language)
155 ->setPluralForm(count($info->categories), $info->formula);
156
157 return $this;
158 }
159
160 public function getLanguage(): ?string
161 {
162 return $this->getHeaders()->getLanguage();
163 }
164
165 public function find(?string $context, string $original): ?Translation
166 {
167 return $this->translations[(Translation::create($context, $original))->getId()] ?? null;
168 }
169
170 public function has(Translation $translation): bool
171 {
172 return (bool) ($this->translations[$translation->getId()] ?? false);
173 }
174
175 public function mergeWith(Translations $translations, int $strategy = 0): Translations
176 {
177 $merged = clone $this;
178
179 if ($strategy & Merge::HEADERS_THEIRS) {
180 $merged->headers = clone $translations->headers;
181 } elseif (!($strategy & Merge::HEADERS_OURS)) {
182 $merged->headers = $merged->headers->mergeWith($translations->headers);
183 }
184
185 if ($strategy & Merge::FLAGS_THEIRS) {
186 $merged->flags = clone $translations->flags;
187 } elseif (!($strategy & Merge::FLAGS_OURS)) {
188 $merged->flags = $merged->flags->mergeWith($translations->flags);
189 }
190
191 if (!$merged->description) {
192 $merged->description = $translations->description;
193 }
194
195 foreach ($translations as $id => $translation) {
196 if (isset($merged->translations[$id])) {
197 $translation = $merged->translations[$id]->mergeWith($translation, $strategy);
198 }
199
200 $merged->add($translation);
201 }
202
203 if ($strategy & Merge::TRANSLATIONS_THEIRS) {
204 $merged->translations = array_intersect_key($merged->translations, $translations->translations);
205 } elseif ($strategy & Merge::TRANSLATIONS_OURS) {
206 $merged->translations = array_intersect_key($merged->translations, $this->translations);
207 }
208
209 return $merged;
210 }
211 }
212