Skip to content

Commit

Permalink
fix: nullable property
Browse files Browse the repository at this point in the history
  • Loading branch information
yzen-dev committed Jul 12, 2023
1 parent 5f78b33 commit 1f01828
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion composer.json
@@ -1,6 +1,6 @@
{
"name": "yzen.dev/plain-to-class",
"version": "3.0.2",
"version": "3.0.3",
"description": "Class-transformer to transform your dataset into a structured object",
"minimum-stability": "dev",
"prefer-stable": true,
Expand Down
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/ValueCasting.php
Expand Up @@ -47,7 +47,7 @@ public function __construct(ReflectionProperty $property, HydratorConfig $config
*/
public function castAttribute(mixed $value): mixed
{
if ($this->property->type->isNullable && empty($value)) {
if ($this->property->type->isNullable && $value === null) {
return null;
}

Expand Down
8 changes: 4 additions & 4 deletions tests/Units/ValueCastingTest.php
Expand Up @@ -50,19 +50,19 @@ public function testCreateProperty(): void


$caster = new ValueCasting(new RuntimeReflectionProperty(new \ReflectionProperty(TypesDto::class, 'nullableInt')));
$value = $caster->castAttribute('');
$value = $caster->castAttribute(null);
$this->assertNull($value);

$caster = new ValueCasting(new RuntimeReflectionProperty(new \ReflectionProperty(TypesDto::class, 'nullableString')));
$value = $caster->castAttribute('');
$value = $caster->castAttribute(null);
$this->assertNull($value);

$caster = new ValueCasting(new RuntimeReflectionProperty(new \ReflectionProperty(TypesDto::class, 'nullableFloat')));
$value = $caster->castAttribute('');
$value = $caster->castAttribute(null);
$this->assertNull($value);

$caster = new ValueCasting(new RuntimeReflectionProperty(new \ReflectionProperty(TypesDto::class, 'nullableBool')));
$value = $caster->castAttribute('');
$value = $caster->castAttribute(null);
$this->assertNull($value);
}

Expand Down

0 comments on commit 1f01828

Please sign in to comment.