Completed
Pull Request — master (#40)
by
unknown
01:56
created

HasTranslations   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 201
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 31
lcom 1
cbo 3
dl 0
loc 201
rs 9.8
c 0
b 0
f 0

16 Methods

Rating   Name   Duplication   Size   Complexity  
A getAttributeValue() 0 8 2
B setAttribute() 0 16 6
A translate() 0 4 1
A getTranslation() 0 14 2
A getTranslationWithFallback() 0 4 1
A getTranslationWithoutFallback() 0 4 1
A getTranslations() 0 6 2
A setTranslation() 0 21 2
A setTranslations() 0 10 2
A forgetTranslation() 0 10 1
A getTranslatedLocales() 0 4 1
A isTranslatableAttribute() 0 4 1
A guardAgainstUntranslatableAttribute() 0 6 2
A normalizeLocale() 0 16 4
A getTranslatableAttributes() 0 6 2
A getCasts() 0 7 1
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 string $value
28
     *
29
     * @return mixed
30
     */
31
    public function setAttribute($key, $value)
32
    {
33
        if (!$this->isTranslatableAttribute($key)) {
34
            return parent::setAttribute($key, $value);
35
        }
36
37
        if (is_string($value) && !json_decode($value)) {
38
            return $this->setTranslation($key, config('app.locale'), $value);
39
        }
40
       
41
        if (is_string($value) && json_decode($value)) {
42
            return $this->setTranslations($key, json_decode($value, true));
43
        }
44
45
        return $value;
46
    }
47
48
    /**
49
     * @param string $key
50
     * @param string $locale
51
     *
52
     * @return mixed
53
     */
54
    public function translate(string $key, string $locale = '')
55
    {
56
        return $this->getTranslation($key, $locale);
57
    }
58
59
    /***
60
     * @param string $key
61
     * @param string $locale
62
     * @param bool $useFallbackLocale
63
     *
64
     * @return mixed
65
     */
66
    public function getTranslation(string $key, string $locale, bool $useFallbackLocale = true)
67
    {
68
        $locale = $this->normalizeLocale($key, $locale, $useFallbackLocale);
69
70
        $translations = $this->getTranslations($key);
71
72
        $translation = $translations[$locale] ?? '';
73
74
        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...
75
            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...
76
        }
77
78
        return $translation;
79
    }
80
81
    public function getTranslationWithFallback(string $key, string $locale)
82
    {
83
        return $this->getTranslation($key, $locale, true);
84
    }
85
86
    public function getTranslationWithoutFallback(string $key, string $locale)
87
    {
88
        return $this->getTranslation($key, $locale, false);
89
    }
90
91
    public function getTranslations($key) : array
92
    {
93
        $this->guardAgainstUntranslatableAttribute($key);
94
95
        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...
96
    }
97
98
    /**
99
     * @param string $key
100
     * @param string $locale
101
     * @param $value
102
     *
103
     * @return $this
104
     */
105
    public function setTranslation(string $key, string $locale, $value)
106
    {
107
        $this->guardAgainstUntranslatableAttribute($key);
108
109
        $translations = $this->getTranslations($key);
110
111
        $oldValue = $translations[$locale] ?? '';
112
113
        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...
114
            $method = 'set'.Str::studly($key).'Attribute';
115
            $value = $this->{$method}($value);
116
        }
117
118
        $translations[$locale] = $value;
119
120
        $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...
121
122
        event(new TranslationHasBeenSet($this, $key, $locale, $oldValue, $value));
123
124
        return $this;
125
    }
126
127
    /**
128
     * @param string $key
129
     * @param array  $translations
130
     *
131
     * @return $this
132
     */
133
    public function setTranslations(string $key, array $translations)
134
    {
135
        $this->guardAgainstUntranslatableAttribute($key);
136
137
        foreach ($translations as $locale => $translation) {
138
            $this->setTranslation($key, $locale, $translation);
139
        }
140
141
        return $this;
142
    }
143
144
    /**
145
     * @param string $key
146
     * @param string $locale
147
     *
148
     * @return $this
149
     */
150
    public function forgetTranslation(string $key, string $locale)
151
    {
152
        $translations = $this->getTranslations($key);
153
154
        unset($translations[$locale]);
155
156
        $this->attributes[$key] = $this->asJson($translations);
0 ignored issues
show
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...
157
158
        return $this;
159
    }
160
161
    public function getTranslatedLocales(string $key) : array
162
    {
163
        return array_keys($this->getTranslations($key));
164
    }
165
166
    public function isTranslatableAttribute(string $key) : bool
167
    {
168
        return in_array($key, $this->getTranslatableAttributes());
169
    }
170
171
    protected function guardAgainstUntranslatableAttribute(string $key)
172
    {
173
        if (!$this->isTranslatableAttribute($key)) {
174
            throw AttributeIsNotTranslatable::make($key, $this);
175
        }
176
    }
177
178
    protected function normalizeLocale(string $key, string $locale, bool $useFallbackLocale) : string
179
    {
180
        if (in_array($locale, $this->getTranslatedLocales($key))) {
181
            return $locale;
182
        }
183
184
        if (!$useFallbackLocale) {
185
            return $locale;
186
        }
187
188
        if (!is_null($fallbackLocale = config('laravel-translatable.fallback_locale'))) {
189
            return $fallbackLocale;
190
        }
191
192
        return $locale;
193
    }
194
195
    public function getTranslatableAttributes() : array
196
    {
197
        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...
198
            ? $this->translatable
199
            : [];
200
    }
201
202
    public function getCasts() : array
203
    {
204
        return array_merge(
205
            parent::getCasts(),
206
            array_fill_keys($this->getTranslatableAttributes(), 'array')
207
        );
208
    }
209
}
210