From e187d63a4abcf43827ec168d49bafc1cf8418cb0 Mon Sep 17 00:00:00 2001 From: Unknown Date: Wed, 28 Mar 2018 21:58:22 +0200 Subject: [PATCH 1/5] Allow empty array in data - added check for empty array in data to avoid unnecessary embedded data with empty lists --- src/HalResource.php | 4 ++-- test/HalResourceTest.php | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/HalResource.php b/src/HalResource.php index 40b0db3..21d540a 100644 --- a/src/HalResource.php +++ b/src/HalResource.php @@ -61,7 +61,7 @@ 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; } @@ -148,7 +148,7 @@ 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); } diff --git a/test/HalResourceTest.php b/test/HalResourceTest.php index 763eae4..4070789 100644 --- a/test/HalResourceTest.php +++ b/test/HalResourceTest.php @@ -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 */ @@ -222,6 +230,15 @@ public function testWithElementProxiesToEmbedIfResourceCollectionValueProvided() $this->assertEquals(['foo' => $collection], $new->getElements()); } + public function testWithElementNotProxiesToEmbededIfEmptyArrayValueProvided() + { + $resource = new HalResource(['foo' => 'bar']); + $new = $resource->withElement('bar', []); + + $representation = $new->toArray(); + $this->assertEquals(['foo' => 'bar', 'bar' => []], $representation); + } + /** * @dataProvider invalidElementNames */ From f7a4179e357b5d3881d610513ab9c3b371f0c5cd Mon Sep 17 00:00:00 2001 From: Unknown Date: Wed, 28 Mar 2018 22:11:51 +0200 Subject: [PATCH 2/5] Updated Changelog - added changelog entry regarding fix - fixed code style issues --- CHANGELOG.md | 4 +++- src/HalResource.php | 4 ++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 82e8a8c..ca89494 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,7 +24,9 @@ Versions prior to 0.4.0 were released as the package "weierophinney/hal". ### Fixed -- Nothing. +- [#34](https://github.com/zendframework/zend-expressive-hal/pull/37) adds ability + to provide _empty_ values eg. empty array as element data and avoids addition of + of empty data as **embedded** element. ## 1.0.0 - 2018-03-15 diff --git a/src/HalResource.php b/src/HalResource.php index 21d540a..3f13118 100644 --- a/src/HalResource.php +++ b/src/HalResource.php @@ -61,7 +61,7 @@ public function __construct(array $data = [], array $links = [], array $embedded $context = __CLASS__; array_walk($data, function ($value, $name) use ($context) { $this->validateElementName($name, $context); - if (!empty($value) && ($value instanceof self || $this->isResourceCollection($value, $name, $context))) { + if (! empty($value) && ($value instanceof self || $this->isResourceCollection($value, $name, $context))) { $this->embedded[$name] = $value; return; } @@ -148,7 +148,7 @@ public function withElement(string $name, $value) : HalResource { $this->validateElementName($name, __METHOD__); - if (!empty($value) && ($value instanceof self || $this->isResourceCollection($value, $name, __METHOD__))) { + if (! empty($value) && ($value instanceof self || $this->isResourceCollection($value, $name, __METHOD__))) { return $this->embed($name, $value); } From 7e42a904abe365228048a5d8d783309c457732db Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Wed, 4 Apr 2018 13:01:27 -0500 Subject: [PATCH 3/5] Rephrases the CHANGELOG entry for #37 - `s/34/37/` in description. - Better description describing change in behavior, and how to get original behavior back. --- CHANGELOG.md | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 74b1e7a..db2dfa6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 @@ -24,9 +24,11 @@ Versions prior to 0.4.0 were released as the package "weierophinney/hal". ### Fixed -- [#34](https://github.com/zendframework/zend-expressive-hal/pull/37) adds ability - to provide _empty_ values eg. empty array as element data and avoids addition of - of empty data as **embedded** element. +- [#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 From 943d8438127e8d7c21d501c1ab0463d027239ab3 Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Wed, 4 Apr 2018 13:02:19 -0500 Subject: [PATCH 4/5] Wrap long conditionals onto multiple lines --- src/HalResource.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/HalResource.php b/src/HalResource.php index 3f13118..2983c6c 100644 --- a/src/HalResource.php +++ b/src/HalResource.php @@ -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 (! empty($value) && ($value instanceof self || $this->isResourceCollection($value, $name, $context))) { + if (! empty($value) + && ($value instanceof self || $this->isResourceCollection($value, $name, $context)) + ) { $this->embedded[$name] = $value; return; } @@ -148,7 +150,9 @@ public function withElement(string $name, $value) : HalResource { $this->validateElementName($name, __METHOD__); - if (! empty($value) && ($value instanceof self || $this->isResourceCollection($value, $name, __METHOD__))) { + if (! empty($value) + && ($value instanceof self || $this->isResourceCollection($value, $name, __METHOD__)) + ) { return $this->embed($name, $value); } From c7a13b263ebcd84404af4fd361a1b6a19c880d93 Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Wed, 4 Apr 2018 13:03:25 -0500 Subject: [PATCH 5/5] Improve test names --- test/HalResourceTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/HalResourceTest.php b/test/HalResourceTest.php index 4070789..8487dc1 100644 --- a/test/HalResourceTest.php +++ b/test/HalResourceTest.php @@ -230,7 +230,7 @@ public function testWithElementProxiesToEmbedIfResourceCollectionValueProvided() $this->assertEquals(['foo' => $collection], $new->getElements()); } - public function testWithElementNotProxiesToEmbededIfEmptyArrayValueProvided() + public function testWithElementDoesNotProxyToEmbedIfAnEmptyArrayValueIsProvided() { $resource = new HalResource(['foo' => 'bar']); $new = $resource->withElement('bar', []);