1 | <?php namespace Arcanedev\Localization\Traits; |
||
13 | trait HasTranslations |
||
14 | { |
||
15 | /* ----------------------------------------------------------------- |
||
16 | | Main Methods |
||
17 | | ----------------------------------------------------------------- |
||
18 | */ |
||
19 | |||
20 | /** |
||
21 | * Get the translatable attributes. |
||
22 | * |
||
23 | * @return array |
||
24 | */ |
||
25 | abstract public function getTranslatableAttributes(); |
||
26 | |||
27 | /** |
||
28 | * Get the translated attribute value. |
||
29 | * |
||
30 | * @param string $key |
||
31 | * |
||
32 | * @return mixed |
||
33 | */ |
||
34 | 18 | public function getAttributeValue($key) |
|
40 | |||
41 | /** |
||
42 | * Get the translated attribute (alias). |
||
43 | * |
||
44 | * @param string $key |
||
45 | * @param string $locale |
||
46 | * |
||
47 | * @return mixed |
||
48 | */ |
||
49 | 6 | public function trans($key, $locale = '') |
|
53 | |||
54 | /*** |
||
55 | * Get the translated attribute. |
||
56 | * |
||
57 | * @param string $key |
||
58 | * @param string $locale |
||
59 | * @param bool $useFallback |
||
60 | * |
||
61 | * @return mixed |
||
62 | */ |
||
63 | 33 | public function getTranslation($key, $locale, $useFallback = true) |
|
73 | |||
74 | /** |
||
75 | * Get the translations for the given key. |
||
76 | * |
||
77 | * @param string $key |
||
78 | * |
||
79 | * @return array |
||
80 | */ |
||
81 | 48 | public function getTranslations($key) |
|
87 | |||
88 | /** |
||
89 | * Set a translation. |
||
90 | * |
||
91 | * @param string $key |
||
92 | * @param string $locale |
||
93 | * @param string $value |
||
94 | * |
||
95 | * @return self |
||
96 | */ |
||
97 | 48 | public function setTranslation($key, $locale, $value) |
|
114 | |||
115 | /** |
||
116 | * Set the translations. |
||
117 | * |
||
118 | * @param string $key |
||
119 | * @param array $translations |
||
120 | * |
||
121 | * @return self |
||
122 | */ |
||
123 | 3 | public function setTranslations($key, array $translations) |
|
124 | { |
||
125 | 3 | $this->guardAgainstUntranslatableAttribute($key); |
|
126 | |||
127 | 3 | foreach ($translations as $locale => $translation) { |
|
128 | 3 | $this->setTranslation($key, $locale, $translation); |
|
129 | } |
||
130 | |||
131 | 3 | return $this; |
|
132 | } |
||
133 | |||
134 | /** |
||
135 | * Forget a translation. |
||
136 | * |
||
137 | * @param string $key |
||
138 | * @param string $locale |
||
139 | * |
||
140 | * @return self |
||
141 | */ |
||
142 | 6 | public function forgetTranslation($key, $locale) |
|
154 | |||
155 | /** |
||
156 | * Forget all the translations by the given locale. |
||
157 | * |
||
158 | * @param string $locale |
||
159 | * |
||
160 | * @return self |
||
161 | */ |
||
162 | 3 | public function flushTranslations($locale) |
|
170 | |||
171 | /** |
||
172 | * Get the translated attribute's locales |
||
173 | * |
||
174 | * @param string $key |
||
175 | * |
||
176 | * @return array |
||
177 | */ |
||
178 | 36 | public function getTranslatedLocales($key) |
|
182 | |||
183 | /** |
||
184 | * Check if the attribute is translatable. |
||
185 | * |
||
186 | * @param string $key |
||
187 | * |
||
188 | * @return bool |
||
189 | */ |
||
190 | 54 | public function isTranslatableAttribute($key) |
|
194 | |||
195 | /* ----------------------------------------------------------------- |
||
196 | | Other Methods |
||
197 | | ----------------------------------------------------------------- |
||
198 | */ |
||
199 | |||
200 | /** |
||
201 | * Guard against untranslatable attribute. |
||
202 | * |
||
203 | * @param string $key |
||
204 | * |
||
205 | * @throws \Arcanedev\Localization\Exceptions\UntranslatableAttributeException |
||
206 | */ |
||
207 | 51 | protected function guardAgainstUntranslatableAttribute($key) |
|
217 | |||
218 | /** |
||
219 | * Normalize the locale. |
||
220 | * |
||
221 | * @param string $key |
||
222 | * @param string $locale |
||
223 | * @param bool $useFallback |
||
224 | * |
||
225 | * @return string |
||
226 | */ |
||
227 | 33 | protected function normalizeLocale($key, $locale, $useFallback) |
|
234 | |||
235 | /** |
||
236 | * Mutate many translations. |
||
237 | * |
||
238 | * @param string $key |
||
239 | * @param array $translations |
||
240 | * |
||
241 | * @return string |
||
242 | */ |
||
243 | 3 | protected function mutateTranslations($key, array $translations) |
|
251 | |||
252 | /* ----------------------------------------------------------------- |
||
253 | | Eloquent Methods |
||
254 | | ----------------------------------------------------------------- |
||
255 | */ |
||
256 | |||
257 | /** |
||
258 | * Get the casts array. |
||
259 | * |
||
260 | * @return array |
||
261 | */ |
||
262 | 39 | public function getCasts() |
|
268 | |||
269 | /** |
||
270 | * Determine if a get mutator exists for an attribute. |
||
271 | * |
||
272 | * @param string $key |
||
273 | * |
||
274 | * @return bool |
||
275 | */ |
||
276 | abstract public function hasGetMutator($key); |
||
277 | |||
278 | /** |
||
279 | * Get the value of an attribute using its mutator. |
||
280 | * |
||
281 | * @param string $key |
||
282 | * @param mixed $value |
||
283 | * |
||
284 | * @return mixed |
||
285 | */ |
||
286 | abstract protected function mutateAttribute($key, $value); |
||
287 | |||
288 | /** |
||
289 | * Get an attribute from the $attributes array. |
||
290 | * |
||
291 | * @param string $key |
||
292 | * |
||
293 | * @return mixed |
||
294 | */ |
||
295 | abstract protected function getAttributeFromArray($key); |
||
296 | |||
297 | /** |
||
298 | * Set a given attribute on the model. |
||
299 | * |
||
300 | * @param string $key |
||
301 | * @param mixed $value |
||
302 | * |
||
303 | * @return self |
||
304 | */ |
||
305 | abstract public function setAttribute($key, $value); |
||
306 | |||
307 | /** |
||
308 | * Determine if a set mutator exists for an attribute. |
||
309 | * |
||
310 | * @param string $key |
||
311 | * |
||
312 | * @return bool |
||
313 | */ |
||
314 | abstract public function hasSetMutator($key); |
||
315 | |||
316 | /** |
||
317 | * Encode the given value as JSON. |
||
318 | * |
||
319 | * @param mixed $value |
||
320 | * |
||
321 | * @return string |
||
322 | */ |
||
323 | abstract protected function asJson($value); |
||
324 | } |
||
325 |
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: