diff --git a/src/Helpers/DataLinksHelper.php b/src/Helpers/DataLinksHelper.php index 6f3358c..ce356d9 100644 --- a/src/Helpers/DataLinksHelper.php +++ b/src/Helpers/DataLinksHelper.php @@ -30,7 +30,7 @@ public static function setResponseDataLinks(array &$mappings, array &$value) $data = []; $type = $value[Serializer::CLASS_IDENTIFIER_KEY]; - if (!empty($mappings[$type])) { + if (is_scalar($type) && !empty($mappings[$type])) { list($idValues, $idProperties) = RecursiveFormatterHelper::getIdPropertyAndValues($mappings, $value, $type); $selfLink = $mappings[$type]->getResourceUrl(); diff --git a/src/Helpers/PropertyHelper.php b/src/Helpers/PropertyHelper.php index f0b9643..78fa3a2 100644 --- a/src/Helpers/PropertyHelper.php +++ b/src/Helpers/PropertyHelper.php @@ -28,6 +28,11 @@ final class PropertyHelper public static function setResponseDataTypeAndId(array &$mappings, array &$value) { $type = $value[Serializer::CLASS_IDENTIFIER_KEY]; + + if (!is_scalar($type)) { + return self::setResponseDataTypeAndId($mappings, $type); + } + $finalType = ($mappings[$type]->getClassAlias()) ? $mappings[$type]->getClassAlias() : $type; $ids = []; diff --git a/src/JsonApiTransformer.php b/src/JsonApiTransformer.php index 667c998..386d0bb 100644 --- a/src/JsonApiTransformer.php +++ b/src/JsonApiTransformer.php @@ -121,10 +121,12 @@ private function removeTypeAndId(array $copy) { $type = $copy[Serializer::CLASS_IDENTIFIER_KEY]; - foreach ($this->mappings[$type]->getIdProperties() as $propertyName) { - unset($copy[$propertyName]); + if (is_scalar($type)) { + foreach ($this->mappings[$type]->getIdProperties() as $propertyName) { + unset($copy[$propertyName]); + } + unset($copy[Serializer::CLASS_IDENTIFIER_KEY]); } - unset($copy[Serializer::CLASS_IDENTIFIER_KEY]); return $copy; } @@ -137,18 +139,21 @@ protected function setResponseLinks(array $value, array &$data) { if (!empty($value[Serializer::CLASS_IDENTIFIER_KEY])) { $type = $value[Serializer::CLASS_IDENTIFIER_KEY]; - $urls = $this->mappings[$type]->getUrls(); - - $data[self::LINKS_KEY] = array_filter( - array_merge( - $this->addHrefToLinks($this->buildLinks()), - (!empty($data[self::LINKS_KEY])) ? $data[self::LINKS_KEY] : [], - (!empty($urls)) ? $this->addHrefToLinks($this->getResponseAdditionalLinks($value, $type)) : [] - ) - ); - - if (empty($data[self::LINKS_KEY])) { - unset($data[self::LINKS_KEY]); + + if (is_scalar($type)) { + $urls = $this->mappings[$type]->getUrls(); + + $data[self::LINKS_KEY] = array_filter( + array_merge( + $this->addHrefToLinks($this->buildLinks()), + (!empty($data[self::LINKS_KEY])) ? $data[self::LINKS_KEY] : [], + (!empty($urls)) ? $this->addHrefToLinks($this->getResponseAdditionalLinks($value, $type)) : [] + ) + ); + + if (empty($data[self::LINKS_KEY])) { + unset($data[self::LINKS_KEY]); + } } } } diff --git a/tests/Http/ResourceCreatedResponseTest.php b/tests/Http/ResourceCreatedResponseTest.php index 5f93ebb..0bfee88 100644 --- a/tests/Http/ResourceCreatedResponseTest.php +++ b/tests/Http/ResourceCreatedResponseTest.php @@ -41,7 +41,7 @@ public function testResponseWithLocation() $this->assertEquals(['application/vnd.api+json'], $response->getHeader('Content-type')); $this->assertEquals(['http://example.com/photos/550e8400-e29b-41d4-a716-446655440000'], $response->getHeader('Location')); } - + public function testResponse() { $json = json_encode([]);