These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Spatie\Translatable; |
||
4 | |||
5 | use Illuminate\Support\Str; |
||
6 | use Spatie\Translatable\Events\TranslationHasBeenSet; |
||
7 | use Spatie\Translatable\Exceptions\AttributeIsNotTranslatable; |
||
8 | |||
9 | trait HasTranslations |
||
10 | { |
||
11 | /** |
||
12 | * @param string $key |
||
13 | * |
||
14 | * @return mixed |
||
15 | */ |
||
16 | public function getAttributeValue($key) |
||
17 | { |
||
18 | if (!$this->isTranslatableAttribute($key)) { |
||
19 | return parent::getAttributeValue($key); |
||
20 | } |
||
21 | |||
22 | return $this->getTranslation($key, config('app.locale')); |
||
23 | } |
||
24 | |||
25 | /** |
||
26 | * Set a given attribute on the model. |
||
27 | * |
||
28 | * @param string $key |
||
29 | * @param mixed $value |
||
30 | * |
||
31 | * @return $this |
||
32 | */ |
||
33 | public function setAttribute($key, $value) |
||
34 | { |
||
35 | // pass arrays and untranslatable attributes to the parent method |
||
36 | if (!$this->isTranslatableAttribute($key) || is_array($value)) { |
||
37 | return parent::setAttribute($key, $value); |
||
38 | } |
||
39 | // if the attribute is translatable and not already translated (=array), |
||
40 | // set a translation for the current app locale |
||
41 | return $this->setTranslation($key, config('app.locale'), $value); |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * @param string $key |
||
46 | * @param string $locale |
||
47 | * |
||
48 | * @return mixed |
||
49 | */ |
||
50 | public function translate(string $key, string $locale = '') |
||
51 | { |
||
52 | return $this->getTranslation($key, $locale); |
||
53 | } |
||
54 | |||
55 | /*** |
||
56 | * @param string $key |
||
57 | * @param string $locale |
||
58 | * @param bool $useFallbackLocale |
||
59 | * |
||
60 | * @return mixed |
||
61 | */ |
||
62 | public function getTranslation(string $key, string $locale, bool $useFallbackLocale = true) |
||
63 | { |
||
64 | $locale = $this->normalizeLocale($key, $locale, $useFallbackLocale); |
||
65 | |||
66 | $translations = $this->getTranslations($key); |
||
67 | |||
68 | $translation = $translations[$locale] ?? ''; |
||
69 | |||
70 | if ($this->hasGetMutator($key)) { |
||
71 | return $this->mutateAttribute($key, $translation); |
||
72 | } |
||
73 | |||
74 | return $translation; |
||
75 | } |
||
76 | |||
77 | public function getTranslationWithFallback(string $key, string $locale) |
||
78 | { |
||
79 | return $this->getTranslation($key, $locale, true); |
||
80 | } |
||
81 | |||
82 | public function getTranslationWithoutFallback(string $key, string $locale) |
||
83 | { |
||
84 | return $this->getTranslation($key, $locale, false); |
||
85 | } |
||
86 | |||
87 | public function getTranslations($key) : array |
||
88 | { |
||
89 | $this->guardAgainstUntranslatableAttribute($key); |
||
90 | |||
91 | return json_decode($this->getAttributes()[$key] ?? '' ?: '{}', true); |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * @param string $key |
||
96 | * @param string $locale |
||
97 | * @param $value |
||
98 | * |
||
99 | * @return $this |
||
100 | */ |
||
101 | public function setTranslation(string $key, string $locale, $value) |
||
102 | { |
||
103 | $this->guardAgainstUntranslatableAttribute($key); |
||
104 | |||
105 | $translations = $this->getTranslations($key); |
||
106 | |||
107 | $oldValue = $translations[$locale] ?? ''; |
||
108 | |||
109 | if ($this->hasSetMutator($key)) { |
||
110 | $method = 'set'.Str::studly($key).'Attribute'; |
||
111 | $value = $this->{$method}($value); |
||
112 | } |
||
113 | |||
114 | $translations[$locale] = $value; |
||
115 | |||
116 | $this->attributes[$key] = $this->asJson($translations); |
||
0 ignored issues
–
show
|
|||
117 | |||
118 | event(new TranslationHasBeenSet($this, $key, $locale, $oldValue, $value)); |
||
119 | |||
120 | return $this; |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * @param string $key |
||
125 | * @param array $translations |
||
126 | * |
||
127 | * @return $this |
||
128 | */ |
||
129 | public function setTranslations(string $key, array $translations) |
||
130 | { |
||
131 | $this->guardAgainstUntranslatableAttribute($key); |
||
132 | |||
133 | foreach ($translations as $locale => $translation) { |
||
134 | $this->setTranslation($key, $locale, $translation); |
||
135 | } |
||
136 | |||
137 | return $this; |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * @param string $key |
||
142 | * @param string $locale |
||
143 | * |
||
144 | * @return $this |
||
145 | */ |
||
146 | public function forgetTranslation(string $key, string $locale) |
||
147 | { |
||
148 | $translations = $this->getTranslations($key); |
||
149 | |||
150 | unset($translations[$locale]); |
||
151 | |||
152 | $this->setAttribute($key, $translations); |
||
153 | |||
154 | return $this; |
||
155 | } |
||
156 | |||
157 | public function forgetAllTranslations(string $locale) |
||
158 | { |
||
159 | collect($this->getTranslatableAttributes())->each(function (string $attribute) use ($locale) { |
||
160 | $this->forgetTranslation($attribute, $locale); |
||
161 | }); |
||
162 | } |
||
163 | |||
164 | public function getTranslatedLocales(string $key) : array |
||
165 | { |
||
166 | return array_keys($this->getTranslations($key)); |
||
167 | } |
||
168 | |||
169 | public function isTranslatableAttribute(string $key) : bool |
||
170 | { |
||
171 | return in_array($key, $this->getTranslatableAttributes()); |
||
172 | } |
||
173 | |||
174 | protected function guardAgainstUntranslatableAttribute(string $key) |
||
175 | { |
||
176 | if (!$this->isTranslatableAttribute($key)) { |
||
177 | throw AttributeIsNotTranslatable::make($key, $this); |
||
178 | } |
||
179 | } |
||
180 | |||
181 | protected function normalizeLocale(string $key, string $locale, bool $useFallbackLocale) : string |
||
182 | { |
||
183 | if (in_array($locale, $this->getTranslatedLocales($key))) { |
||
184 | return $locale; |
||
185 | } |
||
186 | |||
187 | if (!$useFallbackLocale) { |
||
188 | return $locale; |
||
189 | } |
||
190 | |||
191 | if (!is_null($fallbackLocale = config('translatable.fallback_locale'))) { |
||
192 | return $fallbackLocale; |
||
193 | } |
||
194 | |||
195 | return $locale; |
||
196 | } |
||
197 | |||
198 | public function getTranslatableAttributes() : array |
||
199 | { |
||
200 | return is_array($this->translatable) |
||
201 | ? $this->translatable |
||
202 | : []; |
||
203 | } |
||
204 | |||
205 | public function getCasts() : array |
||
206 | { |
||
207 | return array_merge( |
||
208 | parent::getCasts(), |
||
209 | array_fill_keys($this->getTranslatableAttributes(), 'array') |
||
210 | ); |
||
211 | } |
||
212 | } |
||
213 |
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: