Test Setup Failed
Pull Request — master (#508)
by
unknown
63:13
created

Translatable::getTranslationModelNamespace()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Dimsav\Translatable;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Database\Eloquent\Builder;
7
use Illuminate\Database\Query\JoinClause;
8
use Illuminate\Database\Eloquent\Relations\Relation;
9
use Illuminate\Database\Query\Builder as QueryBuilder;
10
use Dimsav\Translatable\Exception\LocalesNotDefinedException;
11
12
trait Translatable
13
{
14
    protected static $autoloadTranslations = null;
15
16
    protected $defaultLocale;
17
18
    /**
19
     * Alias for getTranslation().
20
     *
21
     * @param string|null $locale
22
     * @param bool        $withFallback
23
     *
24
     * @return \Illuminate\Database\Eloquent\Model|null
25
     */
26
    public function translate($locale = null, $withFallback = false)
27
    {
28
        return $this->getTranslation($locale, $withFallback);
29
    }
30
31
    /**
32
     * Alias for getTranslation().
33
     *
34
     * @param string $locale
35
     *
36
     * @return \Illuminate\Database\Eloquent\Model|null
37
     */
38
    public function translateOrDefault($locale = null)
39
    {
40
        return $this->getTranslation($locale, true);
41
    }
42
43
    /**
44
     * Alias for getTranslationOrNew().
45
     *
46
     * @param string $locale
47
     *
48
     * @return \Illuminate\Database\Eloquent\Model|null
49
     */
50
    public function translateOrNew($locale = null)
51
    {
52
        return $this->getTranslationOrNew($locale);
53
    }
54
55
    /**
56
     * @param string|null $locale
57
     * @param bool        $withFallback
58
     *
59
     * @return \Illuminate\Database\Eloquent\Model|null
60
     */
61
    public function getTranslation($locale = null, $withFallback = null)
62
    {
63
        $configFallbackLocale = $this->getFallbackLocale();
64
        $locale = $locale ?: $this->locale();
65
        $withFallback = $withFallback === null ? $this->useFallback() : $withFallback;
66
        $fallbackLocale = $this->getFallbackLocale($locale);
67
68
        if ($translation = $this->getTranslationByLocaleKey($locale)) {
69
            return $translation;
70
        }
71
        if ($withFallback && $fallbackLocale) {
72
            if ($translation = $this->getTranslationByLocaleKey($fallbackLocale)) {
73
                return $translation;
74
            }
75
            if ($fallbackLocale !== $configFallbackLocale && $translation = $this->getTranslationByLocaleKey($configFallbackLocale)) {
76
                return $translation;
77
            }
78
        }
79
80
        return null;
81
    }
82
83
    /**
84
     * @param string|null $locale
85
     *
86
     * @return bool
87
     */
88
    public function hasTranslation($locale = null)
89
    {
90
        $locale = $locale ?: $this->locale();
91
92
        foreach ($this->translations as $translation) {
0 ignored issues
show
Bug introduced by
The property translations does not seem to exist. Did you mean autoloadTranslations?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
93
            if ($translation->getAttribute($this->getLocaleKey()) == $locale) {
94
                return true;
95
            }
96
        }
97
98
        return false;
99
    }
100
101
    /**
102
     * @return string
103
     */
104
    public function getTranslationModelName()
105
    {
106
        return $this->translationModel ?: $this->getTranslationModelNameDefault();
0 ignored issues
show
Bug introduced by
The property translationModel does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
107
    }
108
109
    /**
110
     * @return string
111
     */
112
    public function getTranslationModelNameDefault()
113
    {
114
        $fullModelName = $this->getTranslationModelNamespace().'\\'.class_basename(get_class($this));
115
116
117
        return $fullModelName.config('translatable.translation_suffix', 'Translation');
118
    }
119
120
    /**
121
     * @return string
122
     */
123
    public function getTranslationModelNamespace()
124
    {
125
        return config('translatable.translation_model_namespace', 'App');
126
    }
127
128
    /**
129
     * @return string
130
     */
131
    public function getRelationKey()
132
    {
133
        if ($this->translationForeignKey) {
134
            $key = $this->translationForeignKey;
0 ignored issues
show
Bug introduced by
The property translationForeignKey does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
135
        } elseif ($this->primaryKey !== 'id') {
0 ignored issues
show
Bug introduced by
The property primaryKey does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
136
            $key = $this->primaryKey;
137
        } else {
138
            $key = $this->getForeignKey();
0 ignored issues
show
Bug introduced by
It seems like getForeignKey() must be provided by classes using this trait. How about adding it as abstract method to this trait?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
139
        }
140
141
        return $key;
142
    }
143
144
    /**
145
     * @return string
146
     */
147
    public function getLocaleKey()
148
    {
149
        return $this->localeKey ?: config('translatable.locale_key', 'locale');
0 ignored issues
show
Bug introduced by
The property localeKey does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
150
    }
151
152
    /**
153
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
154
     */
155
    public function translations()
156
    {
157
        return $this->hasMany($this->getTranslationModelName(), $this->getRelationKey());
0 ignored issues
show
Bug introduced by
It seems like hasMany() must be provided by classes using this trait. How about adding it as abstract method to this trait?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
158
    }
159
160
    /**
161
     * @return bool
162
     */
163
    private function usePropertyFallback()
164
    {
165
        return $this->useFallback() && config('translatable.use_property_fallback', false);
166
    }
167
168
    /**
169
     * Returns the attribute value from fallback translation if value of attribute
170
     * is empty and the property fallback is enabled in the configuration.
171
     * in model.
172
     * @param $locale
173
     * @param $attribute
174
     * @return mixed
175
     */
176
    private function getAttributeOrFallback($locale, $attribute)
177
    {
178
        $translation = $this->getTranslation($locale);
179
180
        if (
181
            (
182
                ! $translation instanceof Model ||
183
                empty($translation->$attribute)
184
            ) &&
185
            $this->usePropertyFallback()
186
        ) {
187
            $translation = $this->getTranslation($this->getFallbackLocale(), false);
188
        }
189
190
        if ($translation instanceof Model) {
191
            return $translation->$attribute;
192
        }
193
194
        return null;
195
    }
196
197
    /**
198
     * @param string $key
199
     *
200
     * @return mixed
201
     */
202
    public function getAttribute($key)
203
    {
204
        list($attribute, $locale) = $this->getAttributeAndLocale($key);
205
206
        if ($this->isTranslationAttribute($attribute)) {
207
            if ($this->getTranslation($locale) === null) {
208
                return $this->getAttributeValue($attribute);
0 ignored issues
show
Bug introduced by
The method getAttributeValue() does not exist on Dimsav\Translatable\Translatable. Did you maybe mean getAttribute()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
209
            }
210
211
            // If the given $attribute has a mutator, we push it to $attributes and then call getAttributeValue
212
            // on it. This way, we can use Eloquent's checking for Mutation, type casting, and
213
            // Date fields.
214
            if ($this->hasGetMutator($attribute)) {
0 ignored issues
show
Bug introduced by
It seems like hasGetMutator() must be provided by classes using this trait. How about adding it as abstract method to this trait?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
215
                $this->attributes[$attribute] = $this->getAttributeOrFallback($locale, $attribute);
0 ignored issues
show
Bug introduced by
The property attributes does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
216
217
                return $this->getAttributeValue($attribute);
0 ignored issues
show
Bug introduced by
The method getAttributeValue() does not exist on Dimsav\Translatable\Translatable. Did you maybe mean getAttribute()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
218
            }
219
220
            return $this->getAttributeOrFallback($locale, $attribute);
221
        }
222
223
        return parent::getAttribute($key);
224
    }
225
226
    /**
227
     * @param string $key
228
     * @param mixed  $value
229
     *
230
     * @return $this
231
     */
232
    public function setAttribute($key, $value)
233
    {
234
        list($attribute, $locale) = $this->getAttributeAndLocale($key);
235
236
        if ($this->isTranslationAttribute($attribute)) {
237
            $this->getTranslationOrNew($locale)->$attribute = $value;
238
        } else {
239
            return parent::setAttribute($key, $value);
240
        }
241
242
        return $this;
243
    }
244
245
    /**
246
     * @param array $options
247
     *
248
     * @return bool
249
     */
250
    public function save(array $options = [])
251
    {
252
        if ($this->exists) {
0 ignored issues
show
Bug introduced by
The property exists does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
253
            if ($this->isDirty()) {
0 ignored issues
show
Bug introduced by
It seems like isDirty() must be provided by classes using this trait. How about adding it as abstract method to this trait?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
254
                // If $this->exists and dirty, parent::save() has to return true. If not,
0 ignored issues
show
Unused Code Comprehensibility introduced by
42% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
255
                // an error has occurred. Therefore we shouldn't save the translations.
256
                if (parent::save($options)) {
257
                    return $this->saveTranslations();
258
                }
259
260
                return false;
261
            } else {
262
                // If $this->exists and not dirty, parent::save() skips saving and returns
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
263
                // false. So we have to save the translations
264
                if ($this->fireModelEvent('saving') === false) {
0 ignored issues
show
Bug introduced by
It seems like fireModelEvent() must be provided by classes using this trait. How about adding it as abstract method to this trait?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
265
                    return false;
266
                }
267
268
                if ($saved = $this->saveTranslations()) {
269
                    $this->fireModelEvent('saved', false);
0 ignored issues
show
Bug introduced by
It seems like fireModelEvent() must be provided by classes using this trait. How about adding it as abstract method to this trait?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
270
                    $this->fireModelEvent('updated', false);
0 ignored issues
show
Bug introduced by
It seems like fireModelEvent() must be provided by classes using this trait. How about adding it as abstract method to this trait?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
271
                }
272
273
                return $saved;
274
            }
275
        } elseif (parent::save($options)) {
276
            // We save the translations only if the instance is saved in the database.
277
            return $this->saveTranslations();
278
        }
279
280
        return false;
281
    }
282
283
    /**
284
     * @param string $locale
285
     *
286
     * @return \Illuminate\Database\Eloquent\Model
287
     */
288
    protected function getTranslationOrNew($locale = null)
289
    {
290
        $locale = $locale ?: $this->locale();
291
292
        if (($translation = $this->getTranslation($locale, false)) === null) {
293
            $translation = $this->getNewTranslation($locale);
294
        }
295
296
        return $translation;
297
    }
298
299
    /**
300
     * @param array $attributes
301
     *
302
     * @throws \Illuminate\Database\Eloquent\MassAssignmentException
303
     * @return $this
304
     */
305
    public function fill(array $attributes)
306
    {
307
        foreach ($attributes as $key => $values) {
308
            if ($this->isKeyALocale($key)) {
309
                $this->getTranslationOrNew($key)->fill($values);
310
                unset($attributes[$key]);
311
            } else {
312
                list($attribute, $locale) = $this->getAttributeAndLocale($key);
313
                if ($this->isTranslationAttribute($attribute) and $this->isKeyALocale($locale)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning 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 have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

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 with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
314
                    $this->getTranslationOrNew($locale)->fill([$attribute => $values]);
315
                    unset($attributes[$key]);
316
                }
317
            }
318
        }
319
320
        return parent::fill($attributes);
321
    }
322
323
    /**
324
     * @param string $key
325
     */
326
    private function getTranslationByLocaleKey($key)
327
    {
328
        foreach ($this->translations as $translation) {
0 ignored issues
show
Bug introduced by
The property translations does not seem to exist. Did you mean autoloadTranslations?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
329
            if ($translation->getAttribute($this->getLocaleKey()) == $key) {
330
                return $translation;
331
            }
332
        }
333
334
        return null;
335
    }
336
337
    /**
338
     * @param null $locale
339
     *
340
     * @return string
341
     */
342
    private function getFallbackLocale($locale = null)
343
    {
344
        if ($locale && $this->isLocaleCountryBased($locale)) {
345
            if ($fallback = $this->getLanguageFromCountryBasedLocale($locale)) {
346
                return $fallback;
347
            }
348
        }
349
350
        return config('translatable.fallback_locale');
351
    }
352
353
    /**
354
     * @param $locale
355
     *
356
     * @return bool
357
     */
358
    private function isLocaleCountryBased($locale)
359
    {
360
        return strpos($locale, $this->getLocaleSeparator()) !== false;
361
    }
362
363
    /**
364
     * @param $locale
365
     *
366
     * @return string
367
     */
368
    private function getLanguageFromCountryBasedLocale($locale)
369
    {
370
        $parts = explode($this->getLocaleSeparator(), $locale);
371
372
        return array_get($parts, 0);
373
    }
374
375
    /**
376
     * @return bool|null
377
     */
378
    private function useFallback()
379
    {
380
        if (isset($this->useTranslationFallback) && $this->useTranslationFallback !== null) {
381
            return $this->useTranslationFallback;
0 ignored issues
show
Bug introduced by
The property useTranslationFallback does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
382
        }
383
384
        return config('translatable.use_fallback');
385
    }
386
387
    /**
388
     * @param string $key
389
     *
390
     * @return bool
391
     */
392
    public function isTranslationAttribute($key)
393
    {
394
        return in_array($key, $this->translatedAttributes);
0 ignored issues
show
Bug introduced by
The property translatedAttributes does not seem to exist. Did you mean attributes?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
395
    }
396
397
    /**
398
     * @param string $key
399
     *
400
     * @throws \Dimsav\Translatable\Exception\LocalesNotDefinedException
401
     * @return bool
402
     */
403
    protected function isKeyALocale($key)
404
    {
405
        $locales = $this->getLocales();
406
407
        return in_array($key, $locales);
408
    }
409
410
    /**
411
     * @throws \Dimsav\Translatable\Exception\LocalesNotDefinedException
412
     * @return array
413
     */
414
    protected function getLocales()
415
    {
416
        $localesConfig = (array) config('translatable.locales');
417
418
        if (empty($localesConfig)) {
419
            throw new LocalesNotDefinedException('Please make sure you have run "php artisan config:publish dimsav/laravel-translatable" '.
420
                ' and that the locales configuration is defined.');
421
        }
422
423
        $locales = [];
424
        foreach ($localesConfig as $key => $locale) {
425
            if (is_array($locale)) {
426
                $locales[] = $key;
427
                foreach ($locale as $countryLocale) {
428
                    $locales[] = $key.$this->getLocaleSeparator().$countryLocale;
429
                }
430
            } else {
431
                $locales[] = $locale;
432
            }
433
        }
434
435
        return $locales;
436
    }
437
438
    /**
439
     * @return string
440
     */
441
    protected function getLocaleSeparator()
442
    {
443
        return config('translatable.locale_separator', '-');
444
    }
445
446
    /**
447
     * @return bool
448
     */
449
    protected function saveTranslations()
450
    {
451
        $saved = true;
452
        foreach ($this->translations as $translation) {
0 ignored issues
show
Bug introduced by
The property translations does not seem to exist. Did you mean autoloadTranslations?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
453
            if ($saved && $this->isTranslationDirty($translation)) {
454
                if (! empty($connectionName = $this->getConnectionName())) {
0 ignored issues
show
Bug introduced by
It seems like getConnectionName() must be provided by classes using this trait. How about adding it as abstract method to this trait?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
455
                    $translation->setConnection($connectionName);
456
                }
457
458
                $translation->setAttribute($this->getRelationKey(), $this->getKey());
0 ignored issues
show
Bug introduced by
It seems like getKey() must be provided by classes using this trait. How about adding it as abstract method to this trait?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
459
                $saved = $translation->save();
460
            }
461
        }
462
463
        return $saved;
464
    }
465
466
    /**
467
     * @param array
468
     *
469
     * @return \Illuminate\Database\Eloquent\Model
470
     */
471
    public function replicateWithTranslations(array $except = null)
472
    {
473
        $newInstance = parent::replicate($except);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (replicate() instead of replicateWithTranslations()). Are you sure this is correct? If so, you might want to change this to $this->replicate().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
474
475
        unset($newInstance->translations);
476
        foreach ($this->translations as $translation) {
0 ignored issues
show
Bug introduced by
The property translations does not seem to exist. Did you mean autoloadTranslations?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
477
            $newTranslation = $translation->replicate();
478
            $newInstance->translations->add($newTranslation);
479
        }
480
481
        return  $newInstance;
482
    }
483
484
    /**
485
     * @param \Illuminate\Database\Eloquent\Model $translation
486
     *
487
     * @return bool
488
     */
489
    protected function isTranslationDirty(Model $translation)
490
    {
491
        $dirtyAttributes = $translation->getDirty();
492
        unset($dirtyAttributes[$this->getLocaleKey()]);
493
494
        return count($dirtyAttributes) > 0;
495
    }
496
497
    /**
498
     * @param string $locale
499
     *
500
     * @return \Illuminate\Database\Eloquent\Model
501
     */
502
    public function getNewTranslation($locale)
503
    {
504
        $modelName = $this->getTranslationModelName();
505
        $translation = new $modelName();
506
        $translation->setAttribute($this->getLocaleKey(), $locale);
507
        $this->translations->add($translation);
0 ignored issues
show
Bug introduced by
The property translations does not seem to exist. Did you mean autoloadTranslations?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
508
509
        return $translation;
510
    }
511
512
    /**
513
     * @param $key
514
     *
515
     * @return bool
516
     */
517
    public function __isset($key)
518
    {
519
        return $this->isTranslationAttribute($key) || parent::__isset($key);
520
    }
521
522
    /**
523
     * @param \Illuminate\Database\Eloquent\Builder $query
524
     * @param string                                $locale
525
     *
526
     * @return \Illuminate\Database\Eloquent\Builder|static
527
     */
528 View Code Duplication
    public function scopeTranslatedIn(Builder $query, $locale = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
529
    {
530
        $locale = $locale ?: $this->locale();
531
532
        return $query->whereHas('translations', function (Builder $q) use ($locale) {
533
            $q->where($this->getLocaleKey(), '=', $locale);
534
        });
535
    }
536
537
    /**
538
     * @param \Illuminate\Database\Eloquent\Builder $query
539
     * @param string                                $locale
540
     *
541
     * @return \Illuminate\Database\Eloquent\Builder|static
542
     */
543 View Code Duplication
    public function scopeNotTranslatedIn(Builder $query, $locale = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
544
    {
545
        $locale = $locale ?: $this->locale();
546
547
        return $query->whereDoesntHave('translations', function (Builder $q) use ($locale) {
548
            $q->where($this->getLocaleKey(), '=', $locale);
549
        });
550
    }
551
552
    /**
553
     * @param \Illuminate\Database\Eloquent\Builder $query
554
     *
555
     * @return \Illuminate\Database\Eloquent\Builder|static
556
     */
557
    public function scopeTranslated(Builder $query)
558
    {
559
        return $query->has('translations');
560
    }
561
562
    /**
563
     * Adds scope to get a list of translated attributes, using the current locale.
564
     * Example usage: Country::listsTranslations('name')->get()->toArray()
565
     * Will return an array with items:
566
     *  [
567
     *      'id' => '1',                // The id of country
568
     *      'name' => 'Griechenland'    // The translated name
569
     *  ].
570
     *
571
     * @param \Illuminate\Database\Eloquent\Builder $query
572
     * @param string                                $translationField
573
     */
574
    public function scopeListsTranslations(Builder $query, $translationField)
575
    {
576
        $withFallback = $this->useFallback();
577
        $translationTable = $this->getTranslationsTable();
578
        $localeKey = $this->getLocaleKey();
579
580
        $query
0 ignored issues
show
Bug introduced by
The method select() does not exist on Illuminate\Database\Eloquent\Builder. Did you maybe mean createSelectWithConstraint()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
581
            ->select($this->getTable().'.'.$this->getKeyName(), $translationTable.'.'.$translationField)
0 ignored issues
show
Bug introduced by
It seems like getTable() must be provided by classes using this trait. How about adding it as abstract method to this trait?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
Bug introduced by
It seems like getKeyName() must be provided by classes using this trait. How about adding it as abstract method to this trait?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
582
            ->leftJoin($translationTable, $translationTable.'.'.$this->getRelationKey(), '=', $this->getTable().'.'.$this->getKeyName())
0 ignored issues
show
Bug introduced by
It seems like getTable() must be provided by classes using this trait. How about adding it as abstract method to this trait?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
Bug introduced by
It seems like getKeyName() must be provided by classes using this trait. How about adding it as abstract method to this trait?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
583
            ->where($translationTable.'.'.$localeKey, $this->locale());
584
        if ($withFallback) {
585
            $query->orWhere(function (Builder $q) use ($translationTable, $localeKey) {
586
                $q->where($translationTable.'.'.$localeKey, $this->getFallbackLocale())
587
                  ->whereNotIn($translationTable.'.'.$this->getRelationKey(), function (QueryBuilder $q) use (
588
                      $translationTable,
589
                      $localeKey
590
                  ) {
591
                      $q->select($translationTable.'.'.$this->getRelationKey())
592
                        ->from($translationTable)
593
                        ->where($translationTable.'.'.$localeKey, $this->locale());
594
                  });
595
            });
596
        }
597
    }
598
599
    /**
600
     * This scope eager loads the translations for the default and the fallback locale only.
601
     * We can use this as a shortcut to improve performance in our application.
602
     *
603
     * @param Builder $query
604
     */
605
    public function scopeWithTranslation(Builder $query)
606
    {
607
        $query->with([
608
            'translations' => function (Relation $query) {
609
                if ($this->useFallback()) {
610
                    $locale = $this->locale();
611
                    $countryFallbackLocale = $this->getFallbackLocale($locale); // e.g. de-DE => de
612
                    $locales = array_unique([$locale, $countryFallbackLocale, $this->getFallbackLocale()]);
613
614
                    return $query->whereIn($this->getTranslationsTable().'.'.$this->getLocaleKey(), $locales);
615
                }
616
617
                return $query->where($this->getTranslationsTable().'.'.$this->getLocaleKey(), $this->locale());
618
            },
619
        ]);
620
    }
621
622
    /**
623
     * This scope filters results by checking the translation fields.
624
     *
625
     * @param \Illuminate\Database\Eloquent\Builder $query
626
     * @param string                                $key
627
     * @param string                                $value
628
     * @param string                                $locale
629
     *
630
     * @return \Illuminate\Database\Eloquent\Builder|static
631
     */
632 View Code Duplication
    public function scopeWhereTranslation(Builder $query, $key, $value, $locale = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
633
    {
634
        return $query->whereHas('translations', function (Builder $query) use ($key, $value, $locale) {
635
            $query->where($this->getTranslationsTable().'.'.$key, $value);
636
            if ($locale) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $locale of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
637
                $query->where($this->getTranslationsTable().'.'.$this->getLocaleKey(), $locale);
638
            }
639
        });
640
    }
641
642
    /**
643
     * This scope filters results by checking the translation fields.
644
     *
645
     * @param \Illuminate\Database\Eloquent\Builder $query
646
     * @param string                                $key
647
     * @param string                                $value
648
     * @param string                                $locale
649
     *
650
     * @return \Illuminate\Database\Eloquent\Builder|static
651
     */
652 View Code Duplication
    public function scopeOrWhereTranslation(Builder $query, $key, $value, $locale = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
653
    {
654
        return $query->orWhereHas('translations', function (Builder $query) use ($key, $value, $locale) {
655
            $query->where($this->getTranslationsTable().'.'.$key, $value);
656
            if ($locale) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $locale of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
657
                $query->where($this->getTranslationsTable().'.'.$this->getLocaleKey(), $locale);
658
            }
659
        });
660
    }
661
662
    /**
663
     * This scope filters results by checking the translation fields.
664
     *
665
     * @param \Illuminate\Database\Eloquent\Builder $query
666
     * @param string                                $key
667
     * @param string                                $value
668
     * @param string                                $locale
669
     *
670
     * @return \Illuminate\Database\Eloquent\Builder|static
671
     */
672 View Code Duplication
    public function scopeWhereTranslationLike(Builder $query, $key, $value, $locale = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
673
    {
674
        return $query->whereHas('translations', function (Builder $query) use ($key, $value, $locale) {
675
            $query->where($this->getTranslationsTable().'.'.$key, 'LIKE', $value);
676
            if ($locale) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $locale of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
677
                $query->where($this->getTranslationsTable().'.'.$this->getLocaleKey(), 'LIKE', $locale);
678
            }
679
        });
680
    }
681
682
    /**
683
     * This scope filters results by checking the translation fields.
684
     *
685
     * @param \Illuminate\Database\Eloquent\Builder $query
686
     * @param string                                $key
687
     * @param string                                $value
688
     * @param string                                $locale
689
     *
690
     * @return \Illuminate\Database\Eloquent\Builder|static
691
     */
692 View Code Duplication
    public function scopeOrWhereTranslationLike(Builder $query, $key, $value, $locale = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
693
    {
694
        return $query->orWhereHas('translations', function (Builder $query) use ($key, $value, $locale) {
695
            $query->where($this->getTranslationsTable().'.'.$key, 'LIKE', $value);
696
            if ($locale) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $locale of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
697
                $query->where($this->getTranslationsTable().'.'.$this->getLocaleKey(), 'LIKE', $locale);
698
            }
699
        });
700
    }
701
702
    /**
703
     * This scope sorts results by the given translation field.
704
     *
705
     * @param \Illuminate\Database\Eloquent\Builder $query
706
     * @param string                                $key
707
     * @param string                                $sortmethod
708
     *
709
     * @return \Illuminate\Database\Eloquent\Builder|static
710
     */
711
    public function scopeOrderByTranslation(Builder $query, $key, $sortmethod = 'asc')
712
    {
713
        $translationTable = $this->getTranslationsTable();
714
        $localeKey = $this->getLocaleKey();
715
        $table = $this->getTable();
0 ignored issues
show
Bug introduced by
It seems like getTable() must be provided by classes using this trait. How about adding it as abstract method to this trait?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
716
        $keyName = $this->getKeyName();
0 ignored issues
show
Bug introduced by
It seems like getKeyName() must be provided by classes using this trait. How about adding it as abstract method to this trait?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
717
718
        return $query
719
            ->join($translationTable, function (JoinClause $join) use ($translationTable, $localeKey, $table, $keyName) {
720
                $join
721
                    ->on($translationTable.'.'.$this->getRelationKey(), '=', $table.'.'.$keyName)
722
                    ->where($translationTable.'.'.$localeKey, $this->locale());
723
            })
724
            ->orderBy($translationTable.'.'.$key, $sortmethod)
725
            ->select($table.'.*')
726
            ->with('translations');
727
    }
728
729
    /**
730
     * @return array
731
     */
732
    public function attributesToArray()
733
    {
734
        $attributes = parent::attributesToArray();
735
736
        if (
737
            (! $this->relationLoaded('translations') && ! $this->toArrayAlwaysLoadsTranslations() && is_null(self::$autoloadTranslations))
0 ignored issues
show
Bug introduced by
It seems like relationLoaded() must be provided by classes using this trait. How about adding it as abstract method to this trait?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
738
            || self::$autoloadTranslations === false
739
        ) {
740
            return $attributes;
741
        }
742
743
        $hiddenAttributes = $this->getHidden();
0 ignored issues
show
Bug introduced by
It seems like getHidden() must be provided by classes using this trait. How about adding it as abstract method to this trait?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
744
745
        foreach ($this->translatedAttributes as $field) {
0 ignored issues
show
Bug introduced by
The property translatedAttributes does not seem to exist. Did you mean attributes?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
746
            if (in_array($field, $hiddenAttributes)) {
747
                continue;
748
            }
749
750
            $attributes[$field] = $this->getAttributeOrFallback(null, $field);
751
        }
752
753
        return $attributes;
754
    }
755
756
    /**
757
     * @return array
758
     */
759
    public function getTranslationsArray()
760
    {
761
        $translations = [];
762
763
        foreach ($this->translations as $translation) {
0 ignored issues
show
Bug introduced by
The property translations does not seem to exist. Did you mean autoloadTranslations?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
764
            foreach ($this->translatedAttributes as $attr) {
0 ignored issues
show
Bug introduced by
The property translatedAttributes does not seem to exist. Did you mean attributes?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
765
                $translations[$translation->{$this->getLocaleKey()}][$attr] = $translation->{$attr};
766
            }
767
        }
768
769
        return $translations;
770
    }
771
772
    /**
773
     * @return string
774
     */
775
    private function getTranslationsTable()
776
    {
777
        return app()->make($this->getTranslationModelName())->getTable();
778
    }
779
780
    /**
781
     * @return string
782
     */
783
    protected function locale()
784
    {
785
        if ($this->defaultLocale) {
786
            return $this->defaultLocale;
787
        }
788
789
        return config('translatable.locale')
790
            ?: app()->make('translator')->getLocale();
791
    }
792
793
    /**
794
     * Set the default locale on the model.
795
     *
796
     * @param $locale
797
     *
798
     * @return $this
799
     */
800
    public function setDefaultLocale($locale)
801
    {
802
        $this->defaultLocale = $locale;
803
804
        return $this;
805
    }
806
807
    /**
808
     * Get the default locale on the model.
809
     *
810
     * @return mixed
811
     */
812
    public function getDefaultLocale()
813
    {
814
        return $this->defaultLocale;
815
    }
816
817
    /**
818
     * Deletes all translations for this model.
819
     *
820
     * @param string|array|null $locales The locales to be deleted (array or single string)
821
     *                                   (e.g., ["en", "de"] would remove these translations).
822
     */
823
    public function deleteTranslations($locales = null)
824
    {
825
        if ($locales === null) {
826
            $translations = $this->translations()->get();
827
        } else {
828
            $locales = (array) $locales;
829
            $translations = $this->translations()->whereIn($this->getLocaleKey(), $locales)->get();
830
        }
831
        foreach ($translations as $translation) {
832
            $translation->delete();
833
        }
834
835
        // we need to manually "reload" the collection built from the relationship
836
        // otherwise $this->translations()->get() would NOT be the same as $this->translations
0 ignored issues
show
Unused Code Comprehensibility introduced by
36% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
837
        $this->load('translations');
0 ignored issues
show
Bug introduced by
It seems like load() must be provided by classes using this trait. How about adding it as abstract method to this trait?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
838
    }
839
840
    /**
841
     * @param $key
842
     *
843
     * @return array
844
     */
845
    private function getAttributeAndLocale($key)
846
    {
847
        if (str_contains($key, ':')) {
848
            return explode(':', $key);
849
        }
850
851
        return [$key, $this->locale()];
852
    }
853
854
    /**
855
     * @return bool
856
     */
857
    private function toArrayAlwaysLoadsTranslations()
858
    {
859
        return config('translatable.to_array_always_loads_translations', true);
860
    }
861
862
    public static function enableAutoloadTranslations()
863
    {
864
        self::$autoloadTranslations = true;
865
    }
866
867
    public static function defaultAutoloadTranslations()
868
    {
869
        self::$autoloadTranslations = null;
870
    }
871
872
    public static function disableAutoloadTranslations()
873
    {
874
        self::$autoloadTranslations = false;
875
    }
876
}
877