|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Omatech\Mage\Core\Domains\Translations; |
|
4
|
|
|
|
|
5
|
|
|
use Omatech\Mage\Core\Domains\Shared\Traits\FromArray; |
|
6
|
|
|
use Omatech\Mage\Core\Domains\Translations\Contracts\AllTranslationInterface; |
|
7
|
|
|
use Omatech\Mage\Core\Domains\Translations\Contracts\ExportTranslationInterface; |
|
8
|
|
|
use Omatech\Mage\Core\Domains\Translations\Contracts\FindTranslationInterface; |
|
9
|
|
|
use Omatech\Mage\Core\Domains\Translations\Contracts\ImportTranslationInterface; |
|
10
|
|
|
use Omatech\Mage\Core\Domains\Translations\Contracts\TranslationInterface; |
|
11
|
|
|
use Omatech\Mage\Core\Domains\Translations\Features\ExistsAndDeleteTranslation; |
|
12
|
|
|
use Omatech\Mage\Core\Domains\Translations\Features\FindOrFailTranslation; |
|
13
|
|
|
use Omatech\Mage\Core\Domains\Translations\Features\UpdateOrCreateTranslation; |
|
14
|
|
|
use Omatech\Mage\Core\Domains\Translations\Jobs\ExportTranslation; |
|
15
|
|
|
use Omatech\Mage\Core\Domains\Translations\Jobs\ImportTranslation; |
|
16
|
|
|
|
|
17
|
|
|
class Translation implements TranslationInterface |
|
18
|
|
|
{ |
|
19
|
|
|
use FromArray; |
|
20
|
|
|
|
|
21
|
|
|
private $id; |
|
22
|
|
|
private $group; |
|
23
|
|
|
private $key; |
|
24
|
|
|
private $translations = []; |
|
25
|
|
|
private $syncAt; |
|
26
|
|
|
private $createdAt; |
|
27
|
|
|
private $updatedAt; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @return int|null |
|
31
|
|
|
*/ |
|
32
|
19 |
|
public function getId(): ?int |
|
33
|
|
|
{ |
|
34
|
19 |
|
return $this->id; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param int $id |
|
39
|
|
|
* @return $this |
|
40
|
|
|
*/ |
|
41
|
19 |
|
public function setId(int $id) |
|
42
|
|
|
{ |
|
43
|
19 |
|
$this->id = $id; |
|
44
|
|
|
|
|
45
|
19 |
|
return $this; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @return string |
|
50
|
|
|
*/ |
|
51
|
19 |
|
public function getGroup(): string |
|
52
|
|
|
{ |
|
53
|
19 |
|
return $this->group; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @return string |
|
58
|
|
|
*/ |
|
59
|
19 |
|
public function getKey(): string |
|
60
|
|
|
{ |
|
61
|
19 |
|
return $this->key; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param string $key |
|
66
|
|
|
* @return $this |
|
67
|
|
|
*/ |
|
68
|
19 |
|
public function setKey(string $key) |
|
69
|
|
|
{ |
|
70
|
19 |
|
$key = explode('.', $key); |
|
71
|
|
|
|
|
72
|
19 |
|
if (count($key) > 1) { |
|
73
|
18 |
|
$this->group = $key[0]; |
|
74
|
18 |
|
$this->key = implode('.', array_slice($key, 1)); |
|
75
|
|
|
} else { |
|
76
|
2 |
|
$this->group = 'single'; |
|
77
|
2 |
|
$this->key = $key[0]; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
19 |
|
return $this; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param string $language |
|
85
|
|
|
* @param string $text |
|
86
|
|
|
* @return $this |
|
87
|
|
|
*/ |
|
88
|
19 |
|
public function setTranslation(string $language, string $text) |
|
89
|
|
|
{ |
|
90
|
19 |
|
$this->translations[$language] = $text; |
|
91
|
|
|
|
|
92
|
19 |
|
return $this; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @return array |
|
97
|
|
|
*/ |
|
98
|
19 |
|
public function getTranslations(): array |
|
99
|
|
|
{ |
|
100
|
19 |
|
return $this->translations; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @param array $translations |
|
105
|
|
|
* @return $this |
|
106
|
|
|
*/ |
|
107
|
18 |
|
public function setTranslations(array $translations) |
|
108
|
|
|
{ |
|
109
|
18 |
|
foreach ($translations as $language => $text) { |
|
110
|
9 |
|
$this->setTranslation($language, $text); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
18 |
|
return $this; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @return array |
|
118
|
|
|
*/ |
|
119
|
20 |
|
private static function getLocales(): array |
|
120
|
|
|
{ |
|
121
|
20 |
|
$availableLocales = config('mage.translations.available_locales'); |
|
122
|
20 |
|
$locales = []; |
|
123
|
|
|
|
|
124
|
20 |
|
foreach ($availableLocales as $locale) { |
|
125
|
20 |
|
if (! array_key_exists($locale['locale'], $locales)) { |
|
126
|
20 |
|
$locales[] = $locale['locale']; |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
20 |
|
return $locales; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @return void |
|
135
|
|
|
*/ |
|
136
|
19 |
|
private function setMissingTranslations(): void |
|
137
|
|
|
{ |
|
138
|
19 |
|
foreach (static::getLocales() as $locale) { |
|
|
|
|
|
|
139
|
19 |
|
if (! array_key_exists($locale, $this->getTranslations())) { |
|
140
|
17 |
|
$this->setTranslation($locale, $this->getGroup().'.'.$this->getKey()); |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
19 |
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @return string |
|
147
|
|
|
*/ |
|
148
|
19 |
|
public function getSyncAt(): ?string |
|
149
|
|
|
{ |
|
150
|
19 |
|
return $this->syncAt; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* @param string|null $syncAt |
|
155
|
|
|
* @return $this |
|
156
|
|
|
*/ |
|
157
|
19 |
|
public function setSyncAt(?string $syncAt) |
|
158
|
|
|
{ |
|
159
|
19 |
|
$this->syncAt = $syncAt; |
|
160
|
|
|
|
|
161
|
19 |
|
return $this; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* @return string |
|
166
|
|
|
*/ |
|
167
|
4 |
|
public function getCreatedAt(): string |
|
168
|
|
|
{ |
|
169
|
4 |
|
return $this->createdAt; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* @param string $createdAt |
|
174
|
|
|
* @return $this |
|
175
|
|
|
*/ |
|
176
|
19 |
|
public function setCreatedAt(string $createdAt) |
|
177
|
|
|
{ |
|
178
|
19 |
|
$this->createdAt = $createdAt; |
|
179
|
|
|
|
|
180
|
19 |
|
return $this; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* @return string |
|
185
|
|
|
*/ |
|
186
|
4 |
|
public function getUpdatedAt(): string |
|
187
|
|
|
{ |
|
188
|
4 |
|
return $this->updatedAt; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* @param string $updatedAt |
|
193
|
|
|
* @return $this |
|
194
|
|
|
*/ |
|
195
|
19 |
|
public function setUpdatedAt(string $updatedAt) |
|
196
|
|
|
{ |
|
197
|
19 |
|
$this->updatedAt = $updatedAt; |
|
198
|
|
|
|
|
199
|
19 |
|
return $this; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* @param AllTranslationInterface $all |
|
204
|
|
|
* @return mixed |
|
205
|
|
|
*/ |
|
206
|
1 |
|
public static function all(AllTranslationInterface $all) |
|
207
|
|
|
{ |
|
208
|
1 |
|
return $all->get(static::getLocales()); |
|
|
|
|
|
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* @param FindTranslationInterface $find |
|
213
|
|
|
* @param array $params |
|
214
|
|
|
* @return $this |
|
215
|
|
|
*/ |
|
216
|
9 |
|
public static function find(FindTranslationInterface $find, array $params): self |
|
217
|
|
|
{ |
|
218
|
9 |
|
if (isset($params['key'])) { |
|
219
|
9 |
|
$params['key'] = strtolower(str_replace(' ', '.', $params['key'])); |
|
220
|
|
|
} |
|
221
|
9 |
|
return (new FindOrFailTranslation())->make($find, $params); |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* @return bool |
|
226
|
|
|
* @throws Exceptions\TranslationAlreadyExistsException |
|
227
|
|
|
* @throws Exceptions\TranslationDoesNotExistsException |
|
228
|
|
|
* @throws Exceptions\TranslationExistsMustBeUniqueException |
|
229
|
|
|
*/ |
|
230
|
19 |
|
public function save(): bool |
|
231
|
|
|
{ |
|
232
|
19 |
|
$this->setMissingTranslations(); |
|
233
|
|
|
|
|
234
|
19 |
|
return (new UpdateOrCreateTranslation())->make($this); |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* @throws Exceptions\TranslationDoesNotExistsException |
|
239
|
|
|
*/ |
|
240
|
3 |
|
public function delete(): bool |
|
241
|
|
|
{ |
|
242
|
3 |
|
return (new ExistsAndDeleteTranslation())->make($this); |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
/** |
|
246
|
|
|
* @param AllTranslationInterface $all |
|
247
|
|
|
* @param ExportTranslationInterface $export |
|
248
|
|
|
* @param array|null $locales |
|
249
|
|
|
* @return string |
|
250
|
|
|
*/ |
|
251
|
2 |
|
public static function export( |
|
252
|
|
|
AllTranslationInterface $all, |
|
253
|
|
|
ExportTranslationInterface $export, |
|
254
|
|
|
array $locales = null |
|
255
|
|
|
) { |
|
256
|
2 |
|
$locales = $locales ?? static::getLocales(); |
|
|
|
|
|
|
257
|
|
|
|
|
258
|
2 |
|
return (new ExportTranslation())->make($all, $export, $locales); |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
/** |
|
262
|
|
|
* @param FindTranslationInterface $find |
|
263
|
|
|
* @param ImportTranslationInterface $import |
|
264
|
|
|
* @param string $path |
|
265
|
|
|
* @param string $locale |
|
266
|
|
|
* @return bool |
|
267
|
|
|
*/ |
|
268
|
7 |
|
public static function import( |
|
269
|
|
|
FindTranslationInterface $find, |
|
270
|
|
|
ImportTranslationInterface $import, |
|
271
|
|
|
string $path, |
|
272
|
|
|
string $locale = '' |
|
273
|
|
|
) { |
|
274
|
7 |
|
return (new ImportTranslation())->make($find, $import, $path, $locale); |
|
275
|
|
|
} |
|
276
|
|
|
} |
|
277
|
|
|
|
Let’s assume you have a class which uses late-static binding:
}
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClassto useselfinstead: