Skip to content

Commit

Permalink
tweak: nested empty arrays in objects
Browse files Browse the repository at this point in the history
  • Loading branch information
g105b committed Jul 13, 2023
1 parent 53e355a commit 3b05cc3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/DataObjectBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ public function fromObject(
$value = $this->fromObject($value, $dataObjectClass);
}
elseif(is_array($value)) {
if(is_int(key($value))) {
if(empty($value)) {
$value = [];
}
elseif(is_int(key($value))) {
array_walk($value, function(&$element)use($dataObjectClass) {
if(is_object($element)) {
$element = $this->fromObject($element, $dataObjectClass);
Expand Down
13 changes: 13 additions & 0 deletions test/phpunit/DataObjectBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,4 +193,17 @@ public function testEmptyNestedArray():void {
self::assertSame("value2", $output->getString("key2"));
self::assertSame([], $output->getArray("nested"));
}

public function testEmptyNestedArrayInObject():void {
$object = (object)[
"key1" => "value1",
"key2" => "value2",
"nested" => [],
];
$sut = new DataObjectBuilder();
$output = $sut->fromObject($object);

self::assertSame("value2", $output->getString("key2"));
self::assertSame([], $output->getArray("nested"));
}
}

0 comments on commit 3b05cc3

Please sign in to comment.