Completed
Push — master ( 377466...499630 )
by Freek
02:00
created

HasTranslations::getAttributeValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 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 $locale
28
     *
29
     * @return mixed
30
     */
31
    public function translate(string $key, string $locale = '')
32
    {
33
        return $this->getTranslation($key, $locale);
34
    }
35
36
    /***
37
     * @param string $key
38
     * @param string $locale
39
     *
40
     * @return mixed
41
     */
42
    public function getTranslation(string $key, string $locale)
43
    {
44
        $translations = $this->getTranslations($key);
45
46
        $translation = $translations[$locale] ?? '';
47
48
        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...
49
            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...
50
        }
51
52
        return $translation;
53
    }
54
55
    public function getTranslations($key) : array
56
    {
57
        $this->guardAgainstUntranslatableAttribute($key);
58
59
        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...
60
    }
61
62
    /**
63
     * @param string $key
64
     * @param string $locale
65
     * @param $value
66
     * 
67
     * @return $this
68
     */
69
    public function setTranslation(string $key, string $locale, $value)
70
    {
71
        $this->guardAgainstUntranslatableAttribute($key);
72
73
        $translations = $this->getTranslations($key);
74
75
        $oldValue = $translations[$locale] ?? '';
76
77
        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...
78
            $method = 'set'.Str::studly($key).'Attribute';
79
            $value = $this->{$method}($value);
80
        }
81
82
        $translations[$locale] = $value;
83
84
        $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...
85
86
        event(new TranslationHasBeenSet($this, $key, $locale, $oldValue, $value));
87
88
        return $this;
89
    }
90
91
    /**
92
     * @param string $key
93
     * @param array  $translations
94
     * 
95
     * @return $this
96
     */
97
    public function setTranslations(string $key, array $translations)
98
    {
99
        $this->guardAgainstUntranslatableAttribute($key);
100
101
        foreach ($translations as $locale => $translation) {
102
            $this->setTranslation($key, $locale, $translation);
103
        }
104
105
        return $this;
106
    }
107
108
    /**
109
     * @param string $key
110
     * @param string $locale
111
     * 
112
     * @return $this
113
     */
114
    public function forgetTranslation(string $key, string $locale)
115
    {
116
        $translations = $this->getTranslations($key);
117
118
        unset($translations[$locale]);
119
120
        $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...
121
122
        return $this;
123
    }
124
125
    public function getTranslatedLocales(string $key) : array
126
    {
127
        return array_keys($this->getTranslations($key));
128
    }
129
130
    protected function isTranslatableAttribute(string $key) : bool
131
    {
132
        return in_array($key, $this->getTranslatableAttributes());
133
    }
134
135
    protected function guardAgainstUntranslatableAttribute(string $key)
136
    {
137
        if (!$this->isTranslatableAttribute($key)) {
138
            throw AttributeIsNotTranslatable::make($key, $this);
139
        }
140
    }
141
142
    public function getTranslatableAttributes() : array
143
    {
144
        return is_array($this->translatableAttributes)
0 ignored issues
show
Bug introduced by
The property translatableAttributes does not seem to exist. Did you mean attributes?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
145
            ? $this->translatableAttributes
0 ignored issues
show
Bug introduced by
The property translatableAttributes does not seem to exist. Did you mean attributes?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
146
            : [];
147
    }
148
149
    public function getCasts() : array
150
    {
151
        return array_merge(
152
            parent::getCasts(),
153
            array_fill_keys($this->getTranslatableAttributes(), 'array')
154
        );
155
    }
156
}
157