1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Znck\Eloquent\Traits; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Builder; |
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
7
|
|
|
use Znck\Eloquent\Relations\BelongsToThrough as Relation; |
8
|
|
|
|
9
|
|
|
trait BelongsToThrough |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Define a belongs-to-through relationship. |
13
|
|
|
* |
14
|
|
|
* @template TRelatedModel of \Illuminate\Database\Eloquent\Model |
15
|
|
|
* |
16
|
|
|
* @param class-string<TRelatedModel> $related |
|
|
|
|
17
|
|
|
* @param class-string<\Illuminate\Database\Eloquent\Model>[]|array{0: class-string<\Illuminate\Database\Eloquent\Model>, 1: string}[]|class-string<\Illuminate\Database\Eloquent\Model> $through |
18
|
|
|
* @param string|null $localKey |
19
|
|
|
* @param string $prefix |
20
|
|
|
* @param array<class-string<\Illuminate\Database\Eloquent\Model>, string> $foreignKeyLookup |
21
|
|
|
* @param array<class-string<\Illuminate\Database\Eloquent\Model>, string> $localKeyLookup |
22
|
|
|
* @return \Znck\Eloquent\Relations\BelongsToThrough<TRelatedModel, $this> |
23
|
|
|
*/ |
24
|
|
|
public function belongsToThrough( |
25
|
|
|
$related, |
26
|
|
|
$through, |
27
|
|
|
$localKey = null, |
28
|
|
|
$prefix = '', |
29
|
|
|
$foreignKeyLookup = [], |
30
|
|
|
array $localKeyLookup = [] |
31
|
|
|
) { |
32
|
|
|
/** @var TRelatedModel $relatedInstance */ |
33
|
|
|
$relatedInstance = $this->newRelatedInstance($related); |
|
|
|
|
34
|
|
|
|
35
|
|
|
/** @var list<\Illuminate\Database\Eloquent\Model> $throughParents */ |
36
|
|
|
$throughParents = []; |
37
|
|
|
$foreignKeys = []; |
38
|
|
|
|
39
|
|
|
foreach ((array) $through as $model) { |
40
|
|
|
$foreignKey = null; |
41
|
|
|
|
42
|
|
|
if (is_array($model)) { |
43
|
|
|
/** @var string $foreignKey */ |
44
|
|
|
$foreignKey = $model[1]; |
45
|
|
|
|
46
|
|
|
/** @var class-string<\Illuminate\Database\Eloquent\Model> $model */ |
47
|
|
|
$model = $model[0]; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$instance = $this->belongsToThroughParentInstance($model); |
51
|
|
|
|
52
|
|
|
if ($foreignKey) { |
53
|
|
|
$foreignKeys[$instance->getTable()] = $foreignKey; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$throughParents[] = $instance; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$foreignKeys = array_merge($foreignKeys, $this->mapKeys($foreignKeyLookup)); |
60
|
|
|
|
61
|
|
|
$localKeys = $this->mapKeys($localKeyLookup); |
62
|
|
|
|
63
|
|
|
return $this->newBelongsToThrough( |
64
|
|
|
$relatedInstance->newQuery(), |
65
|
|
|
$this, |
|
|
|
|
66
|
|
|
$throughParents, |
|
|
|
|
67
|
|
|
$localKey, |
68
|
|
|
$prefix, |
69
|
|
|
$foreignKeys, |
70
|
|
|
$localKeys |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Map keys to an associative array where the key is the table name and the value is the key from the lookup. |
76
|
|
|
* |
77
|
|
|
* @param array<class-string<\Illuminate\Database\Eloquent\Model>, string> $keyLookup |
|
|
|
|
78
|
|
|
* @return array<string, string> |
79
|
|
|
*/ |
80
|
|
|
protected function mapKeys(array $keyLookup): array |
81
|
|
|
{ |
82
|
|
|
$keys = []; |
83
|
|
|
|
84
|
|
|
// Iterate over each model and key in the key lookup |
85
|
|
|
foreach ($keyLookup as $model => $key) { |
86
|
|
|
// Create a new instance of the model |
87
|
|
|
$instance = new $model(); |
88
|
|
|
|
89
|
|
|
// Add the table name and key to the keys array |
90
|
|
|
$keys[$instance->getTable()] = $key; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $keys; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Create a through parent instance for a belongs-to-through relationship. |
98
|
|
|
* |
99
|
|
|
* @template TModel of \Illuminate\Database\Eloquent\Model |
100
|
|
|
* |
101
|
|
|
* @param class-string<TModel> $model |
|
|
|
|
102
|
|
|
* @return TModel |
103
|
|
|
*/ |
104
|
|
|
protected function belongsToThroughParentInstance($model) |
105
|
|
|
{ |
106
|
|
|
/** @var array{0: class-string<TModel>, 1?: string} $segments */ |
107
|
|
|
$segments = preg_split('/\s+as\s+/i', $model); |
108
|
|
|
|
109
|
|
|
/** @var TModel $instance */ |
110
|
|
|
$instance = new $segments[0](); |
111
|
|
|
|
112
|
|
|
if (isset($segments[1])) { |
113
|
|
|
$instance->setTable($instance->getTable() . ' as ' . $segments[1]); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $instance; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Instantiate a new BelongsToThrough relationship. |
121
|
|
|
* |
122
|
|
|
* @template TRelatedModel of \Illuminate\Database\Eloquent\Model |
123
|
|
|
* @template TDeclaringModel of \Illuminate\Database\Eloquent\Model |
124
|
|
|
* |
125
|
|
|
* @param \Illuminate\Database\Eloquent\Builder<TRelatedModel> $query |
126
|
|
|
* @param TDeclaringModel $parent |
|
|
|
|
127
|
|
|
* @param list<\Illuminate\Database\Eloquent\Model> $throughParents |
|
|
|
|
128
|
|
|
* @param string|null $localKey |
129
|
|
|
* @param string $prefix |
130
|
|
|
* @param array<string, string> $foreignKeyLookup |
131
|
|
|
* @param array<string, string> $localKeyLookup |
132
|
|
|
* @return \Znck\Eloquent\Relations\BelongsToThrough<TRelatedModel, TDeclaringModel> |
133
|
|
|
*/ |
134
|
|
|
protected function newBelongsToThrough( |
135
|
|
|
Builder $query, |
136
|
|
|
Model $parent, |
137
|
|
|
array $throughParents, |
138
|
|
|
$localKey, |
139
|
|
|
$prefix, |
140
|
|
|
array $foreignKeyLookup, |
141
|
|
|
array $localKeyLookup |
142
|
|
|
) { |
143
|
|
|
return new Relation($query, $parent, $throughParents, $localKey, $prefix, $foreignKeyLookup, $localKeyLookup); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|