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

Commit 31250bd

Browse files
committed
Merge branch 'hotfix/41-null-value-casting' into develop
Forward port #41
2 parents 9b93cc8 + e29eacd commit 31250bd

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
@@ -46,7 +46,9 @@ Versions prior to 0.4.0 were released as the package "weierophinney/hal".
4646

4747
### Fixed
4848

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

5153
## 1.1.0 - 2018-06-05
5254

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)