| Total Complexity | 44 |
| Total Lines | 389 |
| Duplicated Lines | 0 % |
| Changes | 17 | ||
| Bugs | 1 | Features | 0 |
Complex classes like Translatable often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Translatable, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | trait Translatable |
||
| 15 | { |
||
| 16 | public static function bootTranslatable(): void |
||
| 17 | { |
||
| 18 | // Clean up translations |
||
| 19 | static::deleting(function (Model $model) { |
||
| 20 | in_array(SoftDeletes::class, class_uses_recursive($model)) |
||
| 21 | ? $model->clearTranslations($model->forceDeleting) |
||
| 22 | : $model->clearTranslations(true); |
||
| 23 | }); |
||
| 24 | |||
| 25 | if (in_array(SoftDeletes::class, class_uses_recursive(static::class))) { |
||
| 26 | static::restoring(function (Model $model) { |
||
| 27 | if (! Translator::softDeletes()) { |
||
| 28 | return; |
||
| 29 | } |
||
| 30 | |||
| 31 | $model->translations()->restore(); |
||
| 32 | }); |
||
| 33 | } |
||
| 34 | } |
||
| 35 | |||
| 36 | /** |
||
| 37 | * The associated translations relation. |
||
| 38 | * |
||
| 39 | * @return \Illuminate\Database\Eloquent\Relations\MorphMany |
||
| 40 | */ |
||
| 41 | public function translations(): MorphMany |
||
| 42 | { |
||
| 43 | return $this->morphMany(Translator::determineModel(), 'translatable'); |
||
|
|
|||
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Convert the model's attributes to an array. |
||
| 48 | * |
||
| 49 | * @return array |
||
| 50 | */ |
||
| 51 | public function attributesToArray() |
||
| 52 | { |
||
| 53 | $activeLangCode = Translator::activeLanguageCode(); |
||
| 54 | |||
| 55 | if (! Translator::autoTranslates()) { |
||
| 56 | return parent::attributesToArray(); |
||
| 57 | } |
||
| 58 | |||
| 59 | return array_merge( |
||
| 60 | parent::attributesToArray(), |
||
| 61 | $this->getTranslatedValues($activeLangCode) |
||
| 62 | ); |
||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Clear translations based on model deletion. |
||
| 67 | * |
||
| 68 | * @param bool $forceDelete |
||
| 69 | * @return void |
||
| 70 | */ |
||
| 71 | protected function clearTranslations($forceDelete = false): void |
||
| 72 | { |
||
| 73 | if (Translator::softDeletes() && $forceDelete) { |
||
| 74 | $this->translations()->forceDelete(); |
||
| 75 | |||
| 76 | return; |
||
| 77 | } |
||
| 78 | |||
| 79 | $this->translations()->delete(); |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Get a plain attribute (not a relationship). |
||
| 84 | * |
||
| 85 | * @param string $key |
||
| 86 | * @return mixed |
||
| 87 | */ |
||
| 88 | public function getAttributeValue($key) |
||
| 89 | { |
||
| 90 | $activeLangCode = Translator::activeLanguageCode(); |
||
| 91 | $value = parent::getAttributeValue($key); |
||
| 92 | |||
| 93 | if (! Translator::autoTranslates()) { |
||
| 94 | return $value; |
||
| 95 | } |
||
| 96 | |||
| 97 | if (Translator::isDefaultLanguage($activeLangCode)) { |
||
| 98 | return $value; |
||
| 99 | } |
||
| 100 | |||
| 101 | if (! $this->hasTranslation($activeLangCode, $key) || $this->isDirty($key)) { |
||
| 102 | return $value; |
||
| 103 | } |
||
| 104 | |||
| 105 | return $this->getTranslationValue($activeLangCode, $key); |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Returns the columns from the database. |
||
| 110 | * |
||
| 111 | * @return \Illuminate\Support\Collection |
||
| 112 | */ |
||
| 113 | public function getTableColumns(): Collection |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Get the translatable attributes. |
||
| 122 | * |
||
| 123 | * @return array |
||
| 124 | */ |
||
| 125 | public function getTranslatableAttributes(): array |
||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Get the translated values. |
||
| 136 | * |
||
| 137 | * @param string $langCode |
||
| 138 | * @return array |
||
| 139 | */ |
||
| 140 | public function getTranslatedValues(string $langCode): array |
||
| 141 | { |
||
| 142 | return collect($this->getTranslatableAttributes()) |
||
| 143 | ->filter(function ($attribute) use ($langCode) { |
||
| 144 | return $this->hasTranslation($langCode, $attribute); |
||
| 145 | }) |
||
| 146 | ->values() |
||
| 147 | ->mapWithKeys(function ($attribute) use ($langCode) { |
||
| 148 | return [$attribute => $this->getTranslationValue($langCode, $attribute)]; |
||
| 149 | }) |
||
| 150 | ->toArray(); |
||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Get the translation value for a given key. |
||
| 155 | * |
||
| 156 | * @param string $attribute |
||
| 157 | * @return mixed |
||
| 158 | */ |
||
| 159 | public function getTranslationValue(string $langCode, string $attribute) |
||
| 160 | { |
||
| 161 | $translation = $this->translations->where('key', $attribute) |
||
| 162 | ->where('language_code', $langCode) |
||
| 163 | ->first(); |
||
| 164 | |||
| 165 | if ($translation === null) { |
||
| 166 | return; |
||
| 167 | } |
||
| 168 | |||
| 169 | if ($this->hasCast($attribute)) { |
||
| 170 | return $this->castAttribute($attribute, $translation->value); |
||
| 171 | } |
||
| 172 | |||
| 173 | return $translation->value; |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Get the updatable attributes. |
||
| 178 | * |
||
| 179 | * @return array |
||
| 180 | */ |
||
| 181 | public function getUpdatableAttributes(): array |
||
| 182 | { |
||
| 183 | $fillable = collect($this->getFillable()); |
||
| 184 | |||
| 185 | if ($fillable->isEmpty()) { |
||
| 186 | $fillable = $this->getTableColumns(); |
||
| 187 | } |
||
| 188 | |||
| 189 | return $fillable->diff($this->getTranslatableAttributes()) |
||
| 190 | ->values() |
||
| 191 | ->toArray(); |
||
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Check if a translation exists for a given attribute. |
||
| 196 | * |
||
| 197 | * @param string $langCode |
||
| 198 | * @param string $attribute |
||
| 199 | * @return bool |
||
| 200 | */ |
||
| 201 | public function hasTranslation(string $langCode, string $attribute): bool |
||
| 202 | { |
||
| 203 | if (! $this->shouldBeTranslated($attribute)) { |
||
| 204 | return false; |
||
| 205 | } |
||
| 206 | |||
| 207 | if ($this->relationLoaded('translations')) { |
||
| 208 | return $this->translations->where('key', $attribute) |
||
| 209 | ->where('language_code', $langCode) |
||
| 210 | ->count() > 0; |
||
| 211 | } |
||
| 212 | |||
| 213 | return $this->translations()->where('key', $attribute) |
||
| 214 | ->forLang($langCode) |
||
| 215 | ->count() > 0; |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Check if a translation is outdated. |
||
| 220 | * |
||
| 221 | * @param string $langCode |
||
| 222 | * @param string $attribute |
||
| 223 | * @return bool |
||
| 224 | */ |
||
| 225 | public function isTranslationOutdated(string $langCode, string $attribute): bool |
||
| 226 | { |
||
| 227 | if (Translator::isDefaultLanguage($langCode)) { |
||
| 228 | return false; |
||
| 229 | } |
||
| 230 | |||
| 231 | $defaultTranslation = $this->translations->where('key', $attribute) |
||
| 232 | ->where('language_code', Translator::defaultLanguageCode()) |
||
| 233 | ->first(); |
||
| 234 | |||
| 235 | if (! $defaultTranslation) { |
||
| 236 | return false; |
||
| 237 | } |
||
| 238 | |||
| 239 | $targetTranslation = $this->translations->where('key', $attribute) |
||
| 240 | ->where('language_code', $langCode) |
||
| 241 | ->first(); |
||
| 242 | |||
| 243 | if (! $targetTranslation) { |
||
| 244 | return false; |
||
| 245 | } |
||
| 246 | |||
| 247 | // Compare target translation to default translation |
||
| 248 | return $targetTranslation->updated_at->lt($defaultTranslation->updated_at); |
||
| 249 | } |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Checks if the given attribute should be translated. |
||
| 253 | * |
||
| 254 | * @param string $attribute |
||
| 255 | * @return bool |
||
| 256 | */ |
||
| 257 | public function shouldBeTranslated(string $attribute): bool |
||
| 258 | { |
||
| 259 | return in_array($attribute, $this->getTranslatableAttributes()); |
||
| 260 | } |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Set the translatable attributes for the model. |
||
| 264 | * |
||
| 265 | * @param array $attributes |
||
| 266 | * @return self |
||
| 267 | */ |
||
| 268 | public function translatable(array $attributes): self |
||
| 269 | { |
||
| 270 | $this->translatable = $attributes; |
||
| 271 | |||
| 272 | return $this; |
||
| 273 | } |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Translate an array of attribute, value pairs. |
||
| 277 | * |
||
| 278 | * @param string $langCode |
||
| 279 | * @param array $data |
||
| 280 | * @return \Illuminate\Support\Collection |
||
| 281 | */ |
||
| 282 | public function translate(string $langCode, array $data): Collection |
||
| 283 | { |
||
| 284 | return collect($data) |
||
| 285 | ->filter(function ($value, $attribute) { |
||
| 286 | return $this->shouldBeTranslated($attribute); |
||
| 287 | }) |
||
| 288 | ->map(function ($value, $attribute) use ($langCode) { |
||
| 289 | return $this->translateAttribute($langCode, $attribute, $value); |
||
| 290 | }) |
||
| 291 | ->filter() |
||
| 292 | ->values(); |
||
| 293 | } |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Translate the specified attribute, value pair. |
||
| 297 | * |
||
| 298 | * @param string $langCode |
||
| 299 | * @param string $attribute |
||
| 300 | * @param mixed $value |
||
| 301 | * @return \Signifly\Translator\Contracts\Translation|null |
||
| 302 | */ |
||
| 303 | public function translateAttribute(string $langCode, string $attribute, $value): ?Translation |
||
| 304 | { |
||
| 305 | $translation = $this->translations()->firstOrNew([ |
||
| 306 | 'language_code' => $langCode, |
||
| 307 | 'key' => $attribute, |
||
| 308 | ]); |
||
| 309 | |||
| 310 | // If the value provided for translation is empty |
||
| 311 | // then delete the translation and return |
||
| 312 | if (! is_bool($value) && ! is_array($value) && trim((string) $value) === '') { |
||
| 313 | $translation->delete(); |
||
| 314 | |||
| 315 | return null; |
||
| 316 | } |
||
| 317 | |||
| 318 | if ($this->isJsonCastable($attribute)) { |
||
| 319 | $value = $this->castAttributeAsJson($attribute, $value); |
||
| 320 | } |
||
| 321 | |||
| 322 | $translation->fill(compact('value'))->save(); |
||
| 323 | |||
| 324 | return $translation; |
||
| 325 | } |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Create and translate for the specified language code. |
||
| 329 | * |
||
| 330 | * @param string $langCode |
||
| 331 | * @param array $data |
||
| 332 | * @return \Illuminate\Database\Eloquent\Model |
||
| 333 | */ |
||
| 334 | public static function createAndTranslate(string $langCode, array $data): Model |
||
| 335 | { |
||
| 336 | $model = self::create($data); |
||
| 337 | $model->translate($langCode, $data); |
||
| 338 | |||
| 339 | return $model; |
||
| 340 | } |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Update and translate for the specified language code. |
||
| 344 | * |
||
| 345 | * @param string $langCode |
||
| 346 | * @param array $data |
||
| 347 | * @return \Illuminate\Database\Eloquent\Model |
||
| 348 | */ |
||
| 349 | public function updateAndTranslate(string $langCode, array $data): Model |
||
| 364 | } |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Scope a query to include translation stats. |
||
| 368 | * |
||
| 369 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 370 | * @param string $langCode |
||
| 371 | * @return \Illuminate\Database\Eloquent\Builder |
||
| 372 | */ |
||
| 373 | public function scopeWithTranslationStats(Builder $query, string $langCode): Builder |
||
| 403 | ]); |
||
| 404 | } |
||
| 405 | } |
||
| 406 |