Skip to content

Commit

Permalink
Allow HalResource without identifier if identifierName is set to false
Browse files Browse the repository at this point in the history
This change allows registering a single HalResource without an identifier. See for more details this issue:
#109
  • Loading branch information
Wilt authored and weierophinney committed May 26, 2016
1 parent 5776025 commit aa2700e
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/Plugin/HalLinks.php
Original file line number Diff line number Diff line change
Expand Up @@ -546,21 +546,23 @@ public function createResourceFromMetadata($object, Metadata $metadata)
}
$data = $hydrator->extract($object);

$identiferName = $metadata->getIdentifierName();
if (!isset($data[$identiferName])) {
$identifierName = $metadata->getIdentifierName();

if (false !== $identifierName && ! isset($data[$identifierName])) {
throw new Exception\RuntimeException(sprintf(
'Unable to determine identifier for object of type "%s"; no fields matching "%s"',
get_class($object),
$identiferName
$identifierName
));
}
$id = $data[$identiferName];

$id = (false === $identifierName) ? null : $data[$identifierName];

$resource = new HalResource($data, $id);
$links = $resource->getLinks();
$this->marshalMetadataLinks($metadata, $links);
if (!$links->has('self')) {
$link = $this->marshalSelfLinkFromMetadata($metadata, $object, $id, $identiferName);
$link = $this->marshalSelfLinkFromMetadata($metadata, $object, $id, $identifierName);
$links->add($link);
}

Expand Down

0 comments on commit aa2700e

Please sign in to comment.