33namespace Swis \JsonApi \Client ;
44
55use Jenssegers \Model \Model ;
6+ use Swis \JsonApi \Client \Interfaces \DataInterface ;
67use 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 ;
810use Swis \JsonApi \Client \Relations \HasManyRelation ;
911use Swis \JsonApi \Client \Relations \HasOneRelation ;
1012use Swis \JsonApi \Client \Relations \MorphToManyRelation ;
@@ -23,14 +25,14 @@ class Item extends Model implements ItemInterface
2325 protected $ id ;
2426
2527 /**
26- * Contains the initial values (Which fields are pre-filled on CREATE-form) .
28+ * Contains the initial values.
2729 *
2830 * @var array
2931 */
3032 protected $ initial = [];
3133
3234 /**
33- * @var \Swis\JsonApi\Client\Interfaces\RelationInterface []
35+ * @var \Swis\JsonApi\Client\Interfaces\OneRelationInterface[]|\Swis\JsonApi\Client\Interfaces\ManyRelationInterface []
3436 */
3537 protected $ relationships = [];
3638
@@ -135,30 +137,8 @@ public function getRelationships(): array
135137 {
136138 $ relationships = [];
137139
138- /** @var \Swis\JsonApi\Client\Interfaces\RelationInterface $relationship */
139140 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) {
162142 $ relationships [$ name ] = ['data ' => null ];
163143
164144 if ($ relationship ->getIncluded () !== null ) {
@@ -169,7 +149,7 @@ public function getRelationships(): array
169149 ],
170150 ];
171151 }
172- } elseif ($ relationship instanceof MorphToManyRelation ) {
152+ } elseif ($ relationship instanceof ManyRelationInterface ) {
173153 $ relationships [$ name ]['data ' ] = [];
174154
175155 foreach ($ relationship ->getIncluded () as $ item ) {
@@ -188,8 +168,6 @@ public function getRelationships(): array
188168 /**
189169 * @TODO: MEGA TODO. Set up a serializer for the Item so that we can remove this, getRelationships etc
190170 *
191- * @throws \Exception
192- *
193171 * @return \Swis\JsonApi\Client\Collection
194172 */
195173 public function getIncluded (): Collection
@@ -201,23 +179,22 @@ public function getIncluded(): Collection
201179 continue ;
202180 }
203181
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 ());
208187 }
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 (
212191 function (ItemInterface $ item ) use (&$ included ) {
213192 if ($ item ->canBeIncluded ()) {
214193 $ included ->push ($ item ->toJsonApiArray ());
215194 }
216195 $ included = $ included ->merge ($ item ->getIncluded ());
217196 }
218197 );
219- } else {
220- throw new \Exception ('Not yet implemented ' );
221198 }
222199 }
223200
@@ -269,7 +246,7 @@ public function getAttribute($key)
269246 *
270247 * @return bool
271248 */
272- public function hasAttribute ($ key )
249+ public function hasAttribute ($ key ): bool
273250 {
274251 return array_key_exists ($ key , $ this ->attributes );
275252 }
@@ -279,7 +256,7 @@ public function hasAttribute($key)
279256 *
280257 * @param string $key
281258 *
282- * @return \Swis\JsonApi\Client\Interfaces\DataInterface
259+ * @return \Swis\JsonApi\Client\Interfaces\DataInterface|null
283260 */
284261 public function getRelationValue ($ key )
285262 {
@@ -295,6 +272,8 @@ public function getRelationValue($key)
295272 if ($ this ->hasRelationship ($ key )) {
296273 return $ this ->getRelationship ($ key )->getIncluded ();
297274 }
275+
276+ return null ;
298277 }
299278
300279 /**
@@ -306,18 +285,15 @@ public function getRelationValue($key)
306285 */
307286 public function __isset ($ key )
308287 {
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 ));
313289 }
314290
315291 /**
316- * @param $name
292+ * @param string $name
317293 *
318- * @return \Swis\JsonApi\Client\Interfaces\RelationInterface
294+ * @return \Swis\JsonApi\Client\Interfaces\OneRelationInterface|\Swis\JsonApi\Client\Interfaces\ManyRelationInterface
319295 */
320- public function getRelationship (string $ name ): RelationInterface
296+ public function getRelationship (string $ name )
321297 {
322298 return $ this ->relationships [$ name ];
323299 }
@@ -355,10 +331,9 @@ public function removeRelationship(string $name)
355331 public function hasOne (string $ class , string $ relationName = null )
356332 {
357333 $ relationName = $ relationName ?: snake_case (debug_backtrace ()[1 ]['function ' ]);
358- $ itemType = (new $ class ())->getType ();
359334
360335 if (!array_key_exists ($ relationName , $ this ->relationships )) {
361- $ this ->relationships [$ relationName ] = new HasOneRelation ($ itemType );
336+ $ this ->relationships [$ relationName ] = new HasOneRelation (( new $ class ())-> getType () );
362337 }
363338
364339 return $ this ->relationships [$ relationName ];
@@ -375,10 +350,9 @@ public function hasOne(string $class, string $relationName = null)
375350 public function hasMany (string $ class , string $ relationName = null )
376351 {
377352 $ relationName = $ relationName ?: snake_case (debug_backtrace ()[1 ]['function ' ]);
378- $ itemType = (new $ class ())->getType ();
379353
380354 if (!array_key_exists ($ relationName , $ this ->relationships )) {
381- $ this ->relationships [$ relationName ] = new HasManyRelation ($ itemType );
355+ $ this ->relationships [$ relationName ] = new HasManyRelation (( new $ class ())-> getType () );
382356 }
383357
384358 return $ this ->relationships [$ relationName ];
@@ -481,24 +455,22 @@ public function getAvailableRelations(): array
481455 }
482456
483457 /**
484- * Set the specific relationship in the model.
458+ * Set the specific relationship on the model.
485459 *
486- * @param string $relation
487- * @param mixed $value
460+ * @param string $relation
461+ * @param \Swis\JsonApi\Client\Interfaces\DataInterface $value
488462 *
489463 * @return static
490464 */
491- public function setRelation ($ relation , $ value )
465+ public function setRelation (string $ relation , DataInterface $ value )
492466 {
493467 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 */
495469 $ relationObject = $ this ->$ relation ();
470+ } elseif ($ value instanceof Collection) {
471+ $ relationObject = $ this ->morphToMany (snake_case ($ relation ));
496472 } 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 ));
502474 }
503475
504476 $ relationObject ->associate ($ value );
0 commit comments