Completed
Push — master ( 080b9b...8f9441 )
by Freek
01:54
created

HasTranslations::getTranslations()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 12
rs 9.4285
cc 1
eloc 7
nc 1
nop 1
1
<?php
2
3
namespace Spatie\Translatable;
4
5
use Spatie\Translatable\Events\TranslationHasBeenSet;
6
use Spatie\Translatable\Exceptions\AttributeIsNotTranslatable;
7
8
trait HasTranslations
9
{
10
    /**
11
     * @param string $attributeName
12
     *
13
     * @return mixed
14
     */
15
    public function getAttributeValue($attributeName)
16
    {
17
        if (!$this->isTranslatableAttribute($attributeName)) {
18
            return parent::getAttributeValue($attributeName);
19
        }
20
21
        return $this->getTranslation($attributeName, config('app.locale'));
22
    }
23
24
    /**
25
     * @param string $attributeName
26
     * @param string $locale
27
     *
28
     * @return mixed
29
     */
30
    public function translate(string $attributeName, string $locale = '')
31
    {
32
        return $this->getTranslation($attributeName, $locale);
33
    }
34
35
    /***
36
     * @param string $attributeName
37
     * @param string $locale
38
     * @return mixed
39
     */
40
    public function getTranslation(string $attributeName, string $locale)
41
    {
42
        $translations = $this->getTranslations($attributeName);
43
44
        return $translations[$locale] ?? $this->castTranslation('', $attributeName);
45
    }
46
47
    public function getTranslations(string $attributeName) : array
48
    {
49
        $this->guardAgainstUntranslatableAttribute($attributeName);
50
51
        $translations = json_decode($this->getAttributes()[$attributeName] ?? '{}', 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...
52
53
        $castTranslations = array_map(function ($translation) use ($attributeName) {
54
            return $this->castTranslation($translation, $attributeName);
55
        }, $translations);
56
57
        return $castTranslations;
58
    }
59
60
    public function setTranslation(string $attributeName, string $locale, $value)
61
    {
62
        $this->guardAgainstUntranslatableAttribute($attributeName);
63
64
        $translations = $this->getTranslations($attributeName);
65
66
        $oldValue = $translations[$locale] ?? $this->castTranslation('', $attributeName);
67
68
        $translations[$locale] = $value;
69
70
        $this->setAttribute($attributeName, $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...
71
72
        event(new TranslationHasBeenSet($this, $attributeName, $locale, $oldValue, $value));
73
74
        return $this;
75
    }
76
77
    public function setTranslations(string $attributeName, array $translations)
78
    {
79
        $this->guardAgainstUntranslatableAttribute($attributeName);
80
81
        foreach ($translations as $locale => $translation) {
82
            $this->setTranslation($attributeName, $locale, $translation);
83
        }
84
85
        return $this;
86
    }
87
88
    public function forgetTranslation(string $attributeName, string $locale)
89
    {
90
        $translations = $this->getTranslations($attributeName);
91
92
        unset($translations[$locale]);
93
94
        $this->setAttribute($attributeName, $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...
95
96
        return $this;
97
    }
98
99
    public function getTranslatedLocales(string $attributeName) : array
100
    {
101
        return array_keys($this->getTranslations($attributeName));
102
    }
103
104
    protected function isTranslatableAttribute(string $attributeName) : bool
105
    {
106
        return TranslatableAttributeCollection::createForModel($this)->isTranslatable($attributeName);
107
    }
108
109
    protected function castTranslation($translation, $attributeName)
110
    {
111
        $cast = TranslatableAttributeCollection::createForModel($this)->getCast($attributeName);
112
113
        if ($cast === 'bool') {
114
            return boolval($translation);
115
        }
116
117
        if ($cast === 'integer') {
118
            return intval($translation);
119
        }
120
121
        if ($cast === 'float') {
122
            return floatval($translation);
123
        }
124
125
        if ($cast === 'array') {
126
            return (array) $translation;
127
        }
128
129
        return $translation;
130
    }
131
132
    protected function guardAgainstUntranslatableAttribute(string $attributeName)
133
    {
134
        if (!$this->isTranslatableAttribute($attributeName)) {
135
            throw AttributeIsNotTranslatable::make($attributeName, $this);
136
        }
137
    }
138
}
139