1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Imanghafoori\Relativity; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
6
|
|
|
|
7
|
|
|
trait DynamicRelations |
8
|
|
|
{ |
9
|
|
|
use BaseEloquentOverrides; |
10
|
|
|
|
11
|
|
|
protected static $dynamicRelations = []; |
12
|
|
|
|
13
|
|
|
public function getDynamicRelations() |
14
|
|
|
{ |
15
|
|
|
return static::$dynamicRelations; |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
public function hasDynamicRelation(string $relation) |
19
|
|
|
{ |
20
|
|
|
return isset(static::$dynamicRelations[$relation]); |
21
|
|
|
} |
22
|
|
|
|
23
|
7 |
|
public static function defineRelation($relationType, $relationName, $data, $constraints) |
24
|
|
|
{ |
25
|
|
|
$method = function () use ($relationType, $data, $constraints) { |
26
|
7 |
|
$relation = $this->{$relationType} (...$data); |
27
|
7 |
|
foreach ($constraints as $cons) { |
28
|
1 |
|
$relation = $relation->{$cons[0]}(...$cons[1]); |
29
|
|
|
} |
30
|
|
|
|
31
|
7 |
|
return $relation; |
32
|
7 |
|
}; |
33
|
|
|
|
34
|
7 |
|
static::$dynamicRelations[$relationName] = $method; |
35
|
7 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Dynamically handle calls to the class. |
39
|
|
|
* |
40
|
|
|
* @param string $method |
41
|
|
|
* @param array $parameters |
42
|
|
|
* @return mixed |
43
|
|
|
* |
44
|
|
|
* @throws \BadMethodCallException |
45
|
|
|
*/ |
46
|
7 |
|
public function __call($method, $parameters) |
47
|
|
|
{ |
48
|
7 |
|
$dynamicRelation = static::$dynamicRelations[$method] ?? null; |
49
|
7 |
|
if (! $dynamicRelation) { |
50
|
7 |
|
return parent::__call($method, $parameters); |
51
|
|
|
} |
52
|
|
|
|
53
|
7 |
|
return call_user_func_array($dynamicRelation->bindTo($this, static::class), $parameters); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Define a polymorphic, inverse many-to-many relationship. |
58
|
|
|
* |
59
|
|
|
* @param string $relationName |
60
|
|
|
* @param string $related |
61
|
|
|
* @param string $name |
62
|
|
|
* @param string $table |
63
|
|
|
* @param string $foreignPivotKey |
64
|
|
|
* @param string $relatedPivotKey |
65
|
|
|
* @param string $parentKey |
66
|
|
|
* @param string $relatedKey |
67
|
|
|
* |
68
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphToMany |
69
|
|
|
*/ |
70
|
1 |
|
public static function morphed_by_many($relationName, $related, $name, $table = null, $foreignPivotKey = null, |
71
|
|
|
$relatedPivotKey = null, $parentKey = null, $relatedKey = null) |
72
|
|
|
{ |
73
|
1 |
|
return new AbstractRelation(['morphedByMany', static::class, $relationName, [$related, $name, $table, $foreignPivotKey, |
74
|
1 |
|
$relatedPivotKey, $parentKey, $relatedKey, $relationName, ]]); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param string $relationName |
79
|
|
|
* @param string $related |
80
|
|
|
* @param null $foreignKey |
81
|
|
|
* @param null $localKey |
82
|
|
|
* |
83
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
84
|
|
|
*/ |
85
|
2 |
|
public static function has_many($relationName, $related, $foreignKey = null, $localKey = null) |
86
|
|
|
{ |
87
|
2 |
|
return new AbstractRelation(['hasMany', static::class, $relationName, [$related, $foreignKey, $localKey]]); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Define a one-to-one relationship. |
92
|
|
|
* |
93
|
|
|
* @param string $relationName |
94
|
|
|
* @param string $related |
95
|
|
|
* @param string $foreignKey |
96
|
|
|
* @param string $localKey |
97
|
|
|
* |
98
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasOne |
99
|
|
|
*/ |
100
|
1 |
|
public static function has_one($relationName, $related, $foreignKey = null, $localKey = null) |
101
|
|
|
{ |
102
|
1 |
|
return new AbstractRelation(['hasOne', static::class, $relationName, [$related, $foreignKey, $localKey]]); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param string $relationName |
107
|
|
|
* @param string $related |
108
|
|
|
* @param null $foreignKey |
109
|
|
|
* @param null $ownerKey |
110
|
|
|
* |
111
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
112
|
|
|
*/ |
113
|
1 |
|
public static function belongs_to($relationName, string $related, $foreignKey = null, $ownerKey = null) |
114
|
|
|
{ |
115
|
1 |
|
if (is_null($foreignKey)) { |
116
|
|
|
$foreignKey = Str::snake($relationName).'_id'; |
117
|
|
|
} |
118
|
|
|
|
119
|
1 |
|
return new AbstractRelation(['belongsTo', static::class, $relationName, [$related, $foreignKey, $ownerKey, $relationName]]); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Define a many-to-many relationship. |
124
|
|
|
* |
125
|
|
|
* @param string $relationName |
126
|
|
|
* @param string $related |
127
|
|
|
* @param string $table |
128
|
|
|
* @param string $foreignPivotKey |
129
|
|
|
* @param string $relatedPivotKey |
130
|
|
|
* @param string $parentKey |
131
|
|
|
* @param string $relatedKey |
132
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
133
|
|
|
*/ |
134
|
1 |
|
public static function belongs_to_many($relationName, $related, $table = null, $foreignPivotKey = null, $relatedPivotKey = null, |
135
|
|
|
$parentKey = null, $relatedKey = null) |
136
|
|
|
{ |
137
|
1 |
|
$params = [$related, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey, $relationName]; |
138
|
|
|
|
139
|
1 |
|
return new AbstractRelation(['belongsToMany', static::class, $relationName, $params]); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Define a polymorphic many-to-many relationship. |
144
|
|
|
* |
145
|
|
|
* @param string $relationName |
146
|
|
|
* @param string $related |
147
|
|
|
* @param string $name |
148
|
|
|
* @param string $table |
149
|
|
|
* @param string $foreignPivotKey |
150
|
|
|
* @param string $relatedPivotKey |
151
|
|
|
* @param string $parentKey |
152
|
|
|
* @param string $relatedKey |
153
|
|
|
* @param bool $inverse |
154
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphToMany |
155
|
|
|
*/ |
156
|
1 |
|
public static function morph_to_many($relationName, $related, $name, $table = null, $foreignPivotKey = null, |
157
|
|
|
$relatedPivotKey = null, $parentKey = null, |
158
|
|
|
$relatedKey = null, $inverse = false) |
159
|
|
|
{ |
160
|
1 |
|
$params = [$related, $name, $table, $foreignPivotKey, |
161
|
1 |
|
$relatedPivotKey, $parentKey, $relatedKey, $inverse, $relationName, ]; |
162
|
|
|
|
163
|
1 |
|
return new AbstractRelation(['morphToMany', static::class, $relationName, $params]); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Define a polymorphic one-to-many relationship. |
168
|
|
|
* |
169
|
|
|
* @param string $relationName |
170
|
|
|
* @param string $related |
171
|
|
|
* @param string $name |
172
|
|
|
* @param string $type |
173
|
|
|
* @param string $id |
174
|
|
|
* @param string $localKey |
175
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
176
|
|
|
*/ |
177
|
1 |
|
public static function morph_many($relationName, $related, $name, $type = null, $id = null, $localKey = null) |
178
|
|
|
{ |
179
|
1 |
|
$params = [$related, $name, $type, $id, $localKey]; |
180
|
|
|
|
181
|
1 |
|
return new AbstractRelation(['morphMany', static::class, $relationName, $params]); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Define a polymorphic one-to-one relationship. |
186
|
|
|
* |
187
|
|
|
* @param string $relationName |
188
|
|
|
* @param string $related |
189
|
|
|
* @param string $name |
190
|
|
|
* @param string $type |
191
|
|
|
* @param string $id |
192
|
|
|
* @param string $localKey |
193
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphOne |
194
|
|
|
*/ |
195
|
|
|
public static function morph_one($relationName, $related, $name, $type = null, $id = null, $localKey = null) |
196
|
|
|
{ |
197
|
|
|
$params = [$related, $name, $type, $id, $localKey]; |
198
|
|
|
|
199
|
|
|
return new AbstractRelation(['morphOne', static::class, $relationName, $params]); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Define a polymorphic, inverse one-to-one or many relationship. |
204
|
|
|
* |
205
|
|
|
* @param string $relationName |
206
|
|
|
* @param string $type |
207
|
|
|
* @param string $id |
208
|
|
|
* @param string $ownerKey |
209
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphTo |
210
|
|
|
*/ |
211
|
1 |
|
public static function morph_to($relationName, $type = null, $id = null, $ownerKey = null) |
212
|
|
|
{ |
213
|
1 |
|
$params = [$relationName, $type, $id, $ownerKey]; |
214
|
|
|
|
215
|
1 |
|
return new AbstractRelation(['morphTo', static::class, $relationName, $params]); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Define a has-many-through relationship. |
220
|
|
|
* |
221
|
|
|
* @param string $relationName |
222
|
|
|
* @param string $related |
223
|
|
|
* @param string $through |
224
|
|
|
* @param string|null $firstKey |
225
|
|
|
* @param string|null $secondKey |
226
|
|
|
* @param string|null $localKey |
227
|
|
|
* @param string|null $secondLocalKey |
228
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasManyThrough |
229
|
|
|
*/ |
230
|
1 |
|
public static function has_many_through($relationName, $related, $through, $firstKey = null, $secondKey = null, $localKey = null, $secondLocalKey = null) |
231
|
|
|
{ |
232
|
1 |
|
$params = [$related, $through, $firstKey, $secondKey, $localKey, $secondLocalKey]; |
233
|
|
|
|
234
|
1 |
|
return new AbstractRelation(['hasManyThrough', static::class, $relationName, $params]); |
235
|
|
|
} |
236
|
|
|
|
237
|
1 |
|
public static function forceEagerLoading(...$relation) |
238
|
|
|
{ |
239
|
|
|
static::registerModelEvent('booting', function ($model) use ($relation) { |
240
|
1 |
|
$model->with = $model->with + $relation; |
241
|
1 |
|
}); |
242
|
1 |
|
} |
243
|
|
|
} |
244
|
|
|
|