Passed
Push — master ( a29ec7...ef55bd )
by Iman
02:37
created

DynamicRelations::has_many()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 4
crap 1
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 6
    public static function defineRelation($relationType, $relationName, $data, $constraints)
14
    {
15
        $method = function () use ($relationType, $data, $constraints) {
16 6
            $relation = $this->{$relationType} (...$data);
17 6
            foreach ($constraints as $cons) {
18 1
                $relation = $relation->{$cons[0]}(...$cons[1]);
19
            }
20
21 6
            return $relation;
22 6
        };
23
24 6
        static::$dynamicRelations[$relationName] = $method;
25 6
    }
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 6
    public function __call($method, $parameters)
37
    {
38 6
        $dynamicRelation = static::$dynamicRelations[$method] ?? null;
39 6
        if (! $dynamicRelation) {
40 6
            return parent::__call($method, $parameters);
41
        }
42
43 6
        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 1
    public static function has_one($relationName, $related, $foreignKey = null, $localKey = null)
67
    {
68 1
        return new AbstractRelation(['hasOne', static::class, $relationName, [$related, $foreignKey, $localKey]]);
69
    }
70
71
    /**
72
     * @param string $related
73
     * @param $relationName
74
     * @param null $foreignKey
75
     * @param null $ownerKey
76
     *
77
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
78
     */
79 1
    public static function belongs_to($relationName, string $related, $foreignKey = null, $ownerKey = null)
80
    {
81 1
        if (is_null($foreignKey)) {
82
            $foreignKey = Str::snake($relationName).'_id';
83
        }
84
85 1
        return new AbstractRelation(['belongsTo', static::class, $relationName, [$related, $foreignKey, $ownerKey, $relationName]]);
86
    }
87
88 1
    public static function belongs_to_many($relationName, $related, $table = null, $foreignPivotKey = null, $relatedPivotKey = null,
89
        $parentKey = null, $relatedKey = null)
90
    {
91 1
        $params = [$related, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey, $relationName];
92
93 1
        return new AbstractRelation(['belongsToMany', static::class, $relationName, $params]);
94
    }
95
96
    /**
97
     * Define a polymorphic many-to-many relationship.
98
     *
99
     * @param  string  $relationName
100
     * @param  string  $related
101
     * @param  string  $name
102
     * @param  string  $table
103
     * @param  string  $foreignPivotKey
104
     * @param  string  $relatedPivotKey
105
     * @param  string  $parentKey
106
     * @param  string  $relatedKey
107
     * @param  bool  $inverse
108
     *
109
     * @return \Illuminate\Database\Eloquent\Relations\MorphToMany
110
     */
111 1
    public static function morph_to_many($relationName, $related, $name, $table = null, $foreignPivotKey = null,
112
        $relatedPivotKey = null, $parentKey = null,
113
        $relatedKey = null, $inverse = false)
114
    {
115 1
        $params = [$related, $name, $table, $foreignPivotKey,
116 1
            $relatedPivotKey, $parentKey, $relatedKey, $inverse, $relationName, ];
117
118 1
        return new AbstractRelation(['morphToMany', static::class, $relationName, $params]);
119
    }
120
121
    /**
122
     * Define a polymorphic one-to-many relationship.
123
     *
124
     * @param  string  $relationName
125
     * @param  string  $related
126
     * @param  string  $name
127
     * @param  string  $type
128
     * @param  string  $id
129
     * @param  string  $localKey
130
     *
131
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
132
     */
133 1
    public static function morph_many($relationName, $related, $name, $type = null, $id = null, $localKey = null)
134
    {
135 1
        $params = [$related, $name, $type, $id, $localKey];
136
137 1
        return new AbstractRelation(['morphMany', static::class, $relationName, $params]);
138
    }
139
140
    public static function morph_one($relationName, $related, $name, $type = null, $id = null, $localKey = null)
141
    {
142
        $params = [$related, $name, $type, $id, $localKey];
143
144
        return new AbstractRelation(['morphOne', static::class, $relationName, $params]);
145
    }
146
147 1
    public static function morph_to($relationName, $type = null, $id = null, $ownerKey = null)
148
    {
149 1
        $params = [$relationName, $type, $id, $ownerKey];
150
151 1
        return new AbstractRelation(['morphTo', static::class, $relationName, $params]);
152
    }
153
154
    /**
155
     * Define a has-many-through relationship.
156
     *
157
     * @param string $relationName
158
     * @param string $related
159
     * @param string $through
160
     * @param string|null $firstKey
161
     * @param string|null $secondKey
162
     * @param string|null $localKey
163
     * @param string|null $secondLocalKey
164
     *
165
     * @return \Imanghafoori\Relativity\AbstractRelation
166
     */
167
    public static function has_many_through($relationName, $related, $through, $firstKey = null, $secondKey = null, $localKey = null, $secondLocalKey = null)
168
    {
169
        $params = [$related, $through, $firstKey, $secondKey, $localKey, $secondLocalKey];
170
171
        return new AbstractRelation(['hasManyThrough', static::class, $relationName, $params]);
172
    }
173
174 1
    public static function forceEagerLoading(...$relation)
175
    {
176
        static::registerModelEvent('booting', function ($model) use ($relation) {
177 1
            $model->with = $model->with + $relation;
178 1
        });
179 1
    }
180
}
181