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/37'
Browse files Browse the repository at this point in the history
Close #37
Fixes #34
  • Loading branch information
weierophinney committed Apr 4, 2018
2 parents ce89fbc + c7a13b2 commit 68fa1c6
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
8 changes: 6 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ 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".

## 1.0.2 - TBD
## 1.0.2 - 2018-04-04

### Added

Expand All @@ -24,7 +24,11 @@ Versions prior to 0.4.0 were released as the package "weierophinney/hal".

### Fixed

- Nothing.
- [#37](https://github.com/zendframework/zend-expressive-hal/pull/37) modifies
`HalResource` to no longer treat empty arrays as embedded collections when
passed via the constructor or `withElement()`. If an empty embedded collection
is required, use `embed()` with a boolean third argument to force
representation as an array of resources.

## 1.0.1 - 2018-03-28

Expand Down
8 changes: 6 additions & 2 deletions src/HalResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ public function __construct(array $data = [], array $links = [], array $embedded
$context = __CLASS__;
array_walk($data, function ($value, $name) use ($context) {
$this->validateElementName($name, $context);
if ($value instanceof self || $this->isResourceCollection($value, $name, $context)) {
if (! empty($value)
&& ($value instanceof self || $this->isResourceCollection($value, $name, $context))
) {
$this->embedded[$name] = $value;
return;
}
Expand Down Expand Up @@ -148,7 +150,9 @@ public function withElement(string $name, $value) : HalResource
{
$this->validateElementName($name, __METHOD__);

if ($value instanceof self || $this->isResourceCollection($value, $name, __METHOD__)) {
if (! empty($value)
&& ($value instanceof self || $this->isResourceCollection($value, $name, __METHOD__))
) {
return $this->embed($name, $value);
}

Expand Down
17 changes: 17 additions & 0 deletions test/HalResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ public function testNonResourceOrCollectionItemsRaiseExceptionDuringConstruction
$resource = new HalResource([], [], ['foo' => 'bar']);
}

public function testEmptyArrayAsDataWillNotBeEmbeddedDuringConstruction()
{
$resource = new HalResource(['bar' => []]);
$this->assertEquals(['bar' => []], $resource->getElements());
$representation = $resource->toArray();
$this->assertArrayNotHasKey('_embeded', $representation);
}

/**
* @dataProvider invalidElementNames
*/
Expand Down Expand Up @@ -222,6 +230,15 @@ public function testWithElementProxiesToEmbedIfResourceCollectionValueProvided()
$this->assertEquals(['foo' => $collection], $new->getElements());
}

public function testWithElementDoesNotProxyToEmbedIfAnEmptyArrayValueIsProvided()
{
$resource = new HalResource(['foo' => 'bar']);
$new = $resource->withElement('bar', []);

$representation = $new->toArray();
$this->assertEquals(['foo' => 'bar', 'bar' => []], $representation);
}

/**
* @dataProvider invalidElementNames
*/
Expand Down

0 comments on commit 68fa1c6

Please sign in to comment.