1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Translation; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Trait TranslatableTrait |
9
|
|
|
* @package App\Translation |
10
|
|
|
* @property ArrayCollection $translations |
11
|
|
|
*/ |
12
|
|
|
trait TranslatableTrait |
13
|
|
|
{ |
14
|
|
|
private $isTranslationsMappedByLocale = false; |
15
|
|
|
|
16
|
4 |
|
public function updateTranslations(array $translations, callable $add, callable $update = null) |
17
|
|
|
{ |
18
|
4 |
|
foreach ($translations as $translation) { |
19
|
4 |
|
$locale = is_object($translation) ? $translation->getLocale() : $translation['locale']; |
20
|
4 |
|
if (null === $oldTranslation = $this->getTranslation($locale, false)) { |
21
|
3 |
|
$add($translation); |
22
|
3 |
|
continue; |
23
|
|
|
} |
24
|
|
|
|
25
|
1 |
|
if ($update === null) { |
26
|
|
|
// This will called only when there's update action and $update function not defined |
27
|
|
|
// But you still can keep it as null if you creating entity |
28
|
|
|
throw new \InvalidArgumentException('Unexpected behavior: founded old translation but $update function not defined'); |
29
|
|
|
} |
30
|
|
|
|
31
|
1 |
|
$update($translation, $oldTranslation); |
32
|
|
|
} |
33
|
4 |
|
} |
34
|
|
|
|
35
|
4 |
|
public function addTranslation(EntityTranslationInterface $translation): self |
36
|
|
|
{ |
37
|
4 |
|
$this->translations->set($translation->getLocale(), $translation); |
38
|
|
|
|
39
|
4 |
|
return $this; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return array |
44
|
|
|
*/ |
45
|
10 |
|
public function getTranslations(): array |
46
|
|
|
{ |
47
|
10 |
|
if ($this->isTranslationsMappedByLocale === true) { |
48
|
4 |
|
return $this->translations->toArray(); |
49
|
|
|
} |
50
|
|
|
|
51
|
8 |
|
$this->mapTranslationsByLocale(); |
52
|
8 |
|
return $this->translations->toArray(); |
53
|
|
|
} |
54
|
|
|
|
55
|
5 |
|
public function getTranslation(string $locale, bool $useFallbackLocale = true): ?EntityTranslationInterface |
56
|
|
|
{ |
57
|
5 |
|
if ($this->isTranslationsMappedByLocale === false) { |
58
|
5 |
|
$this->mapTranslationsByLocale(); |
59
|
|
|
} |
60
|
|
|
|
61
|
5 |
|
$translation = $this->translations->get($locale); |
62
|
|
|
|
63
|
5 |
|
if ($translation === null && $useFallbackLocale === true) { |
64
|
1 |
|
return $this->getFallbackTranslation(); |
65
|
|
|
} |
66
|
|
|
|
67
|
5 |
|
return $translation; |
68
|
|
|
} |
69
|
|
|
|
70
|
1 |
|
private function getFallbackTranslation() |
71
|
|
|
{ |
72
|
1 |
|
if (null === $translation = $this->translations->first()) { |
73
|
|
|
throw new \ErrorException(sprintf('You are trying to get translation for %s with ID %s but there\'s no translations found.', self::class, $this->id)); |
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
1 |
|
return $translation; |
77
|
|
|
} |
78
|
|
|
|
79
|
11 |
|
private function mapTranslationsByLocale(): void |
80
|
|
|
{ |
81
|
11 |
|
$mappedTranslations = new ArrayCollection(); |
82
|
|
|
|
83
|
11 |
|
foreach ($this->translations as $translation) { |
84
|
9 |
|
$mappedTranslations->set($translation->getLocale(), $translation); |
85
|
|
|
} |
86
|
|
|
|
87
|
11 |
|
$this->isTranslationsMappedByLocale = true; |
88
|
11 |
|
$this->translations = $mappedTranslations; |
89
|
|
|
} |
90
|
|
|
} |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: