Completed
Push — master ( 44c56f...6ae68b )
by Iman
02:36
created

DynamicRelations::defineRelation()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

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