Completed
Push — master ( 9af3cf...09ac23 )
by Jasper
04:37
created

Item::getAttribute()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 3
nc 2
nop 1
crap 3
1
<?php
2
3
namespace Swis\JsonApi\Client;
4
5
use Illuminate\Support\Str;
6
use Jenssegers\Model\Model;
7
use Swis\JsonApi\Client\Concerns\HasId;
8
use Swis\JsonApi\Client\Concerns\HasInitial;
9
use Swis\JsonApi\Client\Concerns\HasLinks;
10
use Swis\JsonApi\Client\Concerns\HasMeta;
11
use Swis\JsonApi\Client\Concerns\HasRelations;
12
use Swis\JsonApi\Client\Concerns\HasType;
13
use Swis\JsonApi\Client\Interfaces\ItemInterface;
14
use Swis\JsonApi\Client\Interfaces\ManyRelationInterface;
15
use Swis\JsonApi\Client\Interfaces\OneRelationInterface;
16
17
/**
18
 * @property string|null id
19
 */
20
class Item extends Model implements ItemInterface
21
{
22
    use HasId;
23
    use HasInitial;
24
    use HasLinks;
25
    use HasMeta;
26
    use HasRelations;
27
    use HasType;
28
29
    /**
30
     * Available relations need to be explicitly set.
31
     *
32
     * @var array
33
     */
34
    protected $availableRelations = [];
35
36
    /**
37
     * @var array
38
     */
39
    protected $guarded = ['id'];
40
41
    /**
42
     * @return array
43
     */
44 288
    public function toJsonApiArray(): array
45
    {
46
        $data = [
47 288
            'type' => $this->getType(),
48
        ];
49
50 288
        if ($this->hasId()) {
51 162
            $data['id'] = $this->getId();
52
        }
53
54 288
        $attributes = $this->toArray();
55 288
        if (!empty($attributes)) {
56 18
            $data['attributes'] = $attributes;
57
        }
58
59 288
        $relationships = $this->getRelationships();
60 288
        if (!empty($relationships)) {
61 189
            $data['relationships'] = $relationships;
62
        }
63
64 288
        $links = $this->getLinks();
65 288
        if ($links !== null) {
66 9
            $data['links'] = $links->toArray();
67
        }
68
69 288
        $meta = $this->getMeta();
70 288
        if ($meta !== null) {
71 9
            $data['meta'] = $meta->toArray();
72
        }
73
74 288
        return $data;
75
    }
76
77
    /**
78
     * @return bool
79
     */
80 27
    public function isNew(): bool
81
    {
82 27
        return !$this->hasId();
83
    }
84
85
    /**
86
     * @param string $key
87
     *
88
     * @return \Swis\JsonApi\Client\Interfaces\DataInterface|mixed
89
     */
90 81
    public function getAttribute($key)
91
    {
92 81
        if ($this->hasAttribute($key) || $this->hasGetMutator($key)) {
93 72
            return parent::getAttribute($key);
94
        }
95
96 18
        return $this->getRelationValue($key);
97
    }
98
99
    /**
100
     * @param $key
101
     *
102
     * @return bool
103
     */
104 432
    public function hasAttribute($key): bool
105
    {
106 432
        return array_key_exists($key, $this->attributes);
107
    }
108
109
    /**
110
     * @return bool
111
     */
112 72
    public function hasAttributes(): bool
113
    {
114 72
        return !empty($this->toArray());
115
    }
116
117
    /**
118
     * @return array
119
     */
120 225
    public function getAvailableRelations(): array
121
    {
122 225
        return $this->availableRelations;
123
    }
124
125
    /**
126
     * @return array
127
     */
128 324
    public function getRelationships(): array
129
    {
130 324
        $relationships = [];
131
132 324
        foreach ($this->getRelations() as $name => $relation) {
133 252
            if (!$relation->hasIncluded()) {
134 36
                continue;
135
            }
136
137 216
            if ($relation instanceof OneRelationInterface) {
138 126
                $relationships[$name]['data'] = null;
139
140 126
                if ($relation->getIncluded() !== null) {
141 90
                    $relationships[$name]['data'] = [
142 90
                        'type' => $relation->getIncluded()->getType(),
143 126
                        'id' => $relation->getIncluded()->getId(),
144
                    ];
145
                }
146 90
            } elseif ($relation instanceof ManyRelationInterface) {
147 90
                $relationships[$name]['data'] = [];
148
149 90
                foreach ($relation->getIncluded() as $item) {
150 54
                    $relationships[$name]['data'][] = [
151 54
                        'type' => $item->getType(),
152 54
                        'id' => $item->getId(),
153
                    ];
154
                }
155
            }
156
157 216
            $links = $relation->getLinks();
158 216
            if ($links !== null) {
159 36
                $relationships[$name]['links'] = $links->toArray();
160
            }
161
162 216
            $meta = $relation->getMeta();
163 216
            if ($meta !== null) {
164 56
                $relationships[$name]['meta'] = $meta->toArray();
165
            }
166
        }
167
168 324
        return $relationships;
169
    }
170
171
    /**
172
     * @return bool
173
     */
174 27
    public function hasRelationships(): bool
175
    {
176 27
        return !empty($this->getRelationships());
177
    }
178
179
    /**
180
     * Fills the model with the values from $initial. This is useful for setting defaults when creating a new item.
181
     *
182
     * @return static
183
     */
184 9
    public function useInitial()
185
    {
186 9
        $this->fill($this->initial);
187
188 9
        return $this;
189
    }
190
191
    /**
192
     * @param string $key
193
     *
194
     * @return mixed
195
     */
196 18
    public function __get($key)
197
    {
198 18
        if ($key === 'id') {
199 9
            return $this->getId();
200
        }
201
202 9
        return parent::__get($key);
203
    }
204
205
    /**
206
     * @param string $key
207
     * @param mixed  $value
208
     */
209 27
    public function __set($key, $value)
210
    {
211 27
        if ($key === 'id') {
212 18
            $this->setId($value);
213
214 18
            return;
215
        }
216
217 9
        parent::__set($key, $value);
218 9
    }
219
220
    /**
221
     * Determine if an attribute exists on the model.
222
     *
223
     * @param string $key
224
     *
225
     * @return bool
226
     */
227 18
    public function __isset($key)
228
    {
229 18
        if ($key === 'id') {
230 9
            return $this->hasId();
231
        }
232
233 9
        return parent::__isset($key) || $this->hasRelation($key) || $this->hasRelation(Str::snake($key));
234
    }
235
236
    /**
237
     * Unset an attribute on the model.
238
     *
239
     * @param string $key
240
     */
241 18
    public function __unset($key)
242
    {
243 18
        if ($key === 'id') {
244 9
            $this->id = null;
245
        }
246
247 18
        unset($this->attributes[$key]);
248 18
    }
249
}
250