1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Rinvex\Support\Traits; |
6
|
|
|
|
7
|
|
|
use Illuminate\Support\Arr; |
8
|
|
|
use Illuminate\Support\Str; |
9
|
|
|
use Spatie\Translatable\Events\TranslationHasBeenSet; |
10
|
|
|
use Spatie\Translatable\HasTranslations as BaseHasTranslations; |
11
|
|
|
|
12
|
|
|
trait HasTranslations |
13
|
|
|
{ |
14
|
|
|
use BaseHasTranslations; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @param string $key |
18
|
|
|
* |
19
|
|
|
* @return mixed |
20
|
|
|
*/ |
21
|
|
|
public function getAttributeValue($key) |
22
|
|
|
{ |
23
|
|
|
if (! $this->isTranslatableAttribute($key)) { |
24
|
|
|
return parent::getAttributeValue($key); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
return $this->getTranslation($key, config('app.locale')) ?: Arr::first($this->getTranslations($key)); |
|
|
|
|
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Get translations. |
32
|
|
|
* |
33
|
|
|
* @param $key |
34
|
|
|
* |
35
|
|
|
* @return array |
36
|
|
|
*/ |
37
|
|
|
public function getTranslations($key): array |
38
|
|
|
{ |
39
|
|
|
$this->guardAgainstNonTranslatableAttribute($key); |
40
|
|
|
$value = json_decode($this->getAttributes()[$key] ?? '' ?: '{}', true); |
41
|
|
|
|
42
|
|
|
// Inject default translation if none supplied |
43
|
|
|
if (! is_array($value)) { |
44
|
|
|
$oldValue = $value; |
45
|
|
|
|
46
|
|
|
if ($this->hasSetMutator($key)) { |
47
|
|
|
$method = 'set'.Str::studly($key).'Attribute'; |
48
|
|
|
$value = $this->{$method}($value); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$value = [$locale = app()->getLocale() => $value]; |
52
|
|
|
|
53
|
|
|
$this->attributes[$key] = $this->asJson($value); |
54
|
|
|
event(new TranslationHasBeenSet($this, $key, $locale, $oldValue, $value)); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $value; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Convert the model's attributes to an array. |
62
|
|
|
* |
63
|
|
|
* @return array |
64
|
|
|
*/ |
65
|
|
|
public function attributesToArray() |
66
|
|
|
{ |
67
|
|
|
$values = array_map(function ($attribute) { |
68
|
|
|
return $this->getTranslation($attribute, config('app.locale')) ?: null; |
69
|
|
|
}, $keys = $this->getTranslatableAttributes()); |
70
|
|
|
|
71
|
|
|
return array_replace(parent::attributesToArray(), array_combine($keys, $values)); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Merge new translatable with existing translatable on the model. |
76
|
|
|
* |
77
|
|
|
* @param array $translatable |
78
|
|
|
* @return void |
79
|
|
|
*/ |
80
|
|
|
public function mergeTranslatable($translatable) |
81
|
|
|
{ |
82
|
|
|
$this->translatable = array_merge($this->translatable, $translatable); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: