Skip to content

Commit

Permalink
Add support for clearing relationships
Browse files Browse the repository at this point in the history
If you sent a request, trying to clear a relationship (remove the relationship) by sending `"relationshipname" : { "data": null}"` yin ignored this, behaving exactly like it did when the relationship was completely omitted.
The hydrator for the relationship was not called and calls to `$jsonApi->getRequest()->getResourceToOneRelationship('relationshipname')` behaved just as if no relationship was sent.
This patch intends to change that by handling the null-case and creating empty ToOneRelationships where appropriate.
  • Loading branch information
Emil Andersson committed Aug 4, 2016
1 parent 57cb7b3 commit b567e21
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 8 deletions.
7 changes: 5 additions & 2 deletions src/JsonApi/Hydrator/HydratorTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
10 changes: 10 additions & 0 deletions src/JsonApi/Hydrator/Relationship/ToManyRelationship.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
14 changes: 12 additions & 2 deletions src/JsonApi/Hydrator/Relationship/ToOneRelationship.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
class ToOneRelationship
{
/**
* @var \WoohooLabs\Yin\JsonApi\Schema\ResourceIdentifier
* @var null | \WoohooLabs\Yin\JsonApi\Schema\ResourceIdentifier
*/
protected $resourceIdentifier;

Expand All @@ -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);
}
}
13 changes: 9 additions & 4 deletions src/JsonApi/Request/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -538,11 +538,16 @@ 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;
}

/**
Expand Down

0 comments on commit b567e21

Please sign in to comment.