Passed
Branch master (39a2d7)
by Jonas
01:38
created

IsJsonRelation::hydratePivotRelation()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 2
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Staudenmeir\EloquentJsonRelations\Relations;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Database\Eloquent\Collection;
7
use Illuminate\Database\Eloquent\Model;
8
use Illuminate\Database\Eloquent\Relations\Pivot;
9
use RuntimeException;
10
use Staudenmeir\EloquentJsonRelations\Grammars\MySqlGrammar;
11
use Staudenmeir\EloquentJsonRelations\Grammars\PostgresGrammar;
12
use Staudenmeir\EloquentJsonRelations\Grammars\SqlServerGrammar;
13
14
trait IsJsonRelation
15
{
16
    /**
17
     * The base path of the foreign key.
18
     *
19
     * @var string
20
     */
21
    protected $path;
22
23
    /**
24
     * The optional object key of the foreign key.
25
     *
26
     * @var string
27
     */
28
    protected $key;
29
30
    /**
31
     * Create a new JSON relationship instance.
32
     *
33
     * @return void
34
     */
35 66
    public function __construct()
36
    {
37 66
        $args = func_get_args();
38
39 66
        $foreignKey = explode('[]->', $args[2]);
40
41 66
        $this->path = $foreignKey[0];
42 66
        $this->key = $foreignKey[1] ?? null;
43
44 66
        parent::__construct(...$args);
45 66
    }
46
47
    /**
48
     * Execute the query as a "select" statement.
49
     *
50
     * @param  array  $columns
51
     * @return \Illuminate\Database\Eloquent\Collection
52
     */
53 34
    public function get($columns = ['*'])
54
    {
55 34
        $models = parent::get($columns);
56
57 34
        if ($this->key && ! empty($this->parent->getAttributes())) {
58 14
            $this->hydratePivotRelation($models, $this->parent);
59
        }
60
61 34
        return $models;
62
    }
63
64
    /**
65
     * Hydrate the pivot relationship on the models.
66
     *
67
     * @param  \Illuminate\Database\Eloquent\Collection  $models
68
     * @param  \Illuminate\Database\Eloquent\Model  $parent
69
     * @return void
70
     */
71 22
    protected function hydratePivotRelation(Collection $models, Model $parent)
72
    {
73 22
        foreach ($models as $i => $model) {
74 22
            $clone = clone $model;
75
76 22
            $models[$i] = $clone->setRelation('pivot', $this->pivotRelation($clone, $parent));
77
        }
78 22
    }
79
80
    /**
81
     * Get the pivot relationship from the query.
82
     *
83
     * @param  \Illuminate\Database\Eloquent\Model  $model
84
     * @param  \Illuminate\Database\Eloquent\Model  $parent
85
     * @return \Illuminate\Database\Eloquent\Model
86
     */
87 22
    protected function pivotRelation(Model $model, Model $parent)
88
    {
89 22
        $attributes = $this->pivotAttributes($model, $parent);
90
91 22
        return Pivot::fromAttributes($model, $attributes, null, true);
92
    }
93
94
    /**
95
     * Get the pivot attributes from a model.
96
     *
97
     * @param  \Illuminate\Database\Eloquent\Model  $model
98
     * @param  \Illuminate\Database\Eloquent\Model  $parent
99
     * @return array
100
     */
101
    abstract protected function pivotAttributes(Model $model, Model $parent);
102
103
    /**
104
     * Get the fully qualified path of the relationship.
105
     *
106
     * @return string
107
     */
108 12
    public function getQualifiedPath()
109
    {
110 12
        return $this->parent->qualifyColumn($this->path);
111
    }
112
113
    /**
114
     * Get the JSON grammar.
115
     *
116
     * @param  \Illuminate\Database\Eloquent\Builder  $query
117
     * @return \Staudenmeir\EloquentJsonRelations\Grammars\JsonGrammar
118
     */
119 16
    protected function getJsonGrammar(Builder $query) {
120 16
        $driver = $query->getConnection()->getDriverName();
121
122 16
        switch ($driver) {
123 16
            case 'mysql':
124 8
                return new MySqlGrammar;
125 8
            case 'pgsql':
126 8
                return new PostgresGrammar;
127
            case 'sqlsrv':
128
                return new SqlServerGrammar;
129
        }
130
131
        throw new RuntimeException('This database is not supported.'); // @codeCoverageIgnore
132
    }
133
}
134