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

Commit f17aad0

Browse files
committed
Merge branch 'hotfix/41-null-value-casting'
Close #41 Fixes #40
2 parents 112cebd + e29eacd commit f17aad0

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ Versions prior to 0.4.0 were released as the package "weierophinney/hal".
2424

2525
### Fixed
2626

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

2931
## 1.1.0 - 2018-06-05
3032

src/Renderer/XmlRenderer.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ private function createLinkNode(DOMDocument $doc, string $rel, array $data)
9090
}
9191

9292
/**
93-
* Convert true, false, and null to appropriate strings.
93+
* Convert true and false to appropriate strings.
9494
*
9595
* In all other cases, return the value as-is.
9696
*
@@ -101,7 +101,6 @@ private function normalizeConstantValue($value)
101101
{
102102
$value = $value === true ? 'true' : $value;
103103
$value = $value === false ? 'false' : $value;
104-
$value = $value === null ? '' : $value;
105104
return $value;
106105
}
107106

@@ -115,6 +114,10 @@ private function isAssocArray(array $value) : bool
115114
*/
116115
private function createResourceElement(DOMDocument $doc, string $name, $data)
117116
{
117+
if ($data === null) {
118+
return $doc->createElement($name, $data);
119+
}
120+
118121
if (is_scalar($data)) {
119122
$data = $this->normalizeConstantValue($data);
120123
return $doc->createElement($name, $data);

test/Renderer/XmlRendererTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,16 @@ public function testCanRenderObjectsThatImplementToString()
9595
$xml = $renderer->render($resource);
9696
$this->assertContains((string) $instance, $xml);
9797
}
98+
99+
public function testRendersNullValuesAsTagsWithNoContent()
100+
{
101+
$resource = new HalResource([
102+
'key' => null,
103+
]);
104+
$resource = $resource->withLink(new Link('self', '/example'));
105+
106+
$renderer = new XmlRenderer();
107+
$xml = $renderer->render($resource);
108+
$this->assertContains('<key/>', $xml);
109+
}
98110
}

0 commit comments

Comments
 (0)