From c0ef87ed28bdef7220e0740366ddc2c80205ecaf Mon Sep 17 00:00:00 2001 From: cyganm Date: Mon, 13 Jan 2020 20:26:35 -0600 Subject: [PATCH 1/2] Issue#11 Add null support to hydrateProperties --- src/ArrayHydrator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ArrayHydrator.php b/src/ArrayHydrator.php index 9c32c49..e9b0776 100644 --- a/src/ArrayHydrator.php +++ b/src/ArrayHydrator.php @@ -118,7 +118,7 @@ protected function hydrateProperties($entity, $data) foreach ($metaData->fieldNames as $fieldName) { $dataKey = $this->hydrateBy === self::HYDRATE_BY_FIELD ? $fieldName : $metaData->getColumnName($fieldName); - if (isset($data[$dataKey]) && !in_array($fieldName, $skipFields, true)) { + if (array_key_exists($dataKey, $data) && !in_array($fieldName, $skipFields, true)) { $value = $data[$dataKey]; if (array_key_exists('type', $metaData->fieldMappings[$fieldName])) { From 962e94f2a5d05e69804d4acc896f0047defeee2b Mon Sep 17 00:00:00 2001 From: cyganm Date: Mon, 13 Jan 2020 20:26:35 -0600 Subject: [PATCH 2/2] Issue#11 Add null support to hydrateProperties --- tests/unit/ArrayHydratorTest.php | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/unit/ArrayHydratorTest.php b/tests/unit/ArrayHydratorTest.php index 6c9a76c..5a81a1c 100644 --- a/tests/unit/ArrayHydratorTest.php +++ b/tests/unit/ArrayHydratorTest.php @@ -189,6 +189,26 @@ public function testHydrateAll() $this->assertEquals(5, $permissions[4]->getId()); } + public function testHydrateNullValues() + { + $data = [ + 'name' => null, + 'email' => null, + ]; + + $user = new Fixture\User; + /** @var Fixture\User $user */ + $user = $this->hydrator->hydrate($user, $data); + $user->setName('name'); + $user->setEmail('email'); + + $this->assertEquals('name', $user->getName()); + $this->assertEquals('email', $user->getEmail()); + $user = $this->hydrator->hydrate($user, $data); + $this->assertNull($user->getName()); + $this->assertNull($user->getEmail()); + } + /** * @expectedException \Exception */