Completed
Pull Request — master (#21)
by Brad
06:28
created

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