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
|
1 |
|
public static function create(array $attributes = []) |
13
|
|
|
{ |
14
|
1 |
|
$translatables = []; |
15
|
|
|
|
16
|
1 |
|
foreach ($attributes as $field => $values) { |
17
|
1 |
|
if (in_array($field, self::$translatable)) { |
18
|
1 |
|
$translatables[$field] = $values; |
19
|
1 |
|
unset($attributes[$field]); |
20
|
|
|
} |
21
|
|
|
} |
22
|
|
|
|
23
|
1 |
|
$model = static::query()->create($attributes); |
24
|
|
|
|
25
|
1 |
|
foreach ($translatables as $field => $values) { |
26
|
1 |
|
foreach ($values as $locale => $value) { |
27
|
1 |
|
$model->setTranslation($field, $locale, $value); |
28
|
|
|
} |
29
|
|
|
} |
30
|
|
|
|
31
|
1 |
|
$model->save(); |
32
|
|
|
|
33
|
1 |
|
return $model; |
34
|
|
|
} |
35
|
|
|
|
36
|
8 |
|
public function getAttributeValue($field) |
37
|
|
|
{ |
38
|
8 |
|
if (! $this->isTranslatableAttribute($field)) { |
39
|
8 |
|
return parent::getAttributeValue($field); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
return $this->getTranslation($field, $this->getLocale()); |
43
|
|
|
} |
44
|
|
|
|
45
|
8 |
|
public function setAttribute($field, $value) |
46
|
|
|
{ |
47
|
8 |
|
if (! $this->isTranslatableAttribute($field) || is_array($value)) { |
48
|
8 |
|
return parent::setAttribute($field, $value); |
49
|
|
|
} |
50
|
|
|
|
51
|
1 |
|
return $this->setTranslation($field, $this->getLocale(), $value); |
52
|
|
|
} |
53
|
|
|
|
54
|
8 |
|
public function isTranslatableAttribute(string $field) : bool |
55
|
|
|
{ |
56
|
8 |
|
return in_array($field, $this->getTranslatableAttributes()); |
57
|
|
|
} |
58
|
|
|
|
59
|
1 |
|
protected function getLocale() : string |
60
|
|
|
{ |
61
|
1 |
|
return Config::get('app.locale'); |
62
|
|
|
} |
63
|
|
|
|
64
|
8 |
|
public function getTranslatableAttributes(): array |
65
|
|
|
{ |
66
|
8 |
|
return is_array(self::$translatable) ? self::$translatable : []; |
67
|
|
|
} |
68
|
|
|
|
69
|
3 |
|
public function getTranslation(string $field, string $locale, bool $useFallbackLocale = true): ?string |
70
|
|
|
{ |
71
|
3 |
|
$locale = $this->normalizeLocale($field, $locale, $useFallbackLocale); |
72
|
|
|
|
73
|
3 |
|
$translations = $this->getTranslations($field)->all(); |
74
|
|
|
|
75
|
3 |
|
$translation = $translations[$locale] ?? ''; |
76
|
|
|
|
77
|
3 |
|
if ($this->hasGetMutator($field)) { |
|
|
|
|
78
|
|
|
return $this->mutateAttribute($field, $translation); |
|
|
|
|
79
|
|
|
} |
80
|
|
|
|
81
|
3 |
|
return $translation; |
82
|
|
|
} |
83
|
|
|
|
84
|
8 |
|
public function setTranslation(string $field, string $locale, $content): self |
85
|
|
|
{ |
86
|
8 |
|
$this->guardAgainstNonTranslatableAttribute($field); |
87
|
|
|
|
88
|
8 |
|
$translation = $this->translations() |
89
|
8 |
|
->where('field', $field) |
90
|
8 |
|
->where('locale', $locale) |
91
|
8 |
|
->first(); |
92
|
|
|
|
93
|
8 |
|
if (! empty($translation)) { |
94
|
|
|
$translation->content = $content; |
95
|
|
|
$translation->save(); |
96
|
|
|
} else { |
97
|
8 |
|
$this->translations()->create([ |
98
|
8 |
|
'field' => $field, |
99
|
8 |
|
'locale' => $locale, |
100
|
8 |
|
'content' => $content, |
101
|
|
|
]); |
102
|
|
|
} |
103
|
|
|
|
104
|
8 |
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
8 |
|
protected function guardAgainstNonTranslatableAttribute(string $key) |
108
|
|
|
{ |
109
|
8 |
|
if (! $this->isTranslatableAttribute($key)) { |
110
|
|
|
throw AttributeIsNotTranslatable::make($key, $this); |
111
|
|
|
} |
112
|
8 |
|
} |
113
|
|
|
|
114
|
8 |
|
public function translations(): MorphToMany |
115
|
|
|
{ |
116
|
8 |
|
return $this->morphToMany( |
|
|
|
|
117
|
8 |
|
config('latevaweb-translatable.models.translation'), |
118
|
8 |
|
'translatable', |
119
|
8 |
|
config('latevaweb-translatable.table_names.translatables'), |
120
|
8 |
|
config('latevaweb-translatable.column_names.model_morph_key'), |
121
|
8 |
|
'translation_id'); |
122
|
|
|
} |
123
|
|
|
|
124
|
3 |
|
protected function normalizeLocale(string $field, string $locale, bool $useFallbackLocale) : string |
125
|
|
|
{ |
126
|
3 |
|
if (in_array($locale, $this->getTranslatedLocales($field)->all())) { |
127
|
3 |
|
return $locale; |
128
|
|
|
} |
129
|
|
|
if (! $useFallbackLocale) { |
130
|
|
|
return $locale; |
131
|
|
|
} |
132
|
|
|
if (! is_null($fallbackLocale = Config::get('app.fallback_locale'))) { |
133
|
|
|
return $fallbackLocale; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return $locale; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Returns a collection with all locales [ locales ]. |
141
|
|
|
* |
142
|
|
|
* @param string $field |
143
|
|
|
* @return Collection |
144
|
|
|
*/ |
145
|
4 |
|
public function getTranslatedLocales(string $field): Collection |
146
|
|
|
{ |
147
|
4 |
|
return $this->getTranslations($field)->keys(); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Returns a collection with [ locale => content ]. |
152
|
|
|
* |
153
|
|
|
* @param string|null $field |
154
|
|
|
* @return Collection |
155
|
|
|
*/ |
156
|
5 |
|
public function getTranslations(string $field = null): Collection |
157
|
|
|
{ |
158
|
5 |
|
return $this->translations() |
159
|
5 |
|
->where('field', $field) |
160
|
5 |
|
->select('locale', 'content') |
161
|
5 |
|
->get() |
162
|
5 |
|
->keyBy('locale') |
163
|
5 |
|
->pluck('content', 'locale'); |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
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
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. 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.