Test Failed
Pull Request — master (#2)
by
unknown
04:16
created

Translatable::getTranslations()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 0
cts 7
cp 0
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace LaTevaWeb\Translatable\Traits;
4
5
use Illuminate\Database\Eloquent\Relations\MorphToMany;
6
use Illuminate\Support\Collection;
7
use Illuminate\Support\Facades\Config;
8
use LaTevaWeb\Translatable\Exceptions\AttributeIsNotTranslatable;
9
10
trait Translatable
11
{
12
    public static function create(array $attributes = [])
13
    {
14
        $translatables = [];
15
16
        foreach ($attributes as $field => $values) {
17
            if (in_array($field, self::$translatable)) {
18
                $translatables[$field] = $values;
19
                unset($attributes[$field]);
20
            }
21
        }
22
23
        $model = static::query()->create($attributes);
24
25
        foreach ($translatables as $field => $values) {
26
            foreach ($values as $locale => $value) {
27
                $model->setTranslation($field, $locale, $value);
28
            }
29
        }
30
31
        $model->save();
32
33
        return $model;
34
    }
35
36
    public function getAttributeValue($field)
37
    {
38
        if (! $this->isTranslatableAttribute($field)) {
39
            return parent::getAttributeValue($field);
40
        }
41
42
        return $this->getTranslation($field, $this->getLocale());
43
    }
44
45
    public function setAttribute($field, $value)
46
    {
47
        if (! $this->isTranslatableAttribute($field) || is_array($value)) {
48
            return parent::setAttribute($field, $value);
49
        }
50
51
        return $this->setTranslation($field, $this->getLocale(), $value);
52
    }
53
54
    public function isTranslatableAttribute(string $field) : bool
55
    {
56
        return in_array($field, $this->getTranslatableAttributes());
57
    }
58
59
    protected function getLocale() : string
60
    {
61
        return Config::get('app.locale');
62
    }
63
64
    public function getTranslatableAttributes(): array
65
    {
66
        return is_array(self::$translatable) ? self::$translatable : [];
67
    }
68
69
    public function getTranslation(string $field, string $locale, bool $useFallbackLocale = true): ?string
70
    {
71
        $locale = $this->normalizeLocale($field, $locale, $useFallbackLocale);
72
73
        $translations = $this->getTranslations($field)->all();
74
75
        $translation = $translations[$locale] ?? '';
76
77
        if ($this->hasGetMutator($field)) {
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...
78
            return $this->mutateAttribute($field, $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...
79
        }
80
81
        return $translation;
82
    }
83
84
    public function setTranslation(string $field, string $locale, $content): self
85
    {
86
        $this->guardAgainstNonTranslatableAttribute($field);
87
88
        $translation = $this->translations()
89
                            ->where('field', $field)
90
                            ->where('locale', $locale)
91
                            ->first();
92
93
        if (! empty($translation)) {
94
            $translation->content = $content;
95
            $translation->save();
96
        } else {
97
            $this->translations()->create([
98
                'field' => $field,
99
                'locale' => $locale,
100
                'content' => $content,
101
            ]);
102
        }
103
104
        return $this;
105
    }
106
107
    protected function guardAgainstNonTranslatableAttribute(string $key)
108
    {
109
        if (! $this->isTranslatableAttribute($key)) {
110
            throw AttributeIsNotTranslatable::make($key, $this);
111
        }
112
    }
113
114
    public function translations(): MorphToMany
115
    {
116
        return $this->morphToMany(config('translatable.models.translation'), 'translatable');
0 ignored issues
show
Bug introduced by
It seems like morphToMany() 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...
117
    }
118
119
    protected function normalizeLocale(string $field, string $locale, bool $useFallbackLocale) : string
120
    {
121
        if (in_array($locale, $this->getTranslatedLocales($field)->all())) {
122
            return $locale;
123
        }
124
        if (! $useFallbackLocale) {
125
            return $locale;
126
        }
127
        if (! is_null($fallbackLocale = Config::get('app.fallback_locale'))) {
128
            return $fallbackLocale;
129
        }
130
131
        return $locale;
132
    }
133
134
    /**
135
     * Returns a collection with all locales [ locales ].
136
     *
137
     * @param string $field
138
     * @return Collection
139
     */
140
    public function getTranslatedLocales(string $field): Collection
141
    {
142
        return $this->getTranslations($field)->keys();
143
    }
144
145
    /**
146
     * Returns a collection with [ locale => content ].
147
     *
148
     * @param string|null $field
149
     * @return Collection
150
     */
151
    public function getTranslations(string $field = null): Collection
152
    {
153
        return $this->translations()
154
                    ->where('field', $field)
155
                    ->select('locale', 'content')
156
                    ->get()
157
                    ->keyBy('locale')
158
                    ->pluck('content', 'locale');
159
    }
160
}
161