1 | <?php |
||
9 | trait HasTranslations |
||
10 | { |
||
11 | /** |
||
12 | * @param string $key |
||
13 | * |
||
14 | * @return mixed |
||
15 | */ |
||
16 | public function getAttributeValue($key) |
||
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 = '') |
||
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) |
||
76 | |||
77 | public function getTranslationWithFallback(string $key, string $locale) |
||
81 | |||
82 | public function getTranslationWithoutFallback(string $key, string $locale) |
||
86 | |||
87 | public function getTranslations($key) : array |
||
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) |
||
122 | |||
123 | /** |
||
124 | * @param string $key |
||
125 | * @param array $translations |
||
126 | * |
||
127 | * @return $this |
||
128 | */ |
||
129 | public function setTranslations(string $key, array $translations) |
||
139 | |||
140 | /** |
||
141 | * @param string $key |
||
142 | * @param string $locale |
||
143 | * |
||
144 | * @return $this |
||
145 | */ |
||
146 | public function forgetTranslation(string $key, string $locale) |
||
156 | |||
157 | public function forgetAllTranslations(string $locale) |
||
163 | |||
164 | public function getTranslatedLocales(string $key) : array |
||
168 | |||
169 | public function isTranslatableAttribute(string $key) : bool |
||
173 | |||
174 | protected function guardAgainstUntranslatableAttribute(string $key) |
||
180 | |||
181 | protected function normalizeLocale(string $key, string $locale, bool $useFallbackLocale) : string |
||
197 | |||
198 | public function getTranslatableAttributes() : array |
||
204 | |||
205 | public function getCasts() : array |
||
212 | } |
||
213 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.