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