Completed
Pull Request — master (#48)
by
unknown
05:20
created

HasTranslations::getTranslationWithoutFallback()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
namespace Spatie\Translatable;
4
5
use Illuminate\Support\Str;
6
use Spatie\Translatable\Events\TranslationHasBeenSet;
7
use Spatie\Translatable\Exceptions\AttributeIsNotTranslatable;
8
9
trait HasTranslations
10
{
11
    /**
12
     * @param string $key
13
     *
14
     * @return mixed
15
     */
16
    public function getAttributeValue($key)
17
    {
18
        if (!$this->isTranslatableAttribute($key)) {
19
            return parent::getAttributeValue($key);
20
        }
21
22
        return $this->getTranslation($key, config('app.locale'));
23
    }
24
25
    /**
26
     * @param string $key
27
     * @param $value
28
     *
29
     * @return $this
30
     */
31
    public function setAttribute($key, $value)
32
    {
33
        if ($this->isTranslatableAttribute($key) && !is_array($value)) {
34
            return $this->setTranslation($key, app()->getLocale(), $value);
35
        }
36
        return parent::setAttribute($key, $value);
37
    }
38
39
    /**
40
     * @param string $key
41
     * @param string $locale
42
     *
43
     * @return mixed
44
     */
45
    public function translate(string $key, string $locale = '')
46
    {
47
        return $this->getTranslation($key, $locale);
48
    }
49
50
    /***
51
     * @param string $key
52
     * @param string $locale
53
     * @param bool $useFallbackLocale
54
     *
55
     * @return mixed
56
     */
57
    public function getTranslation(string $key, string $locale, bool $useFallbackLocale = true)
58
    {
59
        $locale = $this->normalizeLocale($key, $locale, $useFallbackLocale);
60
61
        $translations = $this->getTranslations($key);
62
63
        $translation = $translations[$locale] ?? '';
64
65
        if ($this->hasGetMutator($key)) {
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...
66
            return $this->mutateAttribute($key, $translation);
0 ignored issues
show
Bug introduced by
It seems like mutateAttribute() 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...
67
        }
68
69
        return $translation;
70
    }
71
72
    public function getTranslationWithFallback(string $key, string $locale)
73
    {
74
        return $this->getTranslation($key, $locale, true);
75
    }
76
77
    public function getTranslationWithoutFallback(string $key, string $locale)
78
    {
79
        return $this->getTranslation($key, $locale, false);
80
    }
81
82
    public function getTranslations($key) : array
83
    {
84
        $this->guardAgainstUntranslatableAttribute($key);
85
86
        return json_decode($this->getAttributes()[$key] ?? '' ?: '{}', true);
0 ignored issues
show
Bug introduced by
It seems like getAttributes() 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...
87
    }
88
89
    /**
90
     * @param string $key
91
     * @param string $locale
92
     * @param $value
93
     *
94
     * @return $this
95
     */
96
    public function setTranslation(string $key, string $locale, $value)
97
    {
98
        $this->guardAgainstUntranslatableAttribute($key);
99
100
        $translations = $this->getTranslations($key);
101
102
        $oldValue = $translations[$locale] ?? '';
103
104
        if ($this->hasSetMutator($key)) {
0 ignored issues
show
Bug introduced by
It seems like hasSetMutator() 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...
105
            $method = 'set'.Str::studly($key).'Attribute';
106
            $value = $this->{$method}($value);
107
        }
108
109
        $translations[$locale] = $value;
110
111
        $this->attributes[$key] = $this->asJson($translations);
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...
Bug introduced by
It seems like asJson() 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...
112
113
        event(new TranslationHasBeenSet($this, $key, $locale, $oldValue, $value));
114
115
        return $this;
116
    }
117
118
    /**
119
     * @param string $key
120
     * @param array  $translations
121
     *
122
     * @return $this
123
     */
124
    public function setTranslations(string $key, array $translations)
125
    {
126
        $this->guardAgainstUntranslatableAttribute($key);
127
128
        foreach ($translations as $locale => $translation) {
129
            $this->setTranslation($key, $locale, $translation);
130
        }
131
132
        return $this;
133
    }
134
135
    /**
136
     * @param string $key
137
     * @param string $locale
138
     *
139
     * @return $this
140
     */
141
    public function forgetTranslation(string $key, string $locale)
142
    {
143
        $translations = $this->getTranslations($key);
144
145
        unset($translations[$locale]);
146
147
        $this->setAttribute($key, $translations);
148
149
        return $this;
150
    }
151
152
    public function getTranslatedLocales(string $key) : array
153
    {
154
        return array_keys($this->getTranslations($key));
155
    }
156
157
    public function isTranslatableAttribute(string $key) : bool
158
    {
159
        return in_array($key, $this->getTranslatableAttributes());
160
    }
161
162
    protected function guardAgainstUntranslatableAttribute(string $key)
163
    {
164
        if (!$this->isTranslatableAttribute($key)) {
165
            throw AttributeIsNotTranslatable::make($key, $this);
166
        }
167
    }
168
169
    protected function normalizeLocale(string $key, string $locale, bool $useFallbackLocale) : string
170
    {
171
        if (in_array($locale, $this->getTranslatedLocales($key))) {
172
            return $locale;
173
        }
174
175
        if (!$useFallbackLocale) {
176
            return $locale;
177
        }
178
179
        if (!is_null($fallbackLocale = config('laravel-translatable.fallback_locale'))) {
180
            return $fallbackLocale;
181
        }
182
183
        return $locale;
184
    }
185
186
    public function getTranslatableAttributes() : array
187
    {
188
        return is_array($this->translatable)
0 ignored issues
show
Bug introduced by
The property translatable 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...
189
            ? $this->translatable
190
            : [];
191
    }
192
193
    public function getCasts() : array
194
    {
195
        return array_merge(
196
            parent::getCasts(),
197
            array_fill_keys($this->getTranslatableAttributes(), 'array')
198
        );
199
    }
200
}
201