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\ImportTranslationInterface; |
9
|
|
|
use Omatech\Mage\Core\Domains\Translations\Contracts\TranslationInterface; |
10
|
|
|
use Omatech\Mage\Core\Domains\Translations\Features\ExistsAndDeleteTranslation; |
11
|
|
|
use Omatech\Mage\Core\Domains\Translations\Features\FindOrFailTranslation; |
12
|
|
|
use Omatech\Mage\Core\Domains\Translations\Features\UpdateOrCreateTranslation; |
13
|
|
|
use Omatech\Mage\Core\Domains\Translations\Jobs\AllTranslation; |
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
|
14 |
|
public function getId(): ?int |
30
|
|
|
{ |
31
|
14 |
|
return $this->id; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @return Translation |
36
|
|
|
*/ |
37
|
14 |
|
public function setId(int $id): self |
38
|
|
|
{ |
39
|
14 |
|
$this->id = $id; |
40
|
|
|
|
41
|
14 |
|
return $this; |
42
|
|
|
} |
43
|
|
|
|
44
|
14 |
|
public function getGroup(): string |
45
|
|
|
{ |
46
|
14 |
|
return $this->group; |
47
|
|
|
} |
48
|
|
|
|
49
|
14 |
|
public function getKey(): string |
50
|
|
|
{ |
51
|
14 |
|
return $this->key; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return Translation |
56
|
|
|
*/ |
57
|
14 |
|
public function setKey(string $key): self |
58
|
|
|
{ |
59
|
14 |
|
$key = explode('.', $key); |
60
|
|
|
|
61
|
14 |
|
if (count($key) > 1) { |
62
|
13 |
|
$this->group = $key[0]; |
63
|
13 |
|
$this->key = implode('.', array_slice($key, 1)); |
64
|
|
|
} else { |
65
|
3 |
|
$this->group = 'single'; |
66
|
3 |
|
$this->key = $key[0]; |
67
|
|
|
} |
68
|
|
|
|
69
|
14 |
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return $this |
74
|
|
|
*/ |
75
|
14 |
|
public function setTranslation(string $language, string $text): self |
76
|
|
|
{ |
77
|
14 |
|
$this->translations[$language] = $text; |
78
|
|
|
|
79
|
14 |
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
14 |
|
public function getTranslations(): array |
83
|
|
|
{ |
84
|
14 |
|
return $this->translations; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return $this |
89
|
|
|
*/ |
90
|
14 |
|
public function setTranslations(array $translations): self |
91
|
|
|
{ |
92
|
14 |
|
foreach ($translations as $language => $text) { |
93
|
4 |
|
$this->setTranslation($language, $text); |
94
|
|
|
} |
95
|
|
|
|
96
|
14 |
|
return $this; |
97
|
|
|
} |
98
|
|
|
|
99
|
15 |
|
private static function getLocales(): array |
100
|
|
|
{ |
101
|
15 |
|
$availableLocales = config('mage.translations.available_locales'); |
102
|
15 |
|
$locales = []; |
103
|
|
|
|
104
|
15 |
|
foreach ($availableLocales as $locale) { |
105
|
15 |
|
if (! array_key_exists($locale['locale'], $locales)) { |
106
|
15 |
|
$locales[] = $locale['locale']; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
15 |
|
return $locales; |
111
|
|
|
} |
112
|
|
|
|
113
|
14 |
|
private function setMissingTranslations(): void |
114
|
|
|
{ |
115
|
14 |
|
foreach (static::getLocales() as $locale) { |
116
|
14 |
|
if (! array_key_exists($locale, $this->getTranslations())) { |
117
|
14 |
|
$this->setTranslation($locale, $this->getGroup().'.'.$this->getKey()); |
118
|
|
|
} |
119
|
|
|
} |
120
|
14 |
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @return string |
124
|
|
|
*/ |
125
|
14 |
|
public function getSyncAt(): ?string |
126
|
|
|
{ |
127
|
14 |
|
return $this->syncAt; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param string $syncAt |
132
|
|
|
* |
133
|
|
|
* @return Translation |
134
|
|
|
*/ |
135
|
14 |
|
public function setSyncAt(?string $syncAt): self |
136
|
|
|
{ |
137
|
14 |
|
$this->syncAt = $syncAt; |
138
|
|
|
|
139
|
14 |
|
return $this; |
140
|
|
|
} |
141
|
|
|
|
142
|
4 |
|
public function getCreatedAt(): string |
143
|
|
|
{ |
144
|
4 |
|
return $this->createdAt; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @return Translation |
149
|
|
|
*/ |
150
|
14 |
|
public function setCreatedAt(string $createdAt): self |
151
|
|
|
{ |
152
|
14 |
|
$this->createdAt = $createdAt; |
153
|
|
|
|
154
|
14 |
|
return $this; |
155
|
|
|
} |
156
|
|
|
|
157
|
4 |
|
public function getUpdatedAt(): string |
158
|
|
|
{ |
159
|
4 |
|
return $this->updatedAt; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @return Translation |
164
|
|
|
*/ |
165
|
14 |
|
public function setUpdatedAt(string $updatedAt): self |
166
|
|
|
{ |
167
|
14 |
|
$this->updatedAt = $updatedAt; |
168
|
|
|
|
169
|
14 |
|
return $this; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException |
174
|
|
|
* |
175
|
|
|
* @return mixed |
176
|
|
|
*/ |
177
|
1 |
|
public static function all(AllTranslationInterface $all) |
178
|
|
|
{ |
179
|
1 |
|
return app()->make(AllTranslation::class) |
180
|
1 |
|
->make($all, static::getLocales()); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException |
185
|
|
|
* |
186
|
|
|
* @return Translation |
187
|
|
|
*/ |
188
|
4 |
|
public static function find(string $key): self |
189
|
|
|
{ |
190
|
4 |
|
return app()->make(FindOrFailTranslation::class)->make($key); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException |
195
|
|
|
*/ |
196
|
14 |
|
public function save(): bool |
197
|
|
|
{ |
198
|
14 |
|
$this->setMissingTranslations(); |
199
|
|
|
|
200
|
14 |
|
return app()->make(UpdateOrCreateTranslation::class)->make($this); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException |
205
|
|
|
*/ |
206
|
3 |
|
public function delete(): bool |
207
|
|
|
{ |
208
|
3 |
|
return app()->make(ExistsAndDeleteTranslation::class)->make($this); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException |
213
|
|
|
* |
214
|
|
|
* @return mixed |
215
|
|
|
*/ |
216
|
2 |
|
public static function export( |
217
|
|
|
AllTranslationInterface $allTranslationInterface, |
218
|
|
|
ExportTranslationInterface $exportTranslationInterface, |
219
|
|
|
array $locales = null |
220
|
|
|
) { |
221
|
2 |
|
$locales = $locales ?? static::getLocales(); |
222
|
|
|
|
223
|
2 |
|
return app()->make(ExportTranslation::class) |
224
|
2 |
|
->make($allTranslationInterface, $exportTranslationInterface, $locales); |
225
|
|
|
} |
226
|
|
|
|
227
|
3 |
|
public static function import( |
228
|
|
|
ImportTranslationInterface $importTranslationInterface, |
229
|
|
|
string $path, |
230
|
|
|
string $locale = '' |
231
|
|
|
) { |
232
|
3 |
|
return app()->make(ImportTranslation::class) |
233
|
3 |
|
->make($importTranslationInterface, $path, $locale); |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
|