Completed
Push — master ( b0f055...3c1dda )
by Freek
02:01
created

HasTranslations::getTranslation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 3
1
<?php
2
3
namespace Spatie\Translatable;
4
5
use Spatie\Translatable\Exceptions\Untranslatable;
6
7
trait HasTranslations
8
{
9
    /**
10
     * @param string $attributeName
11
     *
12
     * @return mixed
13
     */
14
    public function getAttributeValue($attributeName)
15
    {
16
        if (!$this->isTranslatableAttribute($attributeName)) {
17
            return parent::getAttributeValue($attributeName);
18
        }
19
20
        return $this->getTranslation($attributeName, config('app.locale'));
21
    }
22
23
    public function translate(string $attributeName, string $locale = '', string $default = '') : string
24
    {
25
        return $this->getTranslation($attributeName, $locale, $default);
26
    }
27
28
    public function getTranslation(string $attributeName, string $locale, string $default = '') : string
29
    {
30
        $translations = $this->getTranslations($attributeName);
31
32
        return $translations[$locale] ?? $default;
33
    }
34
35
    public function getTranslations(string $attributeName) : array
36
    {
37
        $this->guardAgainstUntranslatableFieldName($attributeName);
38
39
        $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...
40
41
        return $translations;
42
    }
43
44
    public function setTranslation(string $attributeName, string $locale, string $value)
45
    {
46
        $this->guardAgainstUntranslatableFieldName($attributeName);
47
48
        $translations = $this->getTranslations($attributeName);
49
50
        $translations[$locale] = $value;
51
52
        $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...
53
54
        return $this;
55
    }
56
57
    public function setTranslations(string $attributeName, array $translations)
58
    {
59
        $this->guardAgainstUntranslatableFieldName($attributeName);
60
61
        foreach ($translations as $locale => $translation) {
62
            $this->setTranslation($attributeName, $locale, $translation);
63
        }
64
65
        return $this;
66
    }
67
68
    public function forgetTranslation(string $attributeName, string $locale)
69
    {
70
        $translations = $this->getTranslations($attributeName);
71
72
        unset($translations[$locale]);
73
74
        $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...
75
76
        return $this;
77
    }
78
79
    public function getTranslatedLocales(string $attributeName) : array
80
    {
81
        return array_keys($this->getTranslations($attributeName));
82
    }
83
84
    protected function isTranslatableAttribute(string $attributeName) : bool
85
    {
86
        return in_array($attributeName, $this->getTranslatableAttributes());
0 ignored issues
show
Bug introduced by
It seems like getTranslatableAttributes() 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...
87
    }
88
89
    protected function guardAgainstUntranslatableFieldName(string $attributeName)
90
    {
91
        if (!$this->isTranslatableAttribute($attributeName)) {
92
            throw Untranslatable::attributeIsNotTranslatable($attributeName, $this);
93
        }
94
    }
95
}
96