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/3-datetime-rendering'
Browse files Browse the repository at this point in the history
Close #27
Fixes #3
  • Loading branch information
weierophinney committed Jan 3, 2018
2 parents 5259cda + e5812fa commit 948285c
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 0 deletions.
26 changes: 26 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,32 @@ All notable changes to this project will be documented in this file, in reverse

Versions prior to 0.4.0 were released as the package "weierophinney/hal".

## 0.6.2 - 2018-01-03

### Added

- Nothing.

### Changed

- [#27](https://github.com/zendframework/zend-expressive-hal/pull/27) modifies
the `XmlRenderer` to raise an exception when attempting to render objects that
are not serializable to strings.

### Deprecated

- Nothing.

### Removed

- Nothing.

### Fixed

- [#27](https://github.com/zendframework/zend-expressive-hal/pull/27) adds
handling for `DateTime` and string serializable objects to the `XmlRenderer`,
allowing them to be rendered.

## 0.6.1 - 2017-12-12

### Added
Expand Down
12 changes: 12 additions & 0 deletions src/Exception/InvalidResourceValueException.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,16 @@ public static function fromValue($value) : self
HalResource::class
));
}

/**
* @param object $object
*/
public static function fromObject($object) : self
{
return new self(sprintf(
'Encountered object of type "%s" when serializing %s instance; unable to serialize',
get_class($object),
HalResource::class
));
}
}
27 changes: 27 additions & 0 deletions src/Renderer/XmlRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Zend\Expressive\Hal\Renderer;

use DateTimeInterface;
use DOMDocument;
use DOMNode;
use Zend\Expressive\Hal\HalResource;
Expand Down Expand Up @@ -112,6 +113,11 @@ private function createResourceElement(DOMDocument $doc, string $name, $data)
return $doc->createElement($name, $data);
}

if (is_object($data)) {
$data = $this->createDataFromObject($data);
return $doc->createElement($name, $data);
}

if (! is_array($data)) {
throw Exception\InvalidResourceValueException::fromValue($data);
}
Expand Down Expand Up @@ -142,4 +148,25 @@ private function createNodeTree(DOMDocument $doc, DOMNode $node, array $data) :

return $node;
}

/**
* @todo Detect JsonSerializable, and pass to
* json_decode(json_encode($object), true), passing the final value
* back to createResourceElement()?
* @param object $object
* @throws Exception\InvalidResourceValueException if unable to serialize
* the data to a string.
*/
private function createDataFromObject($object) : string
{
if ($object instanceof DateTimeInterface) {
return $object->format('c');
}

if (! method_exists($object, '__toString')) {
throw Exception\InvalidResourceValueException::fromObject($object);
}

return (string) $object;
}
}
35 changes: 35 additions & 0 deletions test/Renderer/XmlRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@

namespace ZendTest\Expressive\Hal\Renderer;

use DateTime;
use PHPUnit\Framework\TestCase;
use stdClass;
use Zend\Expressive\Hal\HalResource;
use Zend\Expressive\Hal\Link;
use Zend\Expressive\Hal\Renderer\XmlRenderer;
use ZendTest\Expressive\Hal\TestAsset\StringSerializable;

class XmlRendererTest extends TestCase
{
Expand Down Expand Up @@ -61,4 +66,34 @@ public function testRendersExpectedXmlPayload()

$this->assertSame($expected, $renderer->render($resource));
}

/**
* @see https://github.com/zendframework/zend-expressive-hal/issues/3
*/
public function testCanRenderPhpDateTimeInstances()
{
$dateTime = new DateTime('now');
$resource = new HalResource([
'date' => $dateTime,
]);
$resource = $resource->withLink(new Link('self', '/example'));

$renderer = new XmlRenderer();
$xml = $renderer->render($resource);
$this->assertContains($dateTime->format('c'), $xml);
}

public function testCanRenderObjectsThatImplementToString()
{
$instance = new StringSerializable();

$resource = new HalResource([
'key' => $instance,
]);
$resource = $resource->withLink(new Link('self', '/example'));

$renderer = new XmlRenderer();
$xml = $renderer->render($resource);
$this->assertContains((string) $instance, $xml);
}
}
18 changes: 18 additions & 0 deletions test/TestAsset/StringSerializable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
/**
* @see https://github.com/zendframework/zend-expressive-hal for the canonical source repository
* @copyright Copyright (c) 2017 Zend Technologies USA Inc. (https://www.zend.com)
* @license https://github.com/zendframework/zend-expressive-hal/blob/master/LICENSE.md New BSD License
*/

declare(strict_types=1);

namespace ZendTest\Expressive\Hal\TestAsset;

class StringSerializable
{
public function __toString()
{
return __METHOD__;
}
}

0 comments on commit 948285c

Please sign in to comment.