1 | <?php namespace Arcanedev\Localization\Traits; |
||
15 | trait HasTranslations |
||
16 | { |
||
17 | /* ----------------------------------------------------------------- |
||
18 | | Getters |
||
19 | | ----------------------------------------------------------------- |
||
20 | */ |
||
21 | |||
22 | /** |
||
23 | * Get the translations. |
||
24 | * |
||
25 | * @return array |
||
26 | */ |
||
27 | 4 | public function getTranslationsAttribute(): array |
|
35 | |||
36 | /* ----------------------------------------------------------------- |
||
37 | | Main Methods |
||
38 | | ----------------------------------------------------------------- |
||
39 | */ |
||
40 | |||
41 | /** |
||
42 | * Get the translatable attributes. |
||
43 | * |
||
44 | * @return array |
||
45 | */ |
||
46 | abstract public function getTranslatableAttributes(); |
||
47 | |||
48 | /** |
||
49 | * Get the translated attribute value. |
||
50 | * |
||
51 | * @param string $key |
||
52 | * |
||
53 | * @return mixed |
||
54 | */ |
||
55 | 32 | public function getAttributeValue($key) |
|
61 | |||
62 | /** |
||
63 | * Set a given attribute on the model. |
||
64 | * |
||
65 | * @param string $key |
||
66 | * @param mixed $value |
||
67 | * |
||
68 | * @return self |
||
69 | */ |
||
70 | 80 | public function setAttribute($key, $value) |
|
76 | |||
77 | /** |
||
78 | * Get the translated attribute (alias). |
||
79 | * |
||
80 | * @param string $key |
||
81 | * @param string $locale |
||
82 | * |
||
83 | * @return mixed |
||
84 | */ |
||
85 | 8 | public function trans($key, $locale = '') |
|
89 | |||
90 | /*** |
||
91 | * Get the translated attribute. |
||
92 | * |
||
93 | * @param string $key |
||
94 | * @param string $locale |
||
95 | * @param bool $useFallbackLocale |
||
96 | * |
||
97 | * @return mixed |
||
98 | */ |
||
99 | 64 | public function getTranslation($key, $locale, $useFallbackLocale = true) |
|
109 | |||
110 | /** |
||
111 | * Get the translations for the given key. |
||
112 | * |
||
113 | * @param string|null $key |
||
114 | * |
||
115 | * @return array |
||
116 | */ |
||
117 | 96 | public function getTranslations($key = null) |
|
133 | |||
134 | /** |
||
135 | * Set a translation. |
||
136 | * |
||
137 | * @param string $key |
||
138 | * @param string $locale |
||
139 | * @param string $value |
||
140 | * |
||
141 | * @return self |
||
142 | */ |
||
143 | 92 | public function setTranslation($key, $locale, $value) |
|
162 | |||
163 | /** |
||
164 | * Set the translations. |
||
165 | * |
||
166 | * @param string $key |
||
167 | * @param array $translations |
||
168 | * |
||
169 | * @return self |
||
170 | */ |
||
171 | 8 | public function setTranslations($key, array $translations) |
|
181 | |||
182 | /** |
||
183 | * Forget a translation. |
||
184 | * |
||
185 | * @param string $key |
||
186 | * @param string $locale |
||
187 | * |
||
188 | * @return self |
||
189 | */ |
||
190 | 8 | public function forgetTranslation($key, $locale) |
|
202 | |||
203 | /** |
||
204 | * Forget all the translations by the given locale. |
||
205 | * |
||
206 | * @param string $locale |
||
207 | * |
||
208 | * @return self |
||
209 | */ |
||
210 | 4 | public function flushTranslations($locale) |
|
218 | |||
219 | /** |
||
220 | * Get the translated attribute's locales |
||
221 | * |
||
222 | * @param string $key |
||
223 | * |
||
224 | * @return array |
||
225 | */ |
||
226 | 68 | public function getTranslatedLocales($key): array |
|
230 | |||
231 | /** |
||
232 | * Check if has a translation. |
||
233 | * |
||
234 | * @param string $key |
||
235 | * @param string|null $locale |
||
236 | * |
||
237 | * @return bool |
||
238 | */ |
||
239 | 4 | public function hasTranslation(string $key, string $locale = null): bool |
|
245 | |||
246 | /** |
||
247 | * Check if the attribute is translatable. |
||
248 | * |
||
249 | * @param string $key |
||
250 | * |
||
251 | * @return bool |
||
252 | */ |
||
253 | 104 | public function isTranslatableAttribute($key) |
|
257 | |||
258 | /* ----------------------------------------------------------------- |
||
259 | | Other Methods |
||
260 | | ----------------------------------------------------------------- |
||
261 | */ |
||
262 | |||
263 | /** |
||
264 | * Get the locale. |
||
265 | * |
||
266 | * @return string |
||
267 | */ |
||
268 | 28 | protected function getLocale(): string |
|
272 | |||
273 | /** |
||
274 | * Guard against untranslatable attribute. |
||
275 | * |
||
276 | * @param string $key |
||
277 | */ |
||
278 | 100 | protected function guardAgainstNonTranslatableAttribute($key) |
|
284 | |||
285 | /** |
||
286 | * Normalize the locale. |
||
287 | * |
||
288 | * @param string $key |
||
289 | * @param string $locale |
||
290 | * @param bool $useFallback |
||
291 | * |
||
292 | * @return string |
||
293 | */ |
||
294 | 64 | protected function normalizeLocale($key, $locale, $useFallback) |
|
301 | |||
302 | /* ----------------------------------------------------------------- |
||
303 | | Eloquent Methods |
||
304 | | ----------------------------------------------------------------- |
||
305 | */ |
||
306 | |||
307 | /** |
||
308 | * Get the casts array. |
||
309 | * |
||
310 | * @return array |
||
311 | */ |
||
312 | 84 | public function getCasts() |
|
319 | |||
320 | /** |
||
321 | * Determine if a get mutator exists for an attribute. |
||
322 | * |
||
323 | * @param string $key |
||
324 | * |
||
325 | * @return bool |
||
326 | */ |
||
327 | abstract public function hasGetMutator($key); |
||
328 | |||
329 | /** |
||
330 | * Get the value of an attribute using its mutator. |
||
331 | * |
||
332 | * @param string $key |
||
333 | * @param mixed $value |
||
334 | * |
||
335 | * @return mixed |
||
336 | */ |
||
337 | abstract protected function mutateAttribute($key, $value); |
||
338 | |||
339 | /** |
||
340 | * Get an attribute from the $attributes array. |
||
341 | * |
||
342 | * @param string $key |
||
343 | * |
||
344 | * @return mixed |
||
345 | */ |
||
346 | abstract protected function getAttributeFromArray($key); |
||
347 | |||
348 | /** |
||
349 | * Determine if a set mutator exists for an attribute. |
||
350 | * |
||
351 | * @param string $key |
||
352 | * |
||
353 | * @return bool |
||
354 | */ |
||
355 | abstract public function hasSetMutator($key); |
||
356 | |||
357 | /** |
||
358 | * Encode the given value as JSON. |
||
359 | * |
||
360 | * @param mixed $value |
||
361 | * |
||
362 | * @return string |
||
363 | */ |
||
364 | abstract protected function asJson($value); |
||
365 | } |
||
366 |
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: