diff --git a/src/JsonApi/Hydrator/HydratorTrait.php b/src/JsonApi/Hydrator/HydratorTrait.php index d29cfe53..43f9f051 100644 --- a/src/JsonApi/Hydrator/HydratorTrait.php +++ b/src/JsonApi/Hydrator/HydratorTrait.php @@ -220,11 +220,14 @@ protected function getRelationshipType($object) */ private function createRelationship(array $relationship) { - if (isset($relationship["data"]) === false) { + if (array_key_exists("data", $relationship) === false) { return null; } - if ($this->isAssociativeArray($relationship["data"]) === true) { + //If this is a request to clear the relationship, we create an empty relationship + if (is_null($relationship["data"])) { + $result = new ToOneRelationship(); + } elseif ($this->isAssociativeArray($relationship["data"]) === true) { $result = new ToOneRelationship(ResourceIdentifier::fromArray($relationship["data"])); } else { $result = new ToManyRelationship(); diff --git a/src/JsonApi/Hydrator/Relationship/ToManyRelationship.php b/src/JsonApi/Hydrator/Relationship/ToManyRelationship.php index 9eb5ceed..43db082f 100644 --- a/src/JsonApi/Hydrator/Relationship/ToManyRelationship.php +++ b/src/JsonApi/Hydrator/Relationship/ToManyRelationship.php @@ -65,4 +65,14 @@ public function getResourceIdentifierIds() return $ids; } + + /** + * Returns true if this relationship is empty, not containing a resource identifier + * This will be the case when the request want to clear a relationship and sends an empty array as data. + * @return bool + */ + public function isEmpty() + { + return empty($this->resourceIdentifiers); + } } diff --git a/src/JsonApi/Hydrator/Relationship/ToOneRelationship.php b/src/JsonApi/Hydrator/Relationship/ToOneRelationship.php index c9177821..e66adccd 100644 --- a/src/JsonApi/Hydrator/Relationship/ToOneRelationship.php +++ b/src/JsonApi/Hydrator/Relationship/ToOneRelationship.php @@ -6,7 +6,7 @@ class ToOneRelationship { /** - * @var \WoohooLabs\Yin\JsonApi\Schema\ResourceIdentifier + * @var null | \WoohooLabs\Yin\JsonApi\Schema\ResourceIdentifier */ protected $resourceIdentifier; @@ -29,10 +29,20 @@ public function setResourceIdentifier(ResourceIdentifier $resourceIdentifier) } /** - * @return \WoohooLabs\Yin\JsonApi\Schema\ResourceIdentifier $resourceIdentifier + * @return null | \WoohooLabs\Yin\JsonApi\Schema\ResourceIdentifier $resourceIdentifier */ public function getResourceIdentifier() { return $this->resourceIdentifier; } + + /** + * Returns true if this relationship is empty, not containing a resource identifier + * This will be the case when the request want to clear a relationship and sends null as data. + * @return bool + */ + public function isEmpty() + { + return is_null($this->resourceIdentifier); + } } diff --git a/src/JsonApi/Request/Request.php b/src/JsonApi/Request/Request.php index 4540baf9..140c05c3 100644 --- a/src/JsonApi/Request/Request.php +++ b/src/JsonApi/Request/Request.php @@ -538,11 +538,18 @@ public function getResourceToOneRelationship($relationship) { $data = $this->getResource(); - if (isset($data["relationships"][$relationship]["data"]) === false) { - return null; + //The relationship has to exist in the request and have a data attribute to be valid + if (isset($data["relationships"][$relationship]) && + array_key_exists("data", $data["relationships"][$relationship]) + ) { + //If the data is null, this request is to clear the relationship, we return an empty relationship + if (is_null($data["relationships"][$relationship]["data"])) { + return new ToOneRelationship(); + } + //If the data is set and is not null, we create the relationship with a resourceidentifier from the request + return new ToOneRelationship(ResourceIdentifier::fromArray($data["relationships"][$relationship]["data"])); } - - return new ToOneRelationship(ResourceIdentifier::fromArray($data["relationships"][$relationship]["data"])); + return null; } /**