Skip to content

Commit

Permalink
Merge pull request #15 from sunrise-php/release/v2.4.0
Browse files Browse the repository at this point in the history
v2.4.0
  • Loading branch information
fenric authored Apr 11, 2022
2 parents d57b175 + cdc337b commit 28c7e46
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Hydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,10 @@ private function hydrateProperty(
ReflectionNamedType $type,
$value
) : void {
if ('' === $value && 'string' !== $type->getName()) {
$value = null;
}

if (null === $value) {
$this->hydratePropertyWithNull($object, $class, $property, $type);
return;
Expand Down
18 changes: 18 additions & 0 deletions tests/HydratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -347,4 +347,22 @@ public function testHydratePropertyWithInvalidInterval() : void
'duration' => 'fuuu',
]);
}

public function testHydratePropertyWithEmptyStringIntegerNumber() : void
{
$object = (new Hydrator)->hydrate(Fixtures\ObjectWithNullableIntegerProperty::class, [
'value' => '',
]);

$this->assertNull($object->value);
}

public function testHydratePropertyWithEmptyString() : void
{
$object = (new Hydrator)->hydrate(Fixtures\ObjectWithStringProperty::class, [
'value' => '',
]);

$this->assertSame('', $object->value);
}
}
10 changes: 10 additions & 0 deletions tests/fixtures/ObjectWithNullableIntegerProperty.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Sunrise\Hydrator\Tests\Fixtures;

final class ObjectWithNullableIntegerProperty
{
public ?int $value;
}

0 comments on commit 28c7e46

Please sign in to comment.