1 | <?php |
||
9 | trait HasTranslations |
||
10 | { |
||
11 | /** |
||
12 | * @param string $key |
||
13 | * |
||
14 | * @return mixed |
||
15 | */ |
||
16 | public function getAttributeValue($key) |
||
24 | |||
25 | /** |
||
26 | * @param string $key |
||
27 | * @param $value |
||
28 | * |
||
29 | * @return $this |
||
30 | */ |
||
31 | public function setAttribute($key, $value) |
||
32 | { |
||
33 | if ($this->isTranslatableAttribute($key) && !is_array($value)) { |
||
34 | return $this->setTranslation($key, app()->getLocale(), $value); |
||
35 | } |
||
36 | return parent::setAttribute($key, $value); |
||
37 | } |
||
38 | |||
39 | /** |
||
40 | * @param string $key |
||
41 | * @param string $locale |
||
42 | * |
||
43 | * @return mixed |
||
44 | */ |
||
45 | public function translate(string $key, string $locale = '') |
||
46 | { |
||
47 | return $this->getTranslation($key, $locale); |
||
48 | } |
||
49 | |||
50 | /*** |
||
51 | * @param string $key |
||
52 | * @param string $locale |
||
53 | * @param bool $useFallbackLocale |
||
54 | * |
||
55 | * @return mixed |
||
56 | */ |
||
57 | public function getTranslation(string $key, string $locale, bool $useFallbackLocale = true) |
||
58 | { |
||
59 | $locale = $this->normalizeLocale($key, $locale, $useFallbackLocale); |
||
60 | |||
61 | $translations = $this->getTranslations($key); |
||
62 | |||
63 | $translation = $translations[$locale] ?? ''; |
||
64 | |||
65 | if ($this->hasGetMutator($key)) { |
||
|
|||
66 | return $this->mutateAttribute($key, $translation); |
||
67 | } |
||
68 | |||
69 | return $translation; |
||
70 | } |
||
71 | |||
72 | public function getTranslationWithFallback(string $key, string $locale) |
||
73 | { |
||
74 | return $this->getTranslation($key, $locale, true); |
||
75 | } |
||
76 | |||
77 | public function getTranslationWithoutFallback(string $key, string $locale) |
||
78 | { |
||
79 | return $this->getTranslation($key, $locale, false); |
||
80 | } |
||
81 | |||
82 | public function getTranslations($key) : array |
||
83 | { |
||
84 | $this->guardAgainstUntranslatableAttribute($key); |
||
85 | |||
86 | return json_decode($this->getAttributes()[$key] ?? '' ?: '{}', true); |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * @param string $key |
||
91 | * @param string $locale |
||
92 | * @param $value |
||
93 | * |
||
94 | * @return $this |
||
95 | */ |
||
96 | public function setTranslation(string $key, string $locale, $value) |
||
97 | { |
||
98 | $this->guardAgainstUntranslatableAttribute($key); |
||
99 | |||
100 | $translations = $this->getTranslations($key); |
||
101 | |||
102 | $oldValue = $translations[$locale] ?? ''; |
||
103 | |||
104 | if ($this->hasSetMutator($key)) { |
||
105 | $method = 'set'.Str::studly($key).'Attribute'; |
||
106 | $value = $this->{$method}($value); |
||
107 | } |
||
108 | |||
109 | $translations[$locale] = $value; |
||
110 | |||
111 | $this->attributes[$key] = $this->asJson($translations); |
||
112 | |||
113 | event(new TranslationHasBeenSet($this, $key, $locale, $oldValue, $value)); |
||
114 | |||
115 | return $this; |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * @param string $key |
||
120 | * @param array $translations |
||
121 | * |
||
122 | * @return $this |
||
123 | */ |
||
124 | public function setTranslations(string $key, array $translations) |
||
125 | { |
||
126 | $this->guardAgainstUntranslatableAttribute($key); |
||
127 | |||
128 | foreach ($translations as $locale => $translation) { |
||
129 | $this->setTranslation($key, $locale, $translation); |
||
130 | } |
||
131 | |||
132 | return $this; |
||
133 | } |
||
134 | |||
135 | /** |
||
136 | * @param string $key |
||
137 | * @param string $locale |
||
138 | * |
||
139 | * @return $this |
||
140 | */ |
||
141 | public function forgetTranslation(string $key, string $locale) |
||
142 | { |
||
143 | $translations = $this->getTranslations($key); |
||
144 | |||
145 | unset($translations[$locale]); |
||
146 | |||
147 | $this->setAttribute($key, $translations); |
||
148 | |||
149 | return $this; |
||
150 | } |
||
151 | |||
152 | public function getTranslatedLocales(string $key) : array |
||
156 | |||
157 | public function isTranslatableAttribute(string $key) : bool |
||
158 | { |
||
159 | return in_array($key, $this->getTranslatableAttributes()); |
||
160 | } |
||
161 | |||
162 | protected function guardAgainstUntranslatableAttribute(string $key) |
||
163 | { |
||
164 | if (!$this->isTranslatableAttribute($key)) { |
||
165 | throw AttributeIsNotTranslatable::make($key, $this); |
||
166 | } |
||
168 | |||
169 | protected function normalizeLocale(string $key, string $locale, bool $useFallbackLocale) : string |
||
185 | |||
186 | public function getTranslatableAttributes() : array |
||
192 | |||
193 | public function getCasts() : array |
||
200 | } |
||
201 |
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.