1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BBSLab\NovaTranslation\Models; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Collection; |
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
7
|
|
|
use Illuminate\Database\Eloquent\Relations\Relation; |
8
|
|
|
use Illuminate\Database\Query\Builder; |
9
|
|
|
use Illuminate\Support\Facades\DB; |
10
|
|
|
|
11
|
|
|
class TranslationRelation extends Relation |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var \BBSLab\NovaTranslation\Models\Translation|\Illuminate\Database\Eloquent\Builder |
15
|
|
|
*/ |
16
|
|
|
protected $query; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var \BBSLab\NovaTranslation\Models\Contracts\IsTranslatable |
20
|
|
|
*/ |
21
|
|
|
protected $parent; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var \BBSLab\NovaTranslation\Models\Translation |
25
|
|
|
*/ |
26
|
|
|
protected $related; |
27
|
|
|
|
28
|
|
|
public function __construct(Model $parent) |
29
|
|
|
{ |
30
|
|
|
parent::__construct(Translation::query(), $parent); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* {@inheritdoc} |
35
|
|
|
*/ |
36
|
|
|
public function addConstraints() |
37
|
|
|
{ |
38
|
|
|
if (static::$constraints) { |
39
|
|
|
$this->query |
40
|
|
View Code Duplication |
->whereExists(function (Builder $query) { |
|
|
|
|
41
|
|
|
$query->select(DB::raw(1)) |
42
|
|
|
->from('translations as original') |
43
|
|
|
->whereRaw('original.translation_id = translations.translation_id') |
44
|
|
|
->where('original.translatable_id', '=', $this->parent->getKey()) |
45
|
|
|
->where('original.translatable_type', '=', get_class($this->parent)); |
46
|
|
|
}) |
47
|
|
|
->where('translatable_type', '=', get_class($this->parent)) |
48
|
|
|
->where('translatable_id', '<>', $this->parent->getKey()); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
|
|
public function addEagerConstraints(array $models) |
56
|
|
|
{ |
57
|
|
|
$ids = collect($models)->pluck($this->parent->getKeyName()); |
58
|
|
|
|
59
|
|
|
$this->query |
60
|
|
View Code Duplication |
->whereExists(function (Builder $query) use ($ids) { |
|
|
|
|
61
|
|
|
$query->select(DB::raw(1)) |
62
|
|
|
->from('translations as original') |
63
|
|
|
->whereRaw('translations.translation_id = original.translation_id') |
64
|
|
|
->whereIn('original.translatable_id', $ids) |
65
|
|
|
->where('original.translatable_type', '=', get_class($this->parent)); |
66
|
|
|
}) |
67
|
|
|
->where('translatable_type', '=', get_class($this->parent)) |
68
|
|
|
->whereNotIn('translatable_id', $ids); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritdoc} |
73
|
|
|
*/ |
74
|
|
|
public function initRelation(array $models, $relation) |
75
|
|
|
{ |
76
|
|
|
foreach ($models as $model) { |
77
|
|
|
/** @var \BBSLab\NovaTranslation\Models\Contracts\IsTranslatable $model */ |
78
|
|
|
$model->setRelation($relation, $this->related->newCollection()); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $models; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* {@inheritdoc} |
86
|
|
|
*/ |
87
|
|
|
public function match(array $models, Collection $results, $relation) |
88
|
|
|
{ |
89
|
|
|
if ($results->isEmpty()) { |
90
|
|
|
return $models; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
foreach ($models as $model) { |
94
|
|
|
/** @var \BBSLab\NovaTranslation\Models\Contracts\IsTranslatable $model */ |
95
|
|
|
$model->setRelation( |
96
|
|
|
$relation, |
97
|
|
|
$results->filter(function (Translation $translation) use ($model) { |
98
|
|
|
return $translation->translation_id === $model->translation->translation_id |
|
|
|
|
99
|
|
|
&& $translation->translatable_id !== $model->getKey(); |
100
|
|
|
}) |
101
|
|
|
); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $models; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* {@inheritdoc} |
109
|
|
|
*/ |
110
|
|
|
public function getResults() |
111
|
|
|
{ |
112
|
|
|
return ! is_null($this->getParent()->getKey()) |
113
|
|
|
? $this->query->get() |
|
|
|
|
114
|
|
|
: $this->related->newCollection(); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.