1 | <?php |
||
9 | trait HasTranslations |
||
10 | { |
||
11 | protected $modelLocale = null; |
||
12 | |||
13 | /** |
||
14 | * @param string $locale |
||
15 | * |
||
16 | * @return $this |
||
17 | */ |
||
18 | public function setModelLocale(string $locale = '') |
||
19 | { |
||
20 | $this->modelLocale = $locale; |
||
21 | return $this; |
||
22 | } |
||
23 | |||
24 | /** |
||
25 | * |
||
26 | * @return $this |
||
27 | */ |
||
28 | public function forgetModelLocale() |
||
29 | { |
||
30 | $this->modelLocale = null; |
||
31 | return $this; |
||
32 | } |
||
33 | |||
34 | /** |
||
35 | * @param string $key |
||
36 | * |
||
37 | * @return mixed |
||
38 | */ |
||
39 | public function getAttributeValue($key) |
||
40 | { |
||
41 | if (!$this->isTranslatableAttribute($key)) { |
||
42 | return parent::getAttributeValue($key); |
||
43 | } |
||
44 | |||
45 | return $this->getTranslation($key, is_null($this->modelLocale) ? config('app.locale') : $this->modelLocale); |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * @param string $key |
||
50 | * @param string $locale |
||
51 | * |
||
52 | * @return mixed |
||
53 | */ |
||
54 | public function translate(string $key, string $locale = '') |
||
58 | |||
59 | /*** |
||
60 | * @param string $key |
||
61 | * @param string $locale |
||
62 | * @param bool $useFallbackLocale |
||
63 | * |
||
64 | * @return mixed |
||
65 | */ |
||
66 | public function getTranslation(string $key, string $locale, bool $useFallbackLocale = true) |
||
80 | |||
81 | public function getTranslationWithFallback(string $key, string $locale) |
||
85 | |||
86 | public function getTranslationWithoutFallback(string $key, string $locale) |
||
90 | |||
91 | public function getTranslations($key) : array |
||
97 | |||
98 | /** |
||
99 | * @param string $key |
||
100 | * @param string $locale |
||
101 | * @param $value |
||
102 | * |
||
103 | * @return $this |
||
104 | */ |
||
105 | public function setTranslation(string $key, string $locale, $value) |
||
126 | |||
127 | /** |
||
128 | * @param string $key |
||
129 | * @param array $translations |
||
130 | * |
||
131 | * @return $this |
||
132 | */ |
||
133 | public function setTranslations(string $key, array $translations) |
||
143 | |||
144 | /** |
||
145 | * @param string $key |
||
146 | * @param string $locale |
||
147 | * |
||
148 | * @return $this |
||
149 | */ |
||
150 | public function forgetTranslation(string $key, string $locale) |
||
160 | |||
161 | public function getTranslatedLocales(string $key) : array |
||
165 | |||
166 | public function isTranslatableAttribute(string $key) : bool |
||
170 | |||
171 | protected function guardAgainstUntranslatableAttribute(string $key) |
||
177 | |||
178 | protected function normalizeLocale(string $key, string $locale, bool $useFallbackLocale) : string |
||
194 | |||
195 | public function getTranslatableAttributes() : array |
||
201 | |||
202 | public function getCasts() : array |
||
209 | } |
||
210 |
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.