From 8ae0682d3a35501c2d2eba3d4f9ca96e58654687 Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Thu, 3 Oct 2019 16:19:56 +0930 Subject: [PATCH] Prevent error with empty relationships without links --- src/Serializer.php | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/Serializer.php b/src/Serializer.php index d083b5a..d2cc350 100644 --- a/src/Serializer.php +++ b/src/Serializer.php @@ -88,7 +88,9 @@ final class Serializer } } - $data['fields'][$name] = $value; + if (!empty($value)) { + $data['fields'][$name] = $value; + } } $data['links']['self'] = new SelfLink($resourceUrl); @@ -175,11 +177,17 @@ final class Serializer ); } - private function emptyRelationship(Relationship $field, string $resourceUrl): EmptyRelationship + private function emptyRelationship(Relationship $field, string $resourceUrl): ?EmptyRelationship { + $links = $this->getRelationshipLinks($field, $resourceUrl); + + if (! $links) { + return null; + } + return new EmptyRelationship( $field->getName(), - ...$this->getRelationshipLinks($field, $resourceUrl) + ...$links ); }