1 | <?php |
||||||
2 | |||||||
3 | namespace Fomvasss\LaravelTranslatable\Traits; |
||||||
4 | |||||||
5 | use Illuminate\Database\Eloquent\Relations\HasMany; |
||||||
6 | use Illuminate\Support\Str; |
||||||
7 | use Illuminate\Support\Collection; |
||||||
8 | |||||||
9 | trait HasTranslations |
||||||
10 | { |
||||||
11 | use HasLanguage; |
||||||
12 | |||||||
13 | protected static function bootHasTranslations() |
||||||
14 | { |
||||||
15 | static::saved(function ($model) { |
||||||
16 | if ($model->{$model->getLangcodeColumn()} && empty($model->{$model->getTranslationUuidColumn()})) { |
||||||
17 | $model->{$model->getTranslationUuidColumn()} = Str::uuid(); |
||||||
18 | $model->save(); |
||||||
19 | } |
||||||
20 | }); |
||||||
21 | } |
||||||
22 | |||||||
23 | /** |
||||||
24 | * @return string |
||||||
25 | */ |
||||||
26 | public function getTranslationUuidColumn(): string |
||||||
27 | { |
||||||
28 | return config('translatable.db.columns.translation_uuid'); |
||||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||||
29 | } |
||||||
30 | |||||||
31 | /** |
||||||
32 | * @return \Illuminate\Database\Eloquent\Relations\HasMany |
||||||
33 | */ |
||||||
34 | public function translations(): HasMany |
||||||
35 | { |
||||||
36 | return $this->hasMany(static::class, $this->getTranslationUuidColumn(), $this->getTranslationUuidColumn()); |
||||||
0 ignored issues
–
show
It seems like
hasMany() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
37 | } |
||||||
38 | |||||||
39 | public function saveTranslatable(string $langcode, ?string $uuid = null) |
||||||
40 | { |
||||||
41 | $this->setAttribute($this->getLangcodeColumn(), $langcode); |
||||||
0 ignored issues
–
show
It seems like
setAttribute() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
42 | if ($uuid) { |
||||||
43 | $this->setAttribute($this->getTranslationUuidColumn(), $uuid); |
||||||
44 | } |
||||||
45 | |||||||
46 | return $this->save(); |
||||||
0 ignored issues
–
show
It seems like
save() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
47 | } |
||||||
48 | |||||||
49 | /** |
||||||
50 | * @return Collection |
||||||
51 | */ |
||||||
52 | public function getTranslationList(): Collection |
||||||
53 | { |
||||||
54 | $languages = config('translatable.languages', []); |
||||||
0 ignored issues
–
show
The function
config was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
55 | $translations = $this->translations; |
||||||
56 | $result = new Collection(); |
||||||
57 | |||||||
58 | foreach ($languages as $langcode => $language) { |
||||||
59 | $model = $translations->where($this->getLangcodeColumn(), $langcode)->first(); |
||||||
60 | $result->push([ |
||||||
61 | 'model_key' => optional($model)->{$this->getKeyName()}, |
||||||
0 ignored issues
–
show
It seems like
getKeyName() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
62 | 'model_type' => Str::snake(class_basename(static::class)), |
||||||
63 | 'translation_uuid' => optional($model)->{$this->getTranslationUuidColumn()}, |
||||||
64 | 'langcode' => $langcode, |
||||||
65 | 'language' => $language, |
||||||
66 | 'status' => $this->getTranslationStatus($langcode, $model) |
||||||
67 | ]); |
||||||
68 | } |
||||||
69 | |||||||
70 | return $result; |
||||||
71 | } |
||||||
72 | |||||||
73 | /** |
||||||
74 | * @param string $langcode |
||||||
75 | * @param null $model |
||||||
0 ignored issues
–
show
|
|||||||
76 | * @return string |
||||||
77 | */ |
||||||
78 | protected function getTranslationStatus(string $langcode, $model = null): string |
||||||
79 | { |
||||||
80 | if (empty($model)) { |
||||||
81 | return 'empty'; |
||||||
82 | } |
||||||
83 | |||||||
84 | if ($this->{$this->getLangcodeColumn()} === $langcode) { |
||||||
85 | return 'current'; |
||||||
86 | } |
||||||
87 | |||||||
88 | return 'translation'; |
||||||
89 | } |
||||||
90 | } |