Completed
Pull Request — master (#21)
by Brad
01:36
created

HasTranslations   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 180
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 24
lcom 1
cbo 3
dl 0
loc 180
rs 10
c 0
b 0
f 0

14 Methods

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