Skip to content

Commit

Permalink
Merge pull request #12 from TomHAnderson/feature/null-embedded-resources
Browse files Browse the repository at this point in the history
Allow null for embedded resource
  • Loading branch information
TomHAnderson authored Jan 30, 2022
2 parents 52dcc59 + 4fb4572 commit 48df2d3
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
13 changes: 7 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
vendor
.phpunit.cache
coverage
composer.lock
.vscode
.phpcs-cache
/vendor
/.phpunit.cache
/coverage
/composer.lock
/.vscode
/.phpcs-cache
/.idea
5 changes: 4 additions & 1 deletion src/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use function array_merge;
use function array_push;
use function is_array;
use function is_null;

class Resource
{
Expand Down Expand Up @@ -62,7 +63,7 @@ public function addLink(string $reference, $definition): self
return $this;
}

public function addEmbeddedResource(string $ref, Resource $resource): self
public function addEmbeddedResource(string $ref, Resource |null $resource): self
{
$this->embedded[$ref] = $resource;

Expand Down Expand Up @@ -118,6 +119,8 @@ public function toArray(): array
foreach ($resources as $resource) {
$data['_embedded'][$ref][] = $resource->toArray();
}
} elseif ($resources === null) {
$data['_embedded'][$ref] = $resources;
} else {
$data['_embedded'][$ref] = $resources->toArray();
}
Expand Down
10 changes: 10 additions & 0 deletions test/ResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ public function testEmbeddedResource(): void
$this->assertEquals('testing', $resource1->toArray()['_embedded']['embedded']['test']);
}

public function testNullEmbeddedResource(): void
{
$hydratorManager = new HydratorManager();
$resource1 = $hydratorManager->resource();
$resource1->addEmbeddedResource('embedded', null);
$result = $resource1->toArray();

$this->assertNull($result['_embedded']['embedded']);
}

public function testEmbeddedResources(): void
{
$hydratorManager = new HydratorManager();
Expand Down

0 comments on commit 48df2d3

Please sign in to comment.