Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 |
||
| 12 | trait Translatable |
||
| 13 | { |
||
| 14 | protected $defaultLocale; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Alias for getTranslation(). |
||
| 18 | * |
||
| 19 | * @param string|null $locale |
||
| 20 | * @param bool $withFallback |
||
| 21 | * |
||
| 22 | * @return \Illuminate\Database\Eloquent\Model|null |
||
| 23 | */ |
||
| 24 | public function translate($locale = null, $withFallback = false) |
||
| 25 | { |
||
| 26 | return $this->getTranslation($locale, $withFallback); |
||
| 27 | } |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Alias for getTranslation(). |
||
| 31 | * |
||
| 32 | * @param string $locale |
||
| 33 | * |
||
| 34 | * @return \Illuminate\Database\Eloquent\Model|null |
||
| 35 | */ |
||
| 36 | public function translateOrDefault($locale) |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Alias for getTranslationOrNew(). |
||
| 43 | * |
||
| 44 | * @param string $locale |
||
| 45 | * |
||
| 46 | * @return \Illuminate\Database\Eloquent\Model|null |
||
| 47 | */ |
||
| 48 | public function translateOrNew($locale) |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @param string|null $locale |
||
| 55 | * @param bool $withFallback |
||
| 56 | * |
||
| 57 | * @return \Illuminate\Database\Eloquent\Model|null |
||
| 58 | */ |
||
| 59 | public function getTranslation($locale = null, $withFallback = null) |
||
| 60 | { |
||
| 61 | $configFallbackLocale = $this->getFallbackLocale(); |
||
| 62 | $locale = $locale ?: $this->locale(); |
||
| 63 | $withFallback = $withFallback === null ? $this->useFallback() : $withFallback; |
||
| 64 | $fallbackLocale = $this->getFallbackLocale($locale); |
||
| 65 | |||
| 66 | if ($translation = $this->getTranslationByLocaleKey($locale)) { |
||
| 67 | return $translation; |
||
| 68 | } |
||
| 69 | if ($withFallback && $fallbackLocale) { |
||
| 70 | if ($translation = $this->getTranslationByLocaleKey($fallbackLocale)) { |
||
| 71 | return $translation; |
||
| 72 | } |
||
| 73 | if ($translation = $this->getTranslationByLocaleKey($configFallbackLocale)) { |
||
| 74 | return $translation; |
||
| 75 | } |
||
| 76 | } |
||
| 77 | |||
| 78 | return null; |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @param string|null $locale |
||
| 83 | * |
||
| 84 | * @return bool |
||
| 85 | */ |
||
| 86 | public function hasTranslation($locale = null) |
||
| 87 | { |
||
| 88 | $locale = $locale ?: $this->locale(); |
||
| 89 | |||
| 90 | foreach ($this->translations as $translation) { |
||
|
|
|||
| 91 | if ($translation->getAttribute($this->getLocaleKey()) == $locale) { |
||
| 92 | return true; |
||
| 93 | } |
||
| 94 | } |
||
| 95 | |||
| 96 | return false; |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @return string |
||
| 101 | */ |
||
| 102 | public function getTranslationModelName() |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @return string |
||
| 109 | */ |
||
| 110 | public function getTranslationModelNameDefault() |
||
| 111 | { |
||
| 112 | $config = app()->make('config'); |
||
| 113 | |||
| 114 | return get_class($this).$config->get('translatable.translation_suffix', 'Translation'); |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @return string |
||
| 119 | */ |
||
| 120 | public function getRelationKey() |
||
| 121 | { |
||
| 122 | if ($this->translationForeignKey) { |
||
| 123 | $key = $this->translationForeignKey; |
||
| 124 | } elseif ($this->primaryKey !== 'id') { |
||
| 125 | $key = $this->primaryKey; |
||
| 126 | } else { |
||
| 127 | $key = $this->getForeignKey(); |
||
| 128 | } |
||
| 129 | |||
| 130 | return $key; |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @return string |
||
| 135 | */ |
||
| 136 | public function getLocaleKey() |
||
| 137 | { |
||
| 138 | $config = app()->make('config'); |
||
| 139 | |||
| 140 | return $this->localeKey ?: $config->get('translatable.locale_key', 'locale'); |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @return \Illuminate\Database\Eloquent\Relations\HasMany |
||
| 145 | */ |
||
| 146 | public function translations() |
||
| 150 | |||
| 151 | /** |
||
| 152 | * @return bool |
||
| 153 | */ |
||
| 154 | private function usePropertyFallback() |
||
| 155 | { |
||
| 156 | return app()->make('config')->get('translatable.use_property_fallback', false); |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Returns the attribute value from fallback translation if value of attribute |
||
| 161 | * is empty and the property fallback is enabled in the configuration. |
||
| 162 | * in model. |
||
| 163 | * @param $locale |
||
| 164 | * @param $attribute |
||
| 165 | * @return mixed |
||
| 166 | */ |
||
| 167 | private function getAttributeOrFallback($locale, $attribute) |
||
| 168 | { |
||
| 169 | $usePropertyFallback = $this->useFallback() && $this->usePropertyFallback(); |
||
| 170 | if (!$this->hasTranslation($locale) && $usePropertyFallback) { |
||
| 171 | return $this->getTranslation($this->getFallbackLocale(), true)->$attribute; |
||
| 172 | } |
||
| 173 | |||
| 174 | return $this->getTranslation($locale)->$attribute; |
||
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @param string $key |
||
| 179 | * |
||
| 180 | * @return mixed |
||
| 181 | */ |
||
| 182 | public function getAttribute($key) |
||
| 183 | { |
||
| 184 | list($attribute, $locale) = $this->getAttributeAndLocale($key); |
||
| 185 | |||
| 186 | if ($this->isTranslationAttribute($attribute)) { |
||
| 187 | if ($this->getTranslation($locale) === null) { |
||
| 188 | return null; |
||
| 189 | } |
||
| 190 | |||
| 191 | // If the given $attribute has a mutator, we push it to $attributes and then call getAttributeValue |
||
| 192 | // on it. This way, we can use Eloquent's checking for Mutation, type casting, and |
||
| 193 | // Date fields. |
||
| 194 | if ($this->hasGetMutator($attribute)) { |
||
| 195 | $this->attributes[$attribute] = $this->getAttributeOrFallback($locale, $attribute); |
||
| 196 | |||
| 197 | return $this->getAttributeValue($attribute); |
||
| 198 | } |
||
| 199 | |||
| 200 | return $this->getAttributeOrFallback($locale, $attribute); |
||
| 201 | } |
||
| 202 | |||
| 203 | return parent::getAttribute($key); |
||
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * @param string $key |
||
| 208 | * @param mixed $value |
||
| 209 | * |
||
| 210 | * @return $this |
||
| 211 | */ |
||
| 212 | public function setAttribute($key, $value) |
||
| 213 | { |
||
| 214 | list($attribute, $locale) = $this->getAttributeAndLocale($key); |
||
| 215 | |||
| 216 | if ($this->isTranslationAttribute($attribute)) { |
||
| 217 | $this->getTranslationOrNew($locale)->$attribute = $value; |
||
| 218 | } else { |
||
| 219 | return parent::setAttribute($key, $value); |
||
| 220 | } |
||
| 221 | |||
| 222 | return $this; |
||
| 223 | } |
||
| 224 | |||
| 225 | /** |
||
| 226 | * @param array $options |
||
| 227 | * |
||
| 228 | * @return bool |
||
| 229 | */ |
||
| 230 | public function save(array $options = []) |
||
| 231 | { |
||
| 232 | if ($this->exists) { |
||
| 233 | if (count($this->getDirty()) > 0) { |
||
| 234 | // If $this->exists and dirty, parent::save() has to return true. If not, |
||
| 235 | // an error has occurred. Therefore we shouldn't save the translations. |
||
| 236 | if (parent::save($options)) { |
||
| 237 | return $this->saveTranslations(); |
||
| 238 | } |
||
| 239 | |||
| 240 | return false; |
||
| 241 | } else { |
||
| 242 | // If $this->exists and not dirty, parent::save() skips saving and returns |
||
| 243 | // false. So we have to save the translations |
||
| 244 | if ($saved = $this->saveTranslations()) { |
||
| 245 | $this->fireModelEvent('saved', false); |
||
| 246 | $this->fireModelEvent('updated', false); |
||
| 247 | } |
||
| 248 | |||
| 249 | return $saved; |
||
| 250 | } |
||
| 251 | } elseif (parent::save($options)) { |
||
| 252 | // We save the translations only if the instance is saved in the database. |
||
| 253 | return $this->saveTranslations(); |
||
| 254 | } |
||
| 255 | |||
| 256 | return false; |
||
| 257 | } |
||
| 258 | |||
| 259 | /** |
||
| 260 | * @param string $locale |
||
| 261 | * |
||
| 262 | * @return \Illuminate\Database\Eloquent\Model|null |
||
| 263 | */ |
||
| 264 | protected function getTranslationOrNew($locale) |
||
| 265 | { |
||
| 266 | if (($translation = $this->getTranslation($locale, false)) === null) { |
||
| 267 | $translation = $this->getNewTranslation($locale); |
||
| 268 | } |
||
| 269 | |||
| 270 | return $translation; |
||
| 271 | } |
||
| 272 | |||
| 273 | /** |
||
| 274 | * @param array $attributes |
||
| 275 | * |
||
| 276 | * @throws \Illuminate\Database\Eloquent\MassAssignmentException |
||
| 277 | * @return $this |
||
| 278 | */ |
||
| 279 | public function fill(array $attributes) |
||
| 280 | { |
||
| 281 | foreach ($attributes as $key => $values) { |
||
| 282 | if ($this->isKeyALocale($key)) { |
||
| 283 | $this->getTranslationOrNew($key)->fill($values); |
||
| 284 | unset($attributes[$key]); |
||
| 285 | } else { |
||
| 286 | list($attribute, $locale) = $this->getAttributeAndLocale($key); |
||
| 287 | if ($this->isTranslationAttribute($attribute) and $this->isKeyALocale($locale)) { |
||
| 288 | $this->getTranslationOrNew($locale)->fill([$attribute => $values]); |
||
| 289 | unset($attributes[$key]); |
||
| 290 | } |
||
| 291 | } |
||
| 292 | } |
||
| 293 | |||
| 294 | return parent::fill($attributes); |
||
| 295 | } |
||
| 296 | |||
| 297 | /** |
||
| 298 | * @param string $key |
||
| 299 | */ |
||
| 300 | private function getTranslationByLocaleKey($key) |
||
| 301 | { |
||
| 302 | foreach ($this->translations as $translation) { |
||
| 303 | if ($translation->getAttribute($this->getLocaleKey()) == $key) { |
||
| 304 | return $translation; |
||
| 305 | } |
||
| 306 | } |
||
| 307 | |||
| 308 | return null; |
||
| 309 | } |
||
| 310 | |||
| 311 | /** |
||
| 312 | * @param null $locale |
||
| 313 | * |
||
| 314 | * @return string |
||
| 315 | */ |
||
| 316 | private function getFallbackLocale($locale = null) |
||
| 317 | { |
||
| 318 | if ($locale && $this->isLocaleCountryBased($locale)) { |
||
| 319 | if ($fallback = $this->getLanguageFromCountryBasedLocale($locale)) { |
||
| 320 | return $fallback; |
||
| 321 | } |
||
| 322 | } |
||
| 323 | |||
| 324 | return app()->make('config')->get('translatable.fallback_locale'); |
||
| 325 | } |
||
| 326 | |||
| 327 | /** |
||
| 328 | * @param $locale |
||
| 329 | * |
||
| 330 | * @return bool |
||
| 331 | */ |
||
| 332 | private function isLocaleCountryBased($locale) |
||
| 333 | { |
||
| 334 | return strpos($locale, $this->getLocaleSeparator()) !== false; |
||
| 335 | } |
||
| 336 | |||
| 337 | /** |
||
| 338 | * @param $locale |
||
| 339 | * |
||
| 340 | * @return string |
||
| 341 | */ |
||
| 342 | private function getLanguageFromCountryBasedLocale($locale) |
||
| 343 | { |
||
| 344 | $parts = explode($this->getLocaleSeparator(), $locale); |
||
| 345 | |||
| 346 | return array_get($parts, 0); |
||
| 347 | } |
||
| 348 | |||
| 349 | /** |
||
| 350 | * @return bool|null |
||
| 351 | */ |
||
| 352 | private function useFallback() |
||
| 353 | { |
||
| 354 | if (isset($this->useTranslationFallback) && $this->useTranslationFallback !== null) { |
||
| 355 | return $this->useTranslationFallback; |
||
| 356 | } |
||
| 357 | |||
| 358 | return app()->make('config')->get('translatable.use_fallback'); |
||
| 359 | } |
||
| 360 | |||
| 361 | /** |
||
| 362 | * @param string $key |
||
| 363 | * |
||
| 364 | * @return bool |
||
| 365 | */ |
||
| 366 | public function isTranslationAttribute($key) |
||
| 367 | { |
||
| 368 | return in_array($key, $this->translatedAttributes); |
||
| 369 | } |
||
| 370 | |||
| 371 | /** |
||
| 372 | * @param string $key |
||
| 373 | * |
||
| 374 | * @throws \Dimsav\Translatable\Exception\LocalesNotDefinedException |
||
| 375 | * @return bool |
||
| 376 | */ |
||
| 377 | protected function isKeyALocale($key) |
||
| 378 | { |
||
| 379 | $locales = $this->getLocales(); |
||
| 380 | |||
| 381 | return in_array($key, $locales); |
||
| 382 | } |
||
| 383 | |||
| 384 | /** |
||
| 385 | * @throws \Dimsav\Translatable\Exception\LocalesNotDefinedException |
||
| 386 | * @return array |
||
| 387 | */ |
||
| 388 | protected function getLocales() |
||
| 389 | { |
||
| 390 | $localesConfig = (array) app()->make('config')->get('translatable.locales'); |
||
| 391 | |||
| 392 | if (empty($localesConfig)) { |
||
| 393 | throw new LocalesNotDefinedException('Please make sure you have run "php artisan config:publish dimsav/laravel-translatable" '. |
||
| 394 | ' and that the locales configuration is defined.'); |
||
| 395 | } |
||
| 396 | |||
| 397 | $locales = []; |
||
| 398 | foreach ($localesConfig as $key => $locale) { |
||
| 399 | if (is_array($locale)) { |
||
| 400 | $locales[] = $key; |
||
| 401 | foreach ($locale as $countryLocale) { |
||
| 402 | $locales[] = $key.$this->getLocaleSeparator().$countryLocale; |
||
| 403 | } |
||
| 404 | } else { |
||
| 405 | $locales[] = $locale; |
||
| 406 | } |
||
| 407 | } |
||
| 408 | |||
| 409 | return $locales; |
||
| 410 | } |
||
| 411 | |||
| 412 | /** |
||
| 413 | * @return string |
||
| 414 | */ |
||
| 415 | protected function getLocaleSeparator() |
||
| 416 | { |
||
| 417 | return app()->make('config')->get('translatable.locale_separator', '-'); |
||
| 418 | } |
||
| 419 | |||
| 420 | /** |
||
| 421 | * @return bool |
||
| 422 | */ |
||
| 423 | protected function saveTranslations() |
||
| 424 | { |
||
| 425 | $saved = true; |
||
| 426 | foreach ($this->translations as $translation) { |
||
| 427 | if ($saved && $this->isTranslationDirty($translation)) { |
||
| 428 | if (! empty($connectionName = $this->getConnectionName())) { |
||
| 429 | $translation->setConnection($connectionName); |
||
| 430 | } |
||
| 431 | |||
| 432 | $translation->setAttribute($this->getRelationKey(), $this->getKey()); |
||
| 433 | $saved = $translation->save(); |
||
| 434 | } |
||
| 435 | } |
||
| 436 | |||
| 437 | return $saved; |
||
| 438 | } |
||
| 439 | |||
| 440 | /** |
||
| 441 | * @param array |
||
| 442 | * |
||
| 443 | * @return \Illuminate\Database\Eloquent\Model |
||
| 444 | */ |
||
| 445 | public function replicateWithTranslations(array $except = null) |
||
| 446 | { |
||
| 447 | $newInstance = parent::replicate($except); |
||
| 448 | |||
| 449 | unset($newInstance->translations); |
||
| 450 | foreach ($this->translations as $translation) { |
||
| 451 | $newTranslation = $translation->replicate(); |
||
| 452 | $newInstance->translations->add($newTranslation); |
||
| 453 | } |
||
| 454 | |||
| 455 | return $newInstance; |
||
| 456 | } |
||
| 457 | |||
| 458 | /** |
||
| 459 | * @param \Illuminate\Database\Eloquent\Model $translation |
||
| 460 | * |
||
| 461 | * @return bool |
||
| 462 | */ |
||
| 463 | protected function isTranslationDirty(Model $translation) |
||
| 464 | { |
||
| 465 | $dirtyAttributes = $translation->getDirty(); |
||
| 466 | unset($dirtyAttributes[$this->getLocaleKey()]); |
||
| 467 | |||
| 468 | return count($dirtyAttributes) > 0; |
||
| 469 | } |
||
| 470 | |||
| 471 | /** |
||
| 472 | * @param string $locale |
||
| 473 | * |
||
| 474 | * @return \Illuminate\Database\Eloquent\Model |
||
| 475 | */ |
||
| 476 | public function getNewTranslation($locale) |
||
| 477 | { |
||
| 478 | $modelName = $this->getTranslationModelName(); |
||
| 479 | $translation = new $modelName(); |
||
| 480 | $translation->setAttribute($this->getLocaleKey(), $locale); |
||
| 481 | $this->translations->add($translation); |
||
| 482 | |||
| 483 | return $translation; |
||
| 484 | } |
||
| 485 | |||
| 486 | /** |
||
| 487 | * @param $key |
||
| 488 | * |
||
| 489 | * @return bool |
||
| 490 | */ |
||
| 491 | public function __isset($key) |
||
| 492 | { |
||
| 493 | return $this->isTranslationAttribute($key) || parent::__isset($key); |
||
| 494 | } |
||
| 495 | |||
| 496 | /** |
||
| 497 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 498 | * @param string $locale |
||
| 499 | * |
||
| 500 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
| 501 | */ |
||
| 502 | View Code Duplication | public function scopeTranslatedIn(Builder $query, $locale = null) |
|
| 503 | { |
||
| 504 | $locale = $locale ?: $this->locale(); |
||
| 505 | |||
| 506 | return $query->whereHas('translations', function (Builder $q) use ($locale) { |
||
| 507 | $q->where($this->getLocaleKey(), '=', $locale); |
||
| 508 | }); |
||
| 509 | } |
||
| 510 | |||
| 511 | /** |
||
| 512 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 513 | * @param string $locale |
||
| 514 | * |
||
| 515 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
| 516 | */ |
||
| 517 | View Code Duplication | public function scopeNotTranslatedIn(Builder $query, $locale = null) |
|
| 518 | { |
||
| 519 | $locale = $locale ?: $this->locale(); |
||
| 520 | |||
| 521 | return $query->whereDoesntHave('translations', function (Builder $q) use ($locale) { |
||
| 522 | $q->where($this->getLocaleKey(), '=', $locale); |
||
| 523 | }); |
||
| 524 | } |
||
| 525 | |||
| 526 | /** |
||
| 527 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 528 | * |
||
| 529 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
| 530 | */ |
||
| 531 | public function scopeTranslated(Builder $query) |
||
| 532 | { |
||
| 533 | return $query->has('translations'); |
||
| 534 | } |
||
| 535 | |||
| 536 | /** |
||
| 537 | * Adds scope to get a list of translated attributes, using the current locale. |
||
| 538 | * Example usage: Country::listsTranslations('name')->get()->toArray() |
||
| 539 | * Will return an array with items: |
||
| 540 | * [ |
||
| 541 | * 'id' => '1', // The id of country |
||
| 542 | * 'name' => 'Griechenland' // The translated name |
||
| 543 | * ]. |
||
| 544 | * |
||
| 545 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 546 | * @param string $translationField |
||
| 547 | */ |
||
| 548 | public function scopeListsTranslations(Builder $query, $translationField) |
||
| 549 | { |
||
| 550 | $withFallback = $this->useFallback(); |
||
| 551 | $translationTable = $this->getTranslationsTable(); |
||
| 552 | $localeKey = $this->getLocaleKey(); |
||
| 553 | |||
| 554 | $query |
||
| 555 | ->select($this->getTable().'.'.$this->getKeyName(), $translationTable.'.'.$translationField) |
||
| 556 | ->leftJoin($translationTable, $translationTable.'.'.$this->getRelationKey(), '=', $this->getTable().'.'.$this->getKeyName()) |
||
| 557 | ->where($translationTable.'.'.$localeKey, $this->locale()); |
||
| 558 | if ($withFallback) { |
||
| 559 | $query->orWhere(function (Builder $q) use ($translationTable, $localeKey) { |
||
| 560 | $q->where($translationTable.'.'.$localeKey, $this->getFallbackLocale()) |
||
| 561 | ->whereNotIn($translationTable.'.'.$this->getRelationKey(), function (QueryBuilder $q) use ( |
||
| 562 | $translationTable, |
||
| 563 | $localeKey |
||
| 564 | ) { |
||
| 565 | $q->select($translationTable.'.'.$this->getRelationKey()) |
||
| 566 | ->from($translationTable) |
||
| 567 | ->where($translationTable.'.'.$localeKey, $this->locale()); |
||
| 568 | }); |
||
| 569 | }); |
||
| 570 | } |
||
| 571 | } |
||
| 572 | |||
| 573 | /** |
||
| 574 | * This scope eager loads the translations for the default and the fallback locale only. |
||
| 575 | * We can use this as a shortcut to improve performance in our application. |
||
| 576 | * |
||
| 577 | * @param Builder $query |
||
| 578 | */ |
||
| 579 | public function scopeWithTranslation(Builder $query) |
||
| 580 | { |
||
| 581 | $query->with([ |
||
| 582 | 'translations' => function (Relation $query) { |
||
| 583 | $query->where($this->getTranslationsTable().'.'.$this->getLocaleKey(), $this->locale()); |
||
| 584 | |||
| 585 | if ($this->useFallback()) { |
||
| 586 | return $query->orWhere($this->getTranslationsTable().'.'.$this->getLocaleKey(), $this->getFallbackLocale()); |
||
| 587 | } |
||
| 588 | }, |
||
| 589 | ]); |
||
| 590 | } |
||
| 591 | |||
| 592 | /** |
||
| 593 | * This scope filters results by checking the translation fields. |
||
| 594 | * |
||
| 595 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 596 | * @param string $key |
||
| 597 | * @param string $value |
||
| 598 | * @param string $locale |
||
| 599 | * |
||
| 600 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
| 601 | */ |
||
| 602 | View Code Duplication | public function scopeWhereTranslation(Builder $query, $key, $value, $locale = null) |
|
| 603 | { |
||
| 604 | return $query->whereHas('translations', function (Builder $query) use ($key, $value, $locale) { |
||
| 605 | $query->where($this->getTranslationsTable().'.'.$key, $value); |
||
| 606 | if ($locale) { |
||
| 607 | $query->where($this->getTranslationsTable().'.'.$this->getLocaleKey(), $locale); |
||
| 608 | } |
||
| 609 | }); |
||
| 610 | } |
||
| 611 | |||
| 612 | /** |
||
| 613 | * This scope filters results by checking the translation fields. |
||
| 614 | * |
||
| 615 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 616 | * @param string $key |
||
| 617 | * @param string $value |
||
| 618 | * @param string $locale |
||
| 619 | * |
||
| 620 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
| 621 | */ |
||
| 622 | public function scopeOrWhereTranslation(Builder $query, $key, $value, $locale = null) |
||
| 623 | { |
||
| 624 | return $query->orWhereHas('translations', function (Builder $query) use ($key, $value, $locale) { |
||
| 625 | $query->where($this->getTranslationsTable().'.'.$key, $value); |
||
| 626 | if ($locale) { |
||
| 627 | $query->where($this->getTranslationsTable().'.'.$this->getLocaleKey(), $locale); |
||
| 628 | } |
||
| 629 | }); |
||
| 630 | } |
||
| 631 | |||
| 632 | /** |
||
| 633 | * This scope filters results by checking the translation fields. |
||
| 634 | * |
||
| 635 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 636 | * @param string $key |
||
| 637 | * @param string $value |
||
| 638 | * @param string $locale |
||
| 639 | * |
||
| 640 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
| 641 | */ |
||
| 642 | View Code Duplication | public function scopeWhereTranslationLike(Builder $query, $key, $value, $locale = null) |
|
| 643 | { |
||
| 644 | return $query->whereHas('translations', function (Builder $query) use ($key, $value, $locale) { |
||
| 645 | $query->where($this->getTranslationsTable().'.'.$key, 'LIKE', $value); |
||
| 646 | if ($locale) { |
||
| 647 | $query->where($this->getTranslationsTable().'.'.$this->getLocaleKey(), 'LIKE', $locale); |
||
| 648 | } |
||
| 649 | }); |
||
| 650 | } |
||
| 651 | |||
| 652 | /** |
||
| 653 | * This scope filters results by checking the translation fields. |
||
| 654 | * |
||
| 655 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 656 | * @param string $key |
||
| 657 | * @param string $value |
||
| 658 | * @param string $locale |
||
| 659 | * |
||
| 660 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
| 661 | */ |
||
| 662 | public function scopeOrWhereTranslationLike(Builder $query, $key, $value, $locale = null) |
||
| 663 | { |
||
| 664 | return $query->orWhereHas('translations', function (Builder $query) use ($key, $value, $locale) { |
||
| 665 | $query->where($this->getTranslationsTable().'.'.$key, 'LIKE', $value); |
||
| 666 | if ($locale) { |
||
| 667 | $query->where($this->getTranslationsTable().'.'.$this->getLocaleKey(), 'LIKE', $locale); |
||
| 668 | } |
||
| 669 | }); |
||
| 670 | } |
||
| 671 | |||
| 672 | /** |
||
| 673 | * @return array |
||
| 674 | */ |
||
| 675 | public function attributesToArray() |
||
| 676 | { |
||
| 677 | $attributes = parent::attributesToArray(); |
||
| 678 | |||
| 679 | if (! $this->relationLoaded('translations') && ! $this->toArrayAlwaysLoadsTranslations()) { |
||
| 680 | return $attributes; |
||
| 681 | } |
||
| 682 | |||
| 683 | $hiddenAttributes = $this->getHidden(); |
||
| 684 | |||
| 685 | foreach ($this->translatedAttributes as $field) { |
||
| 686 | if (in_array($field, $hiddenAttributes)) { |
||
| 687 | continue; |
||
| 688 | } |
||
| 689 | |||
| 690 | if ($translations = $this->getTranslation()) { |
||
| 691 | $attributes[$field] = $translations->$field; |
||
| 692 | } |
||
| 693 | } |
||
| 694 | |||
| 695 | return $attributes; |
||
| 696 | } |
||
| 697 | |||
| 698 | /** |
||
| 699 | * @return array |
||
| 700 | */ |
||
| 701 | public function getTranslationsArray() |
||
| 713 | |||
| 714 | /** |
||
| 715 | * @return string |
||
| 716 | */ |
||
| 717 | private function getTranslationsTable() |
||
| 721 | |||
| 722 | /** |
||
| 723 | * @return string |
||
| 724 | */ |
||
| 725 | protected function locale() |
||
| 734 | |||
| 735 | /** |
||
| 736 | * Set the default locale on the model. |
||
| 737 | * |
||
| 738 | * @param $locale |
||
| 739 | * |
||
| 740 | * @return $this |
||
| 741 | */ |
||
| 742 | public function setDefaultLocale($locale) |
||
| 748 | |||
| 749 | /** |
||
| 750 | * Get the default locale on the model. |
||
| 751 | * |
||
| 752 | * @return mixed |
||
| 753 | */ |
||
| 754 | public function getDefaultLocale() |
||
| 758 | |||
| 759 | /** |
||
| 760 | * Deletes all translations for this model. |
||
| 761 | * |
||
| 762 | * @param string|array|null $locales The locales to be deleted (array or single string) |
||
| 763 | * (e.g., ["en", "de"] would remove these translations). |
||
| 764 | */ |
||
| 765 | public function deleteTranslations($locales = null) |
||
| 781 | |||
| 782 | /** |
||
| 783 | * @param $key |
||
| 784 | * |
||
| 785 | * @return array |
||
| 786 | */ |
||
| 787 | private function getAttributeAndLocale($key) |
||
| 795 | |||
| 796 | /** |
||
| 797 | * @return bool |
||
| 798 | */ |
||
| 799 | private function toArrayAlwaysLoadsTranslations() |
||
| 803 | } |
||
| 804 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: