Passed
Pull Request — master (#69)
by
unknown
10:33
created

HasJsonRelationships::newHasManyThroughJson()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 7
dl 0
loc 3
rs 10
ccs 0
cts 0
cp 0
crap 2
1
<?php
2
3
namespace Staudenmeir\EloquentJsonRelations;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Database\Eloquent\Relations\BelongsTo;
8
use Illuminate\Database\Eloquent\Relations\HasMany;
9
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
10
use Illuminate\Database\Eloquent\Relations\HasOne;
11
use Illuminate\Database\Eloquent\Relations\HasOneThrough;
12
use Illuminate\Database\Eloquent\Relations\MorphMany;
13
use Illuminate\Database\Eloquent\Relations\MorphOne;
14
use Illuminate\Support\Str;
15
use Staudenmeir\EloquentJsonRelations\Relations\BelongsToJson;
16
use Staudenmeir\EloquentJsonRelations\Relations\HasManyJson;
17
use Staudenmeir\EloquentJsonRelations\Relations\HasManyThroughJson;
18
use Staudenmeir\EloquentJsonRelations\Relations\Postgres\BelongsTo as BelongsToPostgres;
19
use Staudenmeir\EloquentJsonRelations\Relations\Postgres\HasMany as HasManyPostgres;
20
use Staudenmeir\EloquentJsonRelations\Relations\Postgres\HasManyThrough as HasManyThroughPostgres;
21
use Staudenmeir\EloquentJsonRelations\Relations\Postgres\HasOne as HasOnePostgres;
22
use Staudenmeir\EloquentJsonRelations\Relations\Postgres\HasOneThrough as HasOneThroughPostgres;
23
use Staudenmeir\EloquentJsonRelations\Relations\Postgres\MorphMany as MorphManyPostgres;
24
use Staudenmeir\EloquentJsonRelations\Relations\Postgres\MorphOne as MorphOnePostgres;
25
26
trait HasJsonRelationships
27
{
28
    /**
29
     * Get an attribute from the model.
30
     *
31
     * @param string $key
32
     * @return mixed
33 290
     */
34
    public function getAttribute($key)
35 290
    {
36
        $attribute = preg_split('/(->|\[\])/', $key)[0];
37 290
38 284
        if (array_key_exists($attribute, $this->attributes)) {
39
            return $this->getAttributeValue($key);
40
        }
41 214
42
        return parent::getAttribute($key);
43
    }
44
45
    /**
46
     * Get an attribute from the $attributes array.
47
     *
48
     * @param  string  $key
49
     * @return mixed
50 284
     */
51
    public function getAttributeFromArray($key)
52 284
    {
53 28
        if (Str::contains($key, '->')) {
54
            return $this->getAttributeValue($key);
55
        }
56 284
57
        return parent::getAttributeFromArray($key);
58
    }
59
60
    /**
61
     * Get a plain attribute (not a relationship).
62
     *
63
     * @param string $key
64
     * @return mixed
65 284
     */
66
    public function getAttributeValue($key)
67 284
    {
68 165
        if (Str::contains($key, '->')) {
69
            [$key, $path] = explode('->', $key, 2);
70 165
71 3
            if (substr($key, -2) === '[]') {
72
                $key = substr($key, 0, -2);
73 3
74
                $path = '*.'.$path;
75
            }
76 165
77
            $path = str_replace(['->', '[]'], ['.', '.*'], $path);
78 165
79
            return data_get($this->getAttributeValue($key), $path);
80
        }
81 284
82
        return parent::getAttributeValue($key);
83
    }
84
85
    /**
86
     * Instantiate a new HasOne relationship.
87
     *
88
     * @param \Illuminate\Database\Eloquent\Builder $query
89
     * @param \Illuminate\Database\Eloquent\Model $parent
90
     * @param string $foreignKey
91
     * @param string $localKey
92
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
93 24
     */
94
    protected function newHasOne(Builder $query, Model $parent, $foreignKey, $localKey)
95 24
    {
96 6
        if ($query->getConnection()->getDriverName() === 'pgsql') {
97
            return new HasOnePostgres($query, $parent, $foreignKey, $localKey);
98
        }
99 18
100
        return new HasOne($query, $parent, $foreignKey, $localKey);
101
    }
102
103
    /**
104
     * Instantiate a new HasOneThrough relationship.
105
     *
106
     * @param \Illuminate\Database\Eloquent\Builder $query
107
     * @param \Illuminate\Database\Eloquent\Model $farParent
108
     * @param \Illuminate\Database\Eloquent\Model $throughParent
109
     * @param string $firstKey
110
     * @param string $secondKey
111
     * @param string $localKey
112
     * @param string $secondLocalKey
113
     * @return \Illuminate\Database\Eloquent\Relations\HasOneThrough
114 24
     */
115
    protected function newHasOneThrough(Builder $query, Model $farParent, Model $throughParent, $firstKey, $secondKey, $localKey, $secondLocalKey)
116 24
    {
117 6
        if ($query->getConnection()->getDriverName() === 'pgsql') {
118
            return new HasOneThroughPostgres($query, $farParent, $throughParent, $firstKey, $secondKey, $localKey, $secondLocalKey);
119
        }
120 18
121
        return new HasOneThrough($query, $farParent, $throughParent, $firstKey, $secondKey, $localKey, $secondLocalKey);
122
    }
123
124
    /**
125
     * Instantiate a new MorphOne relationship.
126
     *
127
     * @param \Illuminate\Database\Eloquent\Builder $query
128
     * @param \Illuminate\Database\Eloquent\Model $parent
129
     * @param string $type
130
     * @param string $id
131
     * @param string $localKey
132
     * @return \Illuminate\Database\Eloquent\Relations\MorphOne
133 20
     */
134
    protected function newMorphOne(Builder $query, Model $parent, $type, $id, $localKey)
135 20
    {
136 5
        if ($query->getConnection()->getDriverName() === 'pgsql') {
137
            return new MorphOnePostgres($query, $parent, $type, $id, $localKey);
138
        }
139 15
140
        return new MorphOne($query, $parent, $type, $id, $localKey);
141
    }
142
143
    /**
144
     * Instantiate a new BelongsTo relationship.
145
     *
146
     * @param \Illuminate\Database\Eloquent\Builder $query
147
     * @param \Illuminate\Database\Eloquent\Model $child
148
     * @param string $foreignKey
149
     * @param string $ownerKey
150
     * @param string $relation
151
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
152 36
     */
153
    protected function newBelongsTo(Builder $query, Model $child, $foreignKey, $ownerKey, $relation)
154 36
    {
155 9
        if ($query->getConnection()->getDriverName() === 'pgsql') {
156
            return new BelongsToPostgres($query, $child, $foreignKey, $ownerKey, $relation);
157
        }
158 27
159
        return new BelongsTo($query, $child, $foreignKey, $ownerKey, $relation);
160
    }
161
162
    /**
163
     * Instantiate a new HasMany relationship.
164
     *
165
     * @param \Illuminate\Database\Eloquent\Builder $query
166
     * @param \Illuminate\Database\Eloquent\Model $parent
167
     * @param string $foreignKey
168
     * @param string $localKey
169
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
170 24
     */
171
    protected function newHasMany(Builder $query, Model $parent, $foreignKey, $localKey)
172 24
    {
173 6
        if ($query->getConnection()->getDriverName() === 'pgsql') {
174
            return new HasManyPostgres($query, $parent, $foreignKey, $localKey);
175
        }
176 18
177
        return new HasMany($query, $parent, $foreignKey, $localKey);
178
    }
179
180
    /**
181
     * Instantiate a new HasManyThrough relationship.
182
     *
183
     * @param \Illuminate\Database\Eloquent\Builder $query
184
     * @param \Illuminate\Database\Eloquent\Model $farParent
185
     * @param \Illuminate\Database\Eloquent\Model $throughParent
186
     * @param string $firstKey
187
     * @param string $secondKey
188
     * @param string $localKey
189
     * @param string $secondLocalKey
190
     * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
191 24
     */
192
    protected function newHasManyThrough(Builder $query, Model $farParent, Model $throughParent, $firstKey, $secondKey, $localKey, $secondLocalKey)
193 24
    {
194 6
        if ($query->getConnection()->getDriverName() === 'pgsql') {
195
            return new HasManyThroughPostgres($query, $farParent, $throughParent, $firstKey, $secondKey, $localKey, $secondLocalKey);
196
        }
197 18
198
        return new HasManyThrough($query, $farParent, $throughParent, $firstKey, $secondKey, $localKey, $secondLocalKey);
199
    }
200
201
    /**
202
     * Instantiate a new MorphMany relationship.
203
     *
204
     * @param \Illuminate\Database\Eloquent\Builder $query
205
     * @param \Illuminate\Database\Eloquent\Model $parent
206
     * @param string $type
207
     * @param string $id
208
     * @param string $localKey
209
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
210 20
     */
211
    protected function newMorphMany(Builder $query, Model $parent, $type, $id, $localKey)
212 20
    {
213 5
        if ($query->getConnection()->getDriverName() === 'pgsql') {
214
            return new MorphManyPostgres($query, $parent, $type, $id, $localKey);
215
        }
216 15
217
        return new MorphMany($query, $parent, $type, $id, $localKey);
218
    }
219
220
    /**
221
     * Define an inverse one-to-one or many JSON relationship.
222
     *
223
     * @param string $related
224
     * @param string $foreignKey
225
     * @param string $ownerKey
226
     * @param string $relation
227
     * @return \Staudenmeir\EloquentJsonRelations\Relations\BelongsToJson
228 76
     */
229
    public function belongsToJson($related, $foreignKey, $ownerKey = null, $relation = null)
230 76
    {
231 76
        if (is_null($relation)) {
232
            $relation = $this->guessBelongsToRelation();
0 ignored issues
show
Bug introduced by
It seems like guessBelongsToRelation() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

232
            /** @scrutinizer ignore-call */ 
233
            $relation = $this->guessBelongsToRelation();
Loading history...
233
        }
234
235 76
        /** @var \Illuminate\Database\Eloquent\Model $instance */
236
        $instance = $this->newRelatedInstance($related);
0 ignored issues
show
Bug introduced by
It seems like newRelatedInstance() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

236
        /** @scrutinizer ignore-call */ 
237
        $instance = $this->newRelatedInstance($related);
Loading history...
237 76
238
        $ownerKey = $ownerKey ?: $instance->getKeyName();
239 76
240 76
        return $this->newBelongsToJson(
241
            $instance->newQuery(),
242
            $this,
0 ignored issues
show
Bug introduced by
$this of type Staudenmeir\EloquentJson...ns\HasJsonRelationships is incompatible with the type Illuminate\Database\Eloquent\Model expected by parameter $child of Staudenmeir\EloquentJson...ips::newBelongsToJson(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

242
            /** @scrutinizer ignore-type */ $this,
Loading history...
243
            $foreignKey,
244
            $ownerKey,
245
            $relation
246
        );
247
    }
248
249
    /**
250
     * Instantiate a new BelongsToJson relationship.
251
     *
252
     * @param \Illuminate\Database\Eloquent\Builder $query
253
     * @param \Illuminate\Database\Eloquent\Model $child
254
     * @param string $foreignKey
255
     * @param string $ownerKey
256
     * @param string $relation
257
     * @return \Staudenmeir\EloquentJsonRelations\Relations\BelongsToJson
258 76
     */
259
    protected function newBelongsToJson(Builder $query, Model $child, $foreignKey, $ownerKey, $relation)
260 76
    {
261
        return new BelongsToJson($query, $child, $foreignKey, $ownerKey, $relation);
262
    }
263
264
    /**
265
     * Define a one-to-many JSON relationship.
266
     *
267
     * @param string $related
268
     * @param string $foreignKey
269
     * @param string $localKey
270
     * @return \Staudenmeir\EloquentJsonRelations\Relations\HasManyJson
271 36
     */
272
    public function hasManyJson($related, $foreignKey, $localKey = null)
273
    {
274 36
        /** @var \Illuminate\Database\Eloquent\Model $instance */
275
        $instance = $this->newRelatedInstance($related);
276 36
277
        $localKey = $localKey ?: $this->getKeyName();
0 ignored issues
show
Bug introduced by
It seems like getKeyName() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

277
        $localKey = $localKey ?: $this->/** @scrutinizer ignore-call */ getKeyName();
Loading history...
278 36
279 36
        return $this->newHasManyJson(
280
            $instance->newQuery(),
281 36
            $this,
0 ignored issues
show
Bug introduced by
$this of type Staudenmeir\EloquentJson...ns\HasJsonRelationships is incompatible with the type Illuminate\Database\Eloquent\Model expected by parameter $parent of Staudenmeir\EloquentJson...ships::newHasManyJson(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

281
            /** @scrutinizer ignore-type */ $this,
Loading history...
282
            $instance->getTable().'.'.$foreignKey,
283
            $localKey
284
        );
285
    }
286
287
    /**
288
     * Define has-many-through JSON relationship
289
     *
290
     * @param \Illuminate\Database\Eloquent\Model $related
291
     * @param \Illuminate\Database\Eloquent\Model $through
292
     * @param string $firstKey
293
     * @param string $secondKey
294
     * @param string $localKey
295 36
     * @param string $secondLocalKey
296
     * @return \Staudenmeir\EloquentJsonRelations\Relations\HasManyThroughJson
297 36
     */
298
    public function hasManyThroughJson($related, $through, $firstKey = null, $secondKey = null, $localKey = null, $secondLocalKey = null)
299
    {
300
        $through = new $through;
301
        $firstKey = $firstKey ?: $this->getForeignKey();
0 ignored issues
show
Bug introduced by
It seems like getForeignKey() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

301
        $firstKey = $firstKey ?: $this->/** @scrutinizer ignore-call */ getForeignKey();
Loading history...
302
        $secondKey = $secondKey ?: $through->getForeignKey();
303
304
        $query = $this->newRelatedInstance($related)->newQuery();
305
        
306
        return $this->newHasManyThroughJson(
307
            $query,
308
            $this,
0 ignored issues
show
Bug introduced by
$this of type Staudenmeir\EloquentJson...ns\HasJsonRelationships is incompatible with the type Illuminate\Database\Eloquent\Model expected by parameter $farParent of Staudenmeir\EloquentJson...newHasManyThroughJson(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

308
            /** @scrutinizer ignore-type */ $this,
Loading history...
309
            $through,
310
            $firstKey,
311
            $secondKey,
312
            $localKey ?: $this->getKeyName(),
313
            $secondLocalKey ?: $through->getKeyName()
314
        );
315
    }
316
317
    /**
318
     * Instantiate a new hasManyThroughJson relationship.
319
     *
320
     * @param \Illuminate\Database\Eloquent\Builder $query
321
     * @param \Illuminate\Database\Eloquent\Model $farParent
322
     * @param \Illuminate\Database\Eloquent\Model $through
323
     * @param string $firstKey
324
     * @param string $secondKey
325
     * @param string $localKey
326
     * @param string $secondLocalKey
327
     * @return \Staudenmeir\EloquentJsonRelations\Relations\HasManyThroughJson
328
     */
329
    protected function newHasManyThroughJson(Builder $query, Model $farParent, Model $throughParent, $firstKey, $secondKey, $localKey, $secondLocalKey)
330
    {
331
        return new HasManyThroughJson($query, $farParent, $throughParent, $firstKey, $secondKey, $localKey, $secondLocalKey);
332
    }
333
334
    /**
335
     * Instantiate a new HasManyJson relationship.
336
     *
337
     * @param \Illuminate\Database\Eloquent\Builder $query
338
     * @param \Illuminate\Database\Eloquent\Model $parent
339
     * @param string $foreignKey
340
     * @param string $localKey
341
     * @return \Staudenmeir\EloquentJsonRelations\Relations\HasManyJson
342
     */
343
    protected function newHasManyJson(Builder $query, Model $parent, $foreignKey, $localKey)
344
    {
345
        return new HasManyJson($query, $parent, $foreignKey, $localKey);
346
    }
347
}
348