Skip to content

Commit

Permalink
Merge pull request #12 from cyganm/patch-1
Browse files Browse the repository at this point in the history
#11 Add null support to hydrateProperties
  • Loading branch information
pmill authored Jan 14, 2020
2 parents 2bd7a42 + 962e94f commit 5ea4554
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/ArrayHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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])) {
Expand Down
20 changes: 20 additions & 0 deletions tests/unit/ArrayHydratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down

0 comments on commit 5ea4554

Please sign in to comment.