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

Fix null value throwing error #41

Merged
merged 4 commits into from Dec 11, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.md
Expand Up @@ -24,7 +24,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);
}
}