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