Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
Merge branch 'hotfix/41-null-value-casting' into develop
Browse files Browse the repository at this point in the history
Forward port #41
  • Loading branch information
weierophinney committed Dec 11, 2018
2 parents 9b93cc8 + e29eacd commit 31250bd
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Expand Up @@ -46,7 +46,9 @@ Versions prior to 0.4.0 were released as the package "weierophinney/hal".

### Fixed

- Nothing.
- [#41](https://github.com/zendframework/zend-expressive-hal/pull/41) fixes how `null` values in resources are handled when rendering as XML.
Previously, these would lead to an `InvalidResourceValueException`; now they
are rendered as content-less tags.

## 1.1.0 - 2018-06-05

Expand Down
7 changes: 5 additions & 2 deletions src/Renderer/XmlRenderer.php
Expand Up @@ -90,7 +90,7 @@ private function createLinkNode(DOMDocument $doc, string $rel, array $data)
}

/**
* Convert true, false, and null to appropriate strings.
* Convert true and false to appropriate strings.
*
* In all other cases, return the value as-is.
*
Expand All @@ -101,7 +101,6 @@ private function normalizeConstantValue($value)
{
$value = $value === true ? 'true' : $value;
$value = $value === false ? 'false' : $value;
$value = $value === null ? '' : $value;
return $value;
}

Expand All @@ -115,6 +114,10 @@ private function isAssocArray(array $value) : bool
*/
private function createResourceElement(DOMDocument $doc, string $name, $data)
{
if ($data === null) {
return $doc->createElement($name, $data);
}

if (is_scalar($data)) {
$data = $this->normalizeConstantValue($data);
return $doc->createElement($name, $data);
Expand Down
12 changes: 12 additions & 0 deletions test/Renderer/XmlRendererTest.php
Expand Up @@ -95,4 +95,16 @@ public function testCanRenderObjectsThatImplementToString()
$xml = $renderer->render($resource);
$this->assertContains((string) $instance, $xml);
}

public function testRendersNullValuesAsTagsWithNoContent()
{
$resource = new HalResource([
'key' => null,
]);
$resource = $resource->withLink(new Link('self', '/example'));

$renderer = new XmlRenderer();
$xml = $renderer->render($resource);
$this->assertContains('<key/>', $xml);
}
}

0 comments on commit 31250bd

Please sign in to comment.