1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sylius package. |
5
|
|
|
* |
6
|
|
|
* (c) Paweł Jędrzejewski |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sylius\Component\Resource\Model; |
13
|
|
|
|
14
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @see TranslatableInterface |
18
|
|
|
* |
19
|
|
|
* @author Gonzalo Vilaseca <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
trait TranslatableTrait |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var TranslationInterface[] |
25
|
|
|
*/ |
26
|
|
|
protected $translations; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $currentLocale; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Cache current translation. Useful in Doctrine 2.4+ |
35
|
|
|
* |
36
|
|
|
* @var TranslationInterface |
37
|
|
|
*/ |
38
|
|
|
protected $currentTranslation; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
protected $fallbackLocale; |
44
|
|
|
|
45
|
|
|
public function __construct() |
46
|
|
|
{ |
47
|
|
|
$this->translations = new ArrayCollection(); |
|
|
|
|
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param string $locale |
|
|
|
|
52
|
|
|
* |
53
|
|
|
* @return TranslationInterface |
54
|
|
|
*/ |
55
|
|
|
public function getTranslation($locale = null) |
56
|
|
|
{ |
57
|
|
|
$locale = $locale ?: $this->currentLocale; |
58
|
|
|
if (null === $locale) { |
59
|
|
|
throw new \RuntimeException('No locale has been set and current locale is undefined.'); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$translation = $this->translations->get($locale); |
63
|
|
|
if (null !== $translation) { |
64
|
|
|
return $translation; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$fallbackTranslation = $this->translations->get($this->fallbackLocale); |
68
|
|
|
if (null !== $fallbackTranslation) { |
69
|
|
|
return $fallbackTranslation; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$translation = $this->createTranslation(); |
73
|
|
|
$translation->setLocale($locale); |
74
|
|
|
|
75
|
|
|
$this->addTranslation($translation); |
76
|
|
|
|
77
|
|
|
return $translation; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return TranslationInterface[] |
82
|
|
|
*/ |
83
|
|
|
public function getTranslations() |
84
|
|
|
{ |
85
|
|
|
return $this->translations; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param TranslationInterface $translation |
90
|
|
|
* |
91
|
|
|
* @return bool |
92
|
|
|
*/ |
93
|
|
|
public function hasTranslation(TranslationInterface $translation) |
94
|
|
|
{ |
95
|
|
|
return $this->translations->containsKey($translation->getLocale()); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param TranslationInterface $translation |
100
|
|
|
*/ |
101
|
|
|
public function addTranslation(TranslationInterface $translation) |
102
|
|
|
{ |
103
|
|
|
if (!$this->translations->containsKey($translation->getLocale())) { |
104
|
|
|
$this->translations->set($translation->getLocale(), $translation); |
105
|
|
|
$translation->setTranslatable($this); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param TranslationInterface $translation |
111
|
|
|
*/ |
112
|
|
|
public function removeTranslation(TranslationInterface $translation) |
113
|
|
|
{ |
114
|
|
|
if ($this->translations->removeElement($translation)) { |
115
|
|
|
$translation->setTranslatable(null); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param string $currentLocale |
121
|
|
|
*/ |
122
|
|
|
public function setCurrentLocale($currentLocale) |
123
|
|
|
{ |
124
|
|
|
$this->currentLocale = $currentLocale; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param string $fallbackLocale |
129
|
|
|
*/ |
130
|
|
|
public function setFallbackLocale($fallbackLocale) |
131
|
|
|
{ |
132
|
|
|
$this->fallbackLocale = $fallbackLocale; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @return TranslationInterface |
137
|
|
|
*/ |
138
|
|
|
abstract protected function createTranslation(); |
139
|
|
|
} |
140
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..