From 2abc593a3bb7a80bdb9ad94f3e9c47ead65ea69f Mon Sep 17 00:00:00 2001 From: Radim Vaculik Date: Tue, 11 Jan 2022 13:42:25 +0100 Subject: [PATCH] Support nullable classes definition with '?' --- src/Entity/Reflection/MetadataParser.php | 1 + .../Reflection/PropertyMetadata.isValid().phpt | 16 ++++++++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/Entity/Reflection/MetadataParser.php b/src/Entity/Reflection/MetadataParser.php index da5ed789..6ba19d57 100644 --- a/src/Entity/Reflection/MetadataParser.php +++ b/src/Entity/Reflection/MetadataParser.php @@ -225,6 +225,7 @@ protected function parseAnnotationTypes(PropertyMetadata $property, string $type if (($type[0] ?? '') === '?') { $isNullable = true; $typeLower = substr($typeLower, 1); + $type = substr($type, 1); } if (strpos($type, '[') !== false) { // string[] $type = 'array'; diff --git a/tests/cases/unit/Entity/Reflection/PropertyMetadata.isValid().phpt b/tests/cases/unit/Entity/Reflection/PropertyMetadata.isValid().phpt index 1af5829f..4072d97b 100644 --- a/tests/cases/unit/Entity/Reflection/PropertyMetadata.isValid().phpt +++ b/tests/cases/unit/Entity/Reflection/PropertyMetadata.isValid().phpt @@ -34,7 +34,8 @@ $dic = require_once __DIR__ . '/../../../../bootstrap.php'; * @property scalar $scalar * @property mixed $mixed * @property ArrayHash $type - * @property bool|NULL $nullable + * @property bool|NULL $nullable1 + * @property ?\DateTimeImmutable $nullable2 */ class ValidationTestEntity { @@ -282,7 +283,7 @@ class PropertyMetadataIsValidTest extends TestCase public function testNullable(): void { - $property = $this->metadata->getProperty('nullable'); + $property = $this->metadata->getProperty('nullable1'); $val = null; Assert::true($property->isValid($val)); @@ -293,6 +294,17 @@ class PropertyMetadataIsValidTest extends TestCase $val = 0; Assert::true($property->isValid($val)); Assert::false($val); + + $property = $this->metadata->getProperty('nullable2'); + + $val = null; + Assert::true($property->isValid($val)); + + $val = false; + Assert::false($property->isValid($val)); + + $val = new DateTimeImmutable; + Assert::true($property->isValid($val)); }