From fb2e1843226fbaa45e237b3f1184aead431b5c2b Mon Sep 17 00:00:00 2001 From: david_smith Date: Thu, 24 Oct 2024 08:48:59 -0400 Subject: [PATCH 1/2] Apply missing_as_null to property in `DataModel.php`. --- src/DataModel.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/DataModel.php b/src/DataModel.php index 9cbb3e9..872741a 100644 --- a/src/DataModel.php +++ b/src/DataModel.php @@ -240,7 +240,6 @@ public static function from(iterable|object|null $context = []): self $self->{$methods[$property_name]}($context[$context_key] ?? null, $context, $Attribute, $ReflectionProperty); continue; } - /** When a property name does not match a key name */ if (!array_key_exists($context_key, $context)) { if (isset($Describe->default)) { @@ -250,6 +249,10 @@ public static function from(iterable|object|null $context = []): self if (isset($Describe->required) && $Describe->required) { throw new PropertyRequiredException("Property: $property_name is required"); } + if (isset($Describe->missing_as_null) && $Describe?->missing_as_null) { + $self->{$property_name} = null; + continue; + } if (isset($ClassDescribe->missing_as_null) && $ClassDescribe?->missing_as_null) { $self->{$property_name} = null; continue; From 3bd502ad0e0b329f24590d8829a39fb4d8a2d7ea Mon Sep 17 00:00:00 2001 From: david_smith Date: Thu, 24 Oct 2024 08:49:05 -0400 Subject: [PATCH 2/2] Add tests. --- tests/Unit/Metadata/MissingAsNull/User.php | 3 ++- .../MissingAsNullProperty/MissingAsNullTest.php | 17 +++++++++++++++++ .../Metadata/MissingAsNullProperty/User.php | 17 +++++++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 tests/Unit/Metadata/MissingAsNullProperty/MissingAsNullTest.php create mode 100644 tests/Unit/Metadata/MissingAsNullProperty/User.php diff --git a/tests/Unit/Metadata/MissingAsNull/User.php b/tests/Unit/Metadata/MissingAsNull/User.php index 4f658d8..171dda2 100644 --- a/tests/Unit/Metadata/MissingAsNull/User.php +++ b/tests/Unit/Metadata/MissingAsNull/User.php @@ -2,12 +2,13 @@ namespace Tests\Unit\Metadata\MissingAsNull; +use Zerotoprod\DataModel\DataModel; use Zerotoprod\DataModel\Describe; #[Describe(['missing_as_null' => true])] class User { - use \Zerotoprod\DataModel\DataModel; + use DataModel; public ?string $name; diff --git a/tests/Unit/Metadata/MissingAsNullProperty/MissingAsNullTest.php b/tests/Unit/Metadata/MissingAsNullProperty/MissingAsNullTest.php new file mode 100644 index 0000000..73d6c99 --- /dev/null +++ b/tests/Unit/Metadata/MissingAsNullProperty/MissingAsNullTest.php @@ -0,0 +1,17 @@ +assertNull($User->name); + $this->assertEquals(2, $User->age); + } +} \ No newline at end of file diff --git a/tests/Unit/Metadata/MissingAsNullProperty/User.php b/tests/Unit/Metadata/MissingAsNullProperty/User.php new file mode 100644 index 0000000..0243707 --- /dev/null +++ b/tests/Unit/Metadata/MissingAsNullProperty/User.php @@ -0,0 +1,17 @@ + true])] +class User +{ + use \Zerotoprod\DataModel\DataModel; + + #[Describe(['missing_as_null' => true])] + public ?string $name; + + #[Describe(['default' => 2])] + public ?int $age; +} \ No newline at end of file