Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for clearing relationships #33

Merged
merged 1 commit into from
Aug 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
15 changes: 11 additions & 4 deletions src/JsonApi/Request/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down