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 | * @return $this |
||
31 | */ |
||
32 | public function setAttribute($key, $value) |
||
33 | { |
||
34 | // pass arrays and untranslatable attributes to the parent method |
||
35 | if (!$this->isTranslatableAttribute($key) or is_array($value)) { |
||
|
|||
36 | return parent::setAttribute($key, $value); |
||
37 | } |
||
38 | // if the attribute is translatable and not already translated (=array), |
||
39 | // set a translation for the current app locale |
||
40 | return $this->setTranslation($key, config('app.locale'), $value); |
||
41 | } |
||
42 | |||
43 | /** |
||
44 | * @param string $key |
||
45 | * @param string $locale |
||
46 | * |
||
47 | * @return mixed |
||
48 | */ |
||
49 | public function translate(string $key, string $locale = '') |
||
53 | |||
54 | /*** |
||
55 | * @param string $key |
||
56 | * @param string $locale |
||
57 | * @param bool $useFallbackLocale |
||
58 | * |
||
59 | * @return mixed |
||
60 | */ |
||
61 | public function getTranslation(string $key, string $locale, bool $useFallbackLocale = true) |
||
62 | { |
||
63 | $locale = $this->normalizeLocale($key, $locale, $useFallbackLocale); |
||
64 | |||
65 | $translations = $this->getTranslations($key); |
||
66 | |||
67 | $translation = $translations[$locale] ?? ''; |
||
68 | |||
69 | if ($this->hasGetMutator($key)) { |
||
70 | return $this->mutateAttribute($key, $translation); |
||
71 | } |
||
72 | |||
73 | return $translation; |
||
74 | } |
||
75 | |||
76 | public function getTranslationWithFallback(string $key, string $locale) |
||
77 | { |
||
78 | return $this->getTranslation($key, $locale, true); |
||
79 | } |
||
80 | |||
81 | public function getTranslationWithoutFallback(string $key, string $locale) |
||
82 | { |
||
83 | return $this->getTranslation($key, $locale, false); |
||
84 | } |
||
85 | |||
86 | public function getTranslations($key) : array |
||
87 | { |
||
88 | $this->guardAgainstUntranslatableAttribute($key); |
||
89 | |||
90 | return json_decode($this->getAttributes()[$key] ?? '' ?: '{}', true); |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * @param string $key |
||
95 | * @param string $locale |
||
96 | * @param $value |
||
97 | * |
||
98 | * @return $this |
||
99 | */ |
||
100 | public function setTranslation(string $key, string $locale, $value) |
||
101 | { |
||
102 | $this->guardAgainstUntranslatableAttribute($key); |
||
103 | |||
104 | $translations = $this->getTranslations($key); |
||
105 | |||
106 | $oldValue = $translations[$locale] ?? ''; |
||
107 | |||
108 | if ($this->hasSetMutator($key)) { |
||
109 | $method = 'set'.Str::studly($key).'Attribute'; |
||
110 | $value = $this->{$method}($value); |
||
111 | } |
||
112 | |||
113 | $translations[$locale] = $value; |
||
114 | |||
115 | $this->attributes[$key] = $this->asJson($translations); |
||
116 | |||
117 | event(new TranslationHasBeenSet($this, $key, $locale, $oldValue, $value)); |
||
118 | |||
119 | return $this; |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * @param string $key |
||
124 | * @param array $translations |
||
125 | * |
||
126 | * @return $this |
||
127 | */ |
||
128 | public function setTranslations(string $key, array $translations) |
||
129 | { |
||
130 | $this->guardAgainstUntranslatableAttribute($key); |
||
131 | |||
132 | foreach ($translations as $locale => $translation) { |
||
133 | $this->setTranslation($key, $locale, $translation); |
||
134 | } |
||
135 | |||
136 | return $this; |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * @param string $key |
||
141 | * @param string $locale |
||
142 | * |
||
143 | * @return $this |
||
144 | */ |
||
145 | public function forgetTranslation(string $key, string $locale) |
||
146 | { |
||
147 | $translations = $this->getTranslations($key); |
||
148 | |||
149 | unset($translations[$locale]); |
||
150 | |||
151 | $this->setAttribute($key, $translations); |
||
152 | |||
153 | return $this; |
||
154 | } |
||
155 | |||
156 | public function forgetAllTranslations(string $locale) |
||
157 | { |
||
158 | collect($this->getTranslatableAttributes())->each(function (string $attribute) use ($locale) { |
||
159 | $this->forgetTranslation($attribute, $locale); |
||
160 | }); |
||
161 | } |
||
162 | |||
163 | public function getTranslatedLocales(string $key) : array |
||
167 | |||
168 | public function isTranslatableAttribute(string $key) : bool |
||
172 | |||
173 | protected function guardAgainstUntranslatableAttribute(string $key) |
||
179 | |||
180 | protected function normalizeLocale(string $key, string $locale, bool $useFallbackLocale) : string |
||
196 | |||
197 | public function getTranslatableAttributes() : array |
||
203 | |||
204 | public function getCasts() : array |
||
211 | } |
||
212 |
PHP has two types of connecting operators (logical operators, and boolean operators):
and
&&
or
||
The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like
&&
, or||
.Let’s take a look at a few examples:
Logical Operators are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
Since
die
introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined withthrow
at this point:These limitations lead to logical operators rarely being of use in current PHP code.