Completed
Push — master ( 535b5e...300324 )
by Jasper
13s queued 13s
created

Item::__get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
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 1
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
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 115
    public function toJsonApiArray(): array
45
    {
46
        $data = [
47 115
            'type' => $this->getType(),
48
        ];
49
50 115
        if ($this->hasId()) {
51 70
            $data['id'] = $this->getId();
52
        }
53
54 115
        $attributes = $this->toArray();
55 115
        if (!empty($attributes)) {
56 10
            $data['attributes'] = $attributes;
57
        }
58
59 115
        $relationships = $this->getRelationships();
60 115
        if (!empty($relationships)) {
61 80
            $data['relationships'] = $relationships;
62
        }
63
64 115
        $links = $this->getLinks();
65 115
        if ($links !== null) {
66 5
            $data['links'] = $links->toArray();
67
        }
68
69 115
        $meta = $this->getMeta();
70 115
        if ($meta !== null) {
71 5
            $data['meta'] = $meta->toArray();
72
        }
73
74 115
        return $data;
75
    }
76
77
    /**
78
     * @return bool
79
     */
80 15
    public function isNew(): bool
81
    {
82 15
        return !$this->hasId();
83
    }
84
85
    /**
86
     * @param string $key
87
     *
88
     * @return \Swis\JsonApi\Client\Interfaces\DataInterface|mixed
89
     */
90 65
    public function getAttribute($key)
91
    {
92 65
        if ($this->hasAttribute($key) || $this->hasGetMutator($key)) {
93 40
            return parent::getAttribute($key);
94
        }
95
96 30
        return $this->getRelationValue($key);
97
    }
98
99
    /**
100
     * @param string $key
101
     *
102
     * @return bool
103
     */
104 65
    public function hasAttribute($key): bool
105
    {
106 65
        return array_key_exists($key, $this->attributes);
107
    }
108
109
    /**
110
     * @return bool
111
     */
112 30
    public function hasAttributes(): bool
113
    {
114 30
        return !empty($this->toArray());
115
    }
116
117
    /**
118
     * @return array
119
     */
120 95
    public function getAvailableRelations(): array
121
    {
122 95
        return $this->availableRelations;
123
    }
124
125
    /**
126
     * @return array
127
     */
128 135
    public function getRelationships(): array
129
    {
130 135
        $relationships = [];
131
132 135
        foreach ($this->getRelations() as $name => $relation) {
133 95
            if ($relation instanceof OneRelationInterface) {
134 55
                $relationships[$name] = ['data' => null];
135
136 55
                if ($relation->getIncluded() !== null) {
137 45
                    $relationships[$name] = [
138
                        'data' => [
139 45
                            'type' => $relation->getIncluded()->getType(),
140 55
                            'id'   => $relation->getIncluded()->getId(),
141
                        ],
142
                    ];
143
                }
144 40
            } elseif ($relation instanceof ManyRelationInterface) {
145 40
                $relationships[$name]['data'] = [];
146
147 40
                foreach ($relation->getIncluded() as $item) {
148 30
                    $relationships[$name]['data'][] =
149
                        [
150 30
                            'type' => $item->getType(),
151 43
                            'id'   => $item->getId(),
152
                        ];
153
                }
154
            }
155
        }
156
157 135
        return $relationships;
158
    }
159
160
    /**
161
     * @return bool
162
     */
163 15
    public function hasRelationships(): bool
164
    {
165 15
        return !empty($this->getRelationships());
166
    }
167
168
    /**
169
     * Fills the model with the values from $initial. This is useful for setting defaults when creating a new item.
170
     *
171
     * @return static
172
     */
173 5
    public function useInitial()
174
    {
175 5
        $this->fill($this->initial);
176
177 5
        return $this;
178
    }
179
180
    /**
181
     * @param string $key
182
     *
183
     * @return mixed
184
     */
185 30
    public function __get($key)
186
    {
187 30
        if ($key === 'id') {
188 5
            return $this->getId();
189
        }
190
191 25
        return parent::__get($key);
192
    }
193
194
    /**
195
     * @param string $key
196
     * @param mixed  $value
197
     */
198 15
    public function __set($key, $value)
199
    {
200 15
        if ($key === 'id') {
201 10
            $this->setId($value);
202
203 10
            return;
204
        }
205
206 5
        parent::__set($key, $value);
207 5
    }
208
209
    /**
210
     * Determine if an attribute exists on the model.
211
     *
212
     * @param string $key
213
     *
214
     * @return bool
215
     */
216 10
    public function __isset($key)
217
    {
218 10
        if ($key === 'id') {
219 5
            return $this->hasId();
220
        }
221
222 5
        return parent::__isset($key) || $this->hasRelation($key) || $this->hasRelation(Str::snake($key));
223
    }
224
225
    /**
226
     * Unset an attribute on the model.
227
     *
228
     * @param string $key
229
     */
230 10
    public function __unset($key)
231
    {
232 10
        if ($key === 'id') {
233 5
            $this->id = null;
234
        }
235
236 10
        unset($this->attributes[$key]);
237 10
    }
238
}
239