3
3
namespace Swis \JsonApi \Client ;
4
4
5
5
use Jenssegers \Model \Model ;
6
+ use Swis \JsonApi \Client \Interfaces \DataInterface ;
6
7
use Swis \JsonApi \Client \Interfaces \ItemInterface ;
7
- use Swis \JsonApi \Client \Interfaces \RelationInterface ;
8
+ use Swis \JsonApi \Client \Interfaces \ManyRelationInterface ;
9
+ use Swis \JsonApi \Client \Interfaces \OneRelationInterface ;
8
10
use Swis \JsonApi \Client \Relations \HasManyRelation ;
9
11
use Swis \JsonApi \Client \Relations \HasOneRelation ;
10
12
use Swis \JsonApi \Client \Relations \MorphToManyRelation ;
@@ -23,14 +25,14 @@ class Item extends Model implements ItemInterface
23
25
protected $ id ;
24
26
25
27
/**
26
- * Contains the initial values (Which fields are pre-filled on CREATE-form) .
28
+ * Contains the initial values.
27
29
*
28
30
* @var array
29
31
*/
30
32
protected $ initial = [];
31
33
32
34
/**
33
- * @var \Swis\JsonApi\Client\Interfaces\RelationInterface []
35
+ * @var \Swis\JsonApi\Client\Interfaces\OneRelationInterface[]|\Swis\JsonApi\Client\Interfaces\ManyRelationInterface []
34
36
*/
35
37
protected $ relationships = [];
36
38
@@ -135,30 +137,8 @@ public function getRelationships(): array
135
137
{
136
138
$ relationships = [];
137
139
138
- /** @var \Swis\JsonApi\Client\Interfaces\RelationInterface $relationship */
139
140
foreach ($ this ->relationships as $ name => $ relationship ) {
140
- if ($ relationship instanceof HasOneRelation) {
141
- $ relationships [$ name ] = ['data ' => null ];
142
-
143
- if ($ relationship ->getIncluded () !== null ) {
144
- $ relationships [$ name ] = [
145
- 'data ' => [
146
- 'type ' => $ relationship ->getType (),
147
- 'id ' => $ relationship ->getId (),
148
- ],
149
- ];
150
- }
151
- } elseif ($ relationship instanceof HasManyRelation) {
152
- $ relationships [$ name ]['data ' ] = [];
153
-
154
- foreach ($ relationship ->getIncluded () as $ item ) {
155
- $ relationships [$ name ]['data ' ][] =
156
- [
157
- 'type ' => $ relationship ->getType (),
158
- 'id ' => $ item ->getId (),
159
- ];
160
- }
161
- } elseif ($ relationship instanceof MorphToRelation) {
141
+ if ($ relationship instanceof OneRelationInterface) {
162
142
$ relationships [$ name ] = ['data ' => null ];
163
143
164
144
if ($ relationship ->getIncluded () !== null ) {
@@ -169,7 +149,7 @@ public function getRelationships(): array
169
149
],
170
150
];
171
151
}
172
- } elseif ($ relationship instanceof MorphToManyRelation ) {
152
+ } elseif ($ relationship instanceof ManyRelationInterface ) {
173
153
$ relationships [$ name ]['data ' ] = [];
174
154
175
155
foreach ($ relationship ->getIncluded () as $ item ) {
@@ -188,8 +168,6 @@ public function getRelationships(): array
188
168
/**
189
169
* @TODO: MEGA TODO. Set up a serializer for the Item so that we can remove this, getRelationships etc
190
170
*
191
- * @throws \Exception
192
- *
193
171
* @return \Swis\JsonApi\Client\Collection
194
172
*/
195
173
public function getIncluded (): Collection
@@ -201,23 +179,22 @@ public function getIncluded(): Collection
201
179
continue ;
202
180
}
203
181
204
- $ includedFromRelationship = $ relationship ->getIncluded ();
205
- if ($ includedFromRelationship instanceof ItemInterface) {
206
- if ($ includedFromRelationship ->canBeIncluded ()) {
207
- $ included ->push ($ includedFromRelationship ->toJsonApiArray ());
182
+ if ($ relationship instanceof OneRelationInterface) {
183
+ /** @var \Swis\JsonApi\Client\Interfaces\ItemInterface $item */
184
+ $ item = $ relationship ->getIncluded ();
185
+ if ($ item ->canBeIncluded ()) {
186
+ $ included ->push ($ item ->toJsonApiArray ());
208
187
}
209
- $ included = $ included ->merge ($ includedFromRelationship ->getIncluded ());
210
- } elseif ($ includedFromRelationship instanceof Collection ) {
211
- $ includedFromRelationship ->each (
188
+ $ included = $ included ->merge ($ item ->getIncluded ());
189
+ } elseif ($ relationship instanceof ManyRelationInterface ) {
190
+ $ relationship -> getIncluded () ->each (
212
191
function (ItemInterface $ item ) use (&$ included ) {
213
192
if ($ item ->canBeIncluded ()) {
214
193
$ included ->push ($ item ->toJsonApiArray ());
215
194
}
216
195
$ included = $ included ->merge ($ item ->getIncluded ());
217
196
}
218
197
);
219
- } else {
220
- throw new \Exception ('Not yet implemented ' );
221
198
}
222
199
}
223
200
@@ -269,7 +246,7 @@ public function getAttribute($key)
269
246
*
270
247
* @return bool
271
248
*/
272
- public function hasAttribute ($ key )
249
+ public function hasAttribute ($ key ): bool
273
250
{
274
251
return array_key_exists ($ key , $ this ->attributes );
275
252
}
@@ -279,7 +256,7 @@ public function hasAttribute($key)
279
256
*
280
257
* @param string $key
281
258
*
282
- * @return \Swis\JsonApi\Client\Interfaces\DataInterface
259
+ * @return \Swis\JsonApi\Client\Interfaces\DataInterface|null
283
260
*/
284
261
public function getRelationValue ($ key )
285
262
{
@@ -295,6 +272,8 @@ public function getRelationValue($key)
295
272
if ($ this ->hasRelationship ($ key )) {
296
273
return $ this ->getRelationship ($ key )->getIncluded ();
297
274
}
275
+
276
+ return null ;
298
277
}
299
278
300
279
/**
@@ -306,18 +285,15 @@ public function getRelationValue($key)
306
285
*/
307
286
public function __isset ($ key )
308
287
{
309
- $ result = (isset ($ this ->attributes [$ key ]) || isset ($ this ->relationships [snake_case ($ key )])) ||
310
- ($ this ->hasGetMutator ($ key ) && !is_null ($ this ->getAttributeValue ($ key )));
311
-
312
- return $ result ;
288
+ return parent ::__isset ($ key ) || $ this ->hasRelationship ($ key ) || $ this ->hasRelationship (snake_case ($ key ));
313
289
}
314
290
315
291
/**
316
- * @param $name
292
+ * @param string $name
317
293
*
318
- * @return \Swis\JsonApi\Client\Interfaces\RelationInterface
294
+ * @return \Swis\JsonApi\Client\Interfaces\OneRelationInterface|\Swis\JsonApi\Client\Interfaces\ManyRelationInterface
319
295
*/
320
- public function getRelationship (string $ name ): RelationInterface
296
+ public function getRelationship (string $ name )
321
297
{
322
298
return $ this ->relationships [$ name ];
323
299
}
@@ -355,10 +331,9 @@ public function removeRelationship(string $name)
355
331
public function hasOne (string $ class , string $ relationName = null )
356
332
{
357
333
$ relationName = $ relationName ?: snake_case (debug_backtrace ()[1 ]['function ' ]);
358
- $ itemType = (new $ class ())->getType ();
359
334
360
335
if (!array_key_exists ($ relationName , $ this ->relationships )) {
361
- $ this ->relationships [$ relationName ] = new HasOneRelation ($ itemType );
336
+ $ this ->relationships [$ relationName ] = new HasOneRelation (( new $ class ())-> getType () );
362
337
}
363
338
364
339
return $ this ->relationships [$ relationName ];
@@ -375,10 +350,9 @@ public function hasOne(string $class, string $relationName = null)
375
350
public function hasMany (string $ class , string $ relationName = null )
376
351
{
377
352
$ relationName = $ relationName ?: snake_case (debug_backtrace ()[1 ]['function ' ]);
378
- $ itemType = (new $ class ())->getType ();
379
353
380
354
if (!array_key_exists ($ relationName , $ this ->relationships )) {
381
- $ this ->relationships [$ relationName ] = new HasManyRelation ($ itemType );
355
+ $ this ->relationships [$ relationName ] = new HasManyRelation (( new $ class ())-> getType () );
382
356
}
383
357
384
358
return $ this ->relationships [$ relationName ];
@@ -481,24 +455,22 @@ public function getAvailableRelations(): array
481
455
}
482
456
483
457
/**
484
- * Set the specific relationship in the model.
458
+ * Set the specific relationship on the model.
485
459
*
486
- * @param string $relation
487
- * @param mixed $value
460
+ * @param string $relation
461
+ * @param \Swis\JsonApi\Client\Interfaces\DataInterface $value
488
462
*
489
463
* @return static
490
464
*/
491
- public function setRelation ($ relation , $ value )
465
+ public function setRelation (string $ relation , DataInterface $ value )
492
466
{
493
467
if (method_exists ($ this , $ relation )) {
494
- /** @var \Swis\JsonApi\Client\Interfaces\RelationInterface $relationObject */
468
+ /** @var \Swis\JsonApi\Client\Interfaces\OneRelationInterface|\Swis\JsonApi\Client\Interfaces\ManyRelationInterface $relationObject */
495
469
$ relationObject = $ this ->$ relation ();
470
+ } elseif ($ value instanceof Collection) {
471
+ $ relationObject = $ this ->morphToMany (snake_case ($ relation ));
496
472
} else {
497
- if ($ value instanceof Collection) {
498
- $ relationObject = $ this ->morphToMany (snake_case ($ relation ));
499
- } else {
500
- $ relationObject = $ this ->morphTo (snake_case ($ relation ));
501
- }
473
+ $ relationObject = $ this ->morphTo (snake_case ($ relation ));
502
474
}
503
475
504
476
$ relationObject ->associate ($ value );
0 commit comments