|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Locastic\ApiPlatformTranslationBundle\Model; |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
|
8
|
|
|
use Doctrine\Common\Collections\Collection; |
|
9
|
|
|
use Doctrine\Common\Collections\Criteria; |
|
10
|
|
|
use Doctrine\Common\Collections\Expr\Comparison; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @see TranslatableInterface |
|
14
|
|
|
* |
|
15
|
|
|
* @author Gonzalo Vilaseca <[email protected]> |
|
16
|
|
|
* @template T of TranslationInterface |
|
17
|
|
|
*/ |
|
18
|
|
|
trait TranslatableTrait |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* Protected to allow access in classes using this Trait or extending provided AbstractTranslatable |
|
22
|
|
|
* @var Collection<string, TranslationInterface> |
|
23
|
|
|
* @psalm-var Collection<string, T> |
|
24
|
|
|
*/ |
|
25
|
|
|
protected Collection $translations; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var array|TranslationInterface[] |
|
29
|
|
|
* @psalm-var array<string, T> |
|
30
|
|
|
*/ |
|
31
|
|
|
protected array $translationsCache = []; |
|
32
|
|
|
protected ?string $currentLocale = null; |
|
33
|
|
|
protected ?string $fallbackLocale = null; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @codeCoverageIgnore |
|
37
|
|
|
*/ |
|
38
|
|
|
public function __construct() |
|
39
|
|
|
{ |
|
40
|
|
|
$this->translations = new ArrayCollection(); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* {@inheritdoc} |
|
45
|
|
|
* |
|
46
|
|
|
* @return TranslationInterface |
|
47
|
|
|
* @psalm-return T |
|
48
|
|
|
* |
|
49
|
|
|
* @throws \RuntimeException |
|
50
|
|
|
*/ |
|
51
|
|
|
public function getTranslation(?string $locale = null): TranslationInterface |
|
52
|
|
|
{ |
|
53
|
|
|
$locale = $locale ?: $this->currentLocale; |
|
54
|
|
|
if (null === $locale) { |
|
55
|
|
|
throw new \RuntimeException('No locale has been set and current locale is undefined.'); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
$translation = $this->matchTranslation($locale); |
|
59
|
|
|
if (null !== $translation) { |
|
60
|
|
|
return $translation; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
if ($locale !== $this->fallbackLocale && null !== $this->fallbackLocale) { |
|
64
|
|
|
$fallbackTranslation = $this->matchTranslation($this->fallbackLocale); |
|
65
|
|
|
if (null !== $fallbackTranslation) { |
|
66
|
|
|
return $fallbackTranslation; |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$translation = $this->createTranslation(); |
|
71
|
|
|
$translation->setLocale($locale); |
|
72
|
|
|
|
|
73
|
|
|
$this->addTranslation($translation); |
|
74
|
|
|
|
|
75
|
|
|
$this->translationsCache[$locale] = $translation; |
|
76
|
|
|
|
|
77
|
|
|
return $translation; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param string $locale |
|
82
|
|
|
* @return TranslationInterface|null |
|
83
|
|
|
* @psalm-return T|null |
|
84
|
|
|
*/ |
|
85
|
|
|
private function matchTranslation(string $locale): ?TranslationInterface |
|
86
|
|
|
{ |
|
87
|
|
|
if (isset($this->translationsCache[$locale])) { |
|
88
|
|
|
return $this->translationsCache[$locale]; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
$expr = new Comparison('locale', '=', $locale); |
|
92
|
|
|
$translation = $this->translations->matching(new Criteria($expr))->first(); |
|
|
|
|
|
|
93
|
|
|
|
|
94
|
|
|
if ($translation instanceof TranslationInterface) { |
|
95
|
|
|
$this->translationsCache[$locale] = $translation; |
|
96
|
|
|
|
|
97
|
|
|
return $translation; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return null; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @return string[] |
|
105
|
|
|
*/ |
|
106
|
|
|
public function getTranslationLocales(): array |
|
107
|
|
|
{ |
|
108
|
|
|
$locales = []; |
|
109
|
|
|
|
|
110
|
|
|
foreach ($this->getTranslations() as $translation) { |
|
111
|
|
|
$locales[] = $translation->getLocale(); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
return $locales; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @param string $locale |
|
119
|
|
|
*/ |
|
120
|
|
|
public function removeTranslationWithLocale(string $locale): void |
|
121
|
|
|
{ |
|
122
|
|
|
$translations = $this->getTranslations(); |
|
123
|
|
|
|
|
124
|
|
|
foreach ($translations as $translation) { |
|
125
|
|
|
if ($translation->getLocale() === $locale) { |
|
126
|
|
|
$this->removeTranslation($translation); |
|
127
|
|
|
|
|
128
|
|
|
$translation->setTranslatable(null); |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @return Collection<string, TranslationInterface> |
|
135
|
|
|
* @psalm-return Collection<string, T> |
|
136
|
|
|
*/ |
|
137
|
|
|
public function getTranslations(): Collection |
|
138
|
|
|
{ |
|
139
|
|
|
return $this->translations; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @param TranslationInterface $translation |
|
144
|
|
|
* @psalm-param T $translation |
|
145
|
|
|
* @return bool |
|
146
|
|
|
*/ |
|
147
|
|
|
public function hasTranslation(TranslationInterface $translation): bool |
|
148
|
|
|
{ |
|
149
|
|
|
return isset($this->translationsCache[$translation->getLocale()]) |
|
150
|
|
|
|| $this->translations->containsKey($translation->getLocale()); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* @param TranslationInterface $translation |
|
155
|
|
|
* @psalm-param T $translation |
|
156
|
|
|
* @return void |
|
157
|
|
|
*/ |
|
158
|
|
|
public function addTranslation(TranslationInterface $translation): void |
|
159
|
|
|
{ |
|
160
|
|
|
if (!$this->hasTranslation($translation)) { |
|
161
|
|
|
$this->translationsCache[$translation->getLocale()] = $translation; |
|
162
|
|
|
|
|
163
|
|
|
$this->translations->set($translation->getLocale(), $translation); |
|
164
|
|
|
$translation->setTranslatable($this); |
|
|
|
|
|
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* @param TranslationInterface $translation |
|
170
|
|
|
* @psalm-param T $translation |
|
171
|
|
|
* @return void |
|
172
|
|
|
*/ |
|
173
|
|
|
public function removeTranslation(TranslationInterface $translation): void |
|
174
|
|
|
{ |
|
175
|
|
|
if ($this->translations->removeElement($translation)) { |
|
176
|
|
|
unset($this->translationsCache[$translation->getLocale()]); |
|
177
|
|
|
|
|
178
|
|
|
$translation->setTranslatable(null); |
|
179
|
|
|
} |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
public function setCurrentLocale(?string $currentLocale): void |
|
183
|
|
|
{ |
|
184
|
|
|
$this->currentLocale = $currentLocale; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
public function setFallbackLocale(?string $fallbackLocale): void |
|
188
|
|
|
{ |
|
189
|
|
|
$this->fallbackLocale = $fallbackLocale; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* Create resource translation model. |
|
194
|
|
|
* |
|
195
|
|
|
* @return TranslationInterface $translation |
|
196
|
|
|
* @psalm-return T $translation |
|
197
|
|
|
*/ |
|
198
|
|
|
abstract protected function createTranslation(): TranslationInterface; |
|
199
|
|
|
} |
|
200
|
|
|
|