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

Commit 41a15e8

Browse files
committed
Implement failing test case for #3, and initial solution
Adds a failing test case demonstrating that `DateTime` values trigger an exception currently. I also propose a solution that currently addresses objects with a `__toString()` mechanism. This does not solve the case of `DateTime`, however, as it does not implement that method.
1 parent 5259cda commit 41a15e8

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

src/Exception/InvalidResourceValueException.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,16 @@ public static function fromValue($value) : self
2020
HalResource::class
2121
));
2222
}
23+
24+
/**
25+
* @param object $object
26+
*/
27+
public static function fromObject($object) : self
28+
{
29+
return new self(sprintf(
30+
'Encountered object of type "%s" when serializing %s instance; unable to serialize',
31+
get_class($object),
32+
HalResource::class
33+
));
34+
}
2335
}

src/Renderer/XmlRenderer.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,11 @@ private function createResourceElement(DOMDocument $doc, string $name, $data)
112112
return $doc->createElement($name, $data);
113113
}
114114

115+
if (is_object($data)) {
116+
$data = $this->createDataFromObject($data);
117+
return $doc->createElement($name, $data);
118+
}
119+
115120
if (! is_array($data)) {
116121
throw Exception\InvalidResourceValueException::fromValue($data);
117122
}
@@ -142,4 +147,22 @@ private function createNodeTree(DOMDocument $doc, DOMNode $node, array $data) :
142147

143148
return $node;
144149
}
150+
151+
/**
152+
* @todo Detect JsonSerializable, and pass to
153+
* json_decode(json_encode($object), true), passing the final value
154+
* back to createResourceElement()?
155+
* @todo How should we handle DateTimeInterface implementations?
156+
* $date->format('c')?
157+
* @param object $object
158+
* @throws Exception\InvalidResourceValueException if unable to serialize
159+
* the data to a string.
160+
*/
161+
private function createDataFromObject($object) : string
162+
{
163+
if (! method_exists($object, '__toString')) {
164+
throw Exception\InvalidResourceValueException::fromObject($object);
165+
}
166+
return (string) $object;
167+
}
145168
}

test/Renderer/XmlRendererTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77

88
namespace ZendTest\Expressive\Hal\Renderer;
99

10+
use DateTime;
1011
use PHPUnit\Framework\TestCase;
12+
use Zend\Expressive\Hal\HalResource;
13+
use Zend\Expressive\Hal\Link;
1114
use Zend\Expressive\Hal\Renderer\XmlRenderer;
1215

1316
class XmlRendererTest extends TestCase
@@ -61,4 +64,17 @@ public function testRendersExpectedXmlPayload()
6164

6265
$this->assertSame($expected, $renderer->render($resource));
6366
}
67+
68+
public function testCanRenderPhpDateTimeInstances()
69+
{
70+
$dateTime = new DateTime('now');
71+
$resource = new HalResource([
72+
'date' => $dateTime,
73+
]);
74+
$resource = $resource->withLink(new Link('self', '/example'));
75+
76+
$renderer = new XmlRenderer();
77+
$xml = $renderer->render($resource);
78+
$this->assertContains((string) $dateTime, $xml);
79+
}
6480
}

0 commit comments

Comments
 (0)