Completed
Pull Request — master (#11)
by
unknown
02:41
created

HasTranslations   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 187
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

14 Methods

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