diff --git a/src/DataTransferObject.php b/src/DataTransferObject.php index 960d997..522ec6b 100644 --- a/src/DataTransferObject.php +++ b/src/DataTransferObject.php @@ -74,7 +74,11 @@ protected function setItems(array $items): void protected function getValueByKey(array $items, string $key, string $default) { - return Arr::get($items, $key) ?: Arr::get($items, $default); + if (Arr::exists($items, $key)) { + return Arr::get($items, $key); + } + + return Arr::get($items, $default); } /** diff --git a/tests/Unit/MapTest.php b/tests/Unit/MapTest.php index 8f23e2a..0602aef 100644 --- a/tests/Unit/MapTest.php +++ b/tests/Unit/MapTest.php @@ -58,4 +58,23 @@ public function testToArray() 'baz' => $this->baz, ], $object->toArray()); } + + public function testMissingValues() + { + $object = new Map([ + 'wa' => [ + 'sd' => $this->foo, + ], + + 'qwe.rty' => false, + ]); + + $this->assertIsArray($object->toArray()); + + $this->assertSame([ + 'foo' => $this->foo, + 'bar' => false, + 'baz' => null, + ], $object->toArray()); + } }