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