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

Commit 68fa1c6

Browse files
committed
Merge branch 'hotfix/37'
Close #37 Fixes #34
2 parents ce89fbc + c7a13b2 commit 68fa1c6

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

CHANGELOG.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ All notable changes to this project will be documented in this file, in reverse
44

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

7-
## 1.0.2 - TBD
7+
## 1.0.2 - 2018-04-04
88

99
### Added
1010

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

2525
### Fixed
2626

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

2933
## 1.0.1 - 2018-03-28
3034

src/HalResource.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ public function __construct(array $data = [], array $links = [], array $embedded
6161
$context = __CLASS__;
6262
array_walk($data, function ($value, $name) use ($context) {
6363
$this->validateElementName($name, $context);
64-
if ($value instanceof self || $this->isResourceCollection($value, $name, $context)) {
64+
if (! empty($value)
65+
&& ($value instanceof self || $this->isResourceCollection($value, $name, $context))
66+
) {
6567
$this->embedded[$name] = $value;
6668
return;
6769
}
@@ -148,7 +150,9 @@ public function withElement(string $name, $value) : HalResource
148150
{
149151
$this->validateElementName($name, __METHOD__);
150152

151-
if ($value instanceof self || $this->isResourceCollection($value, $name, __METHOD__)) {
153+
if (! empty($value)
154+
&& ($value instanceof self || $this->isResourceCollection($value, $name, __METHOD__))
155+
) {
152156
return $this->embed($name, $value);
153157
}
154158

test/HalResourceTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,14 @@ public function testNonResourceOrCollectionItemsRaiseExceptionDuringConstruction
9292
$resource = new HalResource([], [], ['foo' => 'bar']);
9393
}
9494

95+
public function testEmptyArrayAsDataWillNotBeEmbeddedDuringConstruction()
96+
{
97+
$resource = new HalResource(['bar' => []]);
98+
$this->assertEquals(['bar' => []], $resource->getElements());
99+
$representation = $resource->toArray();
100+
$this->assertArrayNotHasKey('_embeded', $representation);
101+
}
102+
95103
/**
96104
* @dataProvider invalidElementNames
97105
*/
@@ -222,6 +230,15 @@ public function testWithElementProxiesToEmbedIfResourceCollectionValueProvided()
222230
$this->assertEquals(['foo' => $collection], $new->getElements());
223231
}
224232

233+
public function testWithElementDoesNotProxyToEmbedIfAnEmptyArrayValueIsProvided()
234+
{
235+
$resource = new HalResource(['foo' => 'bar']);
236+
$new = $resource->withElement('bar', []);
237+
238+
$representation = $new->toArray();
239+
$this->assertEquals(['foo' => 'bar', 'bar' => []], $representation);
240+
}
241+
225242
/**
226243
* @dataProvider invalidElementNames
227244
*/

0 commit comments

Comments
 (0)