1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Imanghafoori\Relativity; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Illuminate\Support\Str; |
7
|
|
|
|
8
|
|
|
trait DynamicRelations |
9
|
|
|
{ |
10
|
|
|
use BaseEloquentOverrides; |
11
|
|
|
|
12
|
|
|
protected static $macros = []; |
13
|
|
|
|
14
|
5 |
|
public static function macro($name, $macro) |
15
|
|
|
{ |
16
|
5 |
|
static::$macros[$name] = $macro; |
17
|
5 |
|
} |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Dynamically handle calls to the class. |
21
|
|
|
* |
22
|
|
|
* @param string $method |
23
|
|
|
* @param array $parameters |
24
|
|
|
* @return mixed |
25
|
|
|
* |
26
|
|
|
* @throws \BadMethodCallException |
27
|
|
|
*/ |
28
|
5 |
|
public function __call($method, $parameters) |
29
|
|
|
{ |
30
|
5 |
|
$macro = static::$macros[$method] ?? null; |
31
|
5 |
|
if (! $macro) { |
32
|
5 |
|
return parent::__call($method, $parameters); |
33
|
|
|
} |
34
|
|
|
|
35
|
5 |
|
if ($macro instanceof Closure) { |
36
|
5 |
|
$macro = $macro->bindTo($this, static::class); |
37
|
|
|
} |
38
|
|
|
|
39
|
5 |
|
return call_user_func_array($macro, $parameters); |
40
|
|
|
} |
41
|
|
|
|
42
|
1 |
|
public static function morphed_by_many($relationName, $related, $name, $table = null, $foreignPivotKey = null, |
43
|
|
|
$relatedPivotKey = null, $parentKey = null, $relatedKey = null) |
44
|
|
|
{ |
45
|
1 |
|
return new AbstractRelation(['morphedByMany', static::class, $relationName, [$related, $name, $table, $foreignPivotKey, |
46
|
1 |
|
$relatedPivotKey, $parentKey, $relatedKey, $relationName]]); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param string $related |
51
|
|
|
* @param $relationName |
52
|
|
|
* @param null $foreignKey |
53
|
|
|
* @param null $localKey |
54
|
|
|
* |
55
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
56
|
|
|
*/ |
57
|
1 |
|
public static function has_many($relationName = null, string $related, $foreignKey = null, $localKey = null) |
58
|
|
|
{ |
59
|
1 |
|
return new AbstractRelation(['hasMany', static::class, $relationName, [$related, $foreignKey, $localKey]]); |
60
|
|
|
} |
61
|
|
|
|
62
|
1 |
|
public static function has_one($relationName = null, $related, $foreignKey = null, $localKey = null) |
63
|
|
|
{ |
64
|
1 |
|
return new AbstractRelation(['hasOne', static::class, $relationName, [$related, $foreignKey, $localKey]]); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param string $related |
69
|
|
|
* @param $relationName |
70
|
|
|
* @param null $foreignKey |
71
|
|
|
* @param null $ownerKey |
72
|
|
|
* |
73
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
74
|
|
|
*/ |
75
|
1 |
|
public static function belongs_to($relationName = null, string $related, $foreignKey = null, $ownerKey = null) |
76
|
|
|
{ |
77
|
1 |
|
if (is_null($foreignKey)) { |
78
|
1 |
|
$foreignKey = Str::snake($relationName).'_id'; |
79
|
|
|
} |
80
|
|
|
|
81
|
1 |
|
return new AbstractRelation(['belongsTo', static::class, $relationName, [$related, $foreignKey, $ownerKey, $relationName]]); |
82
|
|
|
} |
83
|
|
|
|
84
|
1 |
View Code Duplication |
public static function belongs_to_many($relationName, $related, $table = null, $foreignPivotKey = null, $relatedPivotKey = null, |
|
|
|
|
85
|
|
|
$parentKey = null, $relatedKey = null) |
86
|
|
|
{ |
87
|
1 |
|
$params = [$related, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey, $relationName]; |
88
|
|
|
|
89
|
1 |
|
return new AbstractRelation(['belongsToMany', static::class, $relationName, $params]); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Define a polymorphic many-to-many relationship. |
94
|
|
|
* |
95
|
|
|
* @param string $relationName |
96
|
|
|
* @param string $related |
97
|
|
|
* @param string $name |
98
|
|
|
* @param string $table |
99
|
|
|
* @param string $foreignPivotKey |
100
|
|
|
* @param string $relatedPivotKey |
101
|
|
|
* @param string $parentKey |
102
|
|
|
* @param string $relatedKey |
103
|
|
|
* @param bool $inverse |
|
|
|
|
104
|
|
|
* |
105
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphToMany |
106
|
|
|
*/ |
107
|
1 |
View Code Duplication |
public static function morph_to_many($relationName, $related, $name, $table = null, $foreignPivotKey = null, |
|
|
|
|
108
|
|
|
$relatedPivotKey = null, $parentKey = null, $relatedKey = null) |
109
|
|
|
{ |
110
|
1 |
|
$params = [$related, $name, $table, $foreignPivotKey, |
111
|
1 |
|
$relatedPivotKey, $parentKey, $relatedKey, $relationName]; |
112
|
|
|
|
113
|
1 |
|
return new AbstractRelation(['morphToMany', static::class, $relationName, $params]); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Define a polymorphic one-to-many relationship. |
118
|
|
|
* |
119
|
|
|
* @param string $relationName |
120
|
|
|
* @param string $related |
121
|
|
|
* @param string $name |
122
|
|
|
* @param string $type |
123
|
|
|
* @param string $id |
124
|
|
|
* @param string $localKey |
125
|
|
|
* |
126
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
127
|
|
|
*/ |
128
|
1 |
|
public static function morph_many($relationName, $related, $name, $type = null, $id = null, $localKey = null) |
129
|
|
|
{ |
130
|
1 |
|
$params = [$related, $name, $type, $id, $localKey]; |
131
|
|
|
|
132
|
1 |
|
return new AbstractRelation(['morphMany', static::class, $relationName, $params]); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public static function morph_one($relationName, $related, $name, $type = null, $id = null, $localKey = null) |
136
|
|
|
{ |
137
|
|
|
$params = [$related, $name, $type, $id, $localKey]; |
138
|
|
|
return new AbstractRelation(['morphOne', static::class, $relationName, $params]); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public static function morph_to($relationName, $type = null, $id = null, $ownerKey = null) |
142
|
|
|
{ |
143
|
|
|
$params = [$relationName, $type, $id, $ownerKey]; |
144
|
|
|
|
145
|
|
|
return new AbstractRelation(['morphTo', static::class, $relationName, $params]); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public static function forceEagerLoading(...$relation) |
149
|
|
|
{ |
150
|
|
|
static::registerModelEvent('booting', function ($model) use ($relation) { |
151
|
|
|
$model->with = $model->with + $relation; |
152
|
|
|
}); |
153
|
|
|
} |
154
|
|
|
} |
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.