From 2451ff1d110e4ea3b36fdfc22ccfc14d4882f0a6 Mon Sep 17 00:00:00 2001 From: david <61474950+zero-to-prod@users.noreply.github.com> Date: Wed, 20 Nov 2024 07:23:46 -0500 Subject: [PATCH] Use `!isset($context[$Property->getName()])` for evaluating a nullable property. (#23) Co-authored-by: david_smith --- src/DataModelHelper.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/DataModelHelper.php b/src/DataModelHelper.php index 992f701..6935ffe 100644 --- a/src/DataModelHelper.php +++ b/src/DataModelHelper.php @@ -62,7 +62,7 @@ public static function mapOf(mixed $value, array $context, ?ReflectionAttribute throw new PropertyRequiredException("Property `\${$Property->getName()}` is required."); } - if (!$value && $Property->getType()?->allowsNull()) { + if (!isset($context[$Property->getName()]) && $Property->getType()?->allowsNull()) { return null; } @@ -122,7 +122,7 @@ public static function pregReplace(mixed $value, array $context, ?ReflectionAttr throw new PropertyRequiredException("Property `\${$Property->getName()}` is required."); } - if (!$value) { + if (!isset($context[$Property->getName()])) { return $Property->getType()?->allowsNull() ? null : ''; @@ -156,7 +156,7 @@ public static function pregMatch(mixed $value, array $context, ?ReflectionAttrib throw new PropertyRequiredException("Property `\${$Property->getName()}` is required."); } - if (!$value && $Property->getType()?->allowsNull()) { + if (!isset($context[$Property->getName()]) && $Property->getType()?->allowsNull()) { return null; } @@ -196,7 +196,7 @@ public static function isUrl(mixed $value, array $context, ?ReflectionAttribute throw new PropertyRequiredException("Property `\${$Property->getName()}` is required."); } - if (!$value && $Property->getType()?->allowsNull()) { + if (!isset($context[$Property->getName()]) && $Property->getType()?->allowsNull()) { return null; } @@ -242,7 +242,7 @@ public static function isEmail(mixed $value, array $context, ?ReflectionAttribut throw new PropertyRequiredException("Property `\${$Property->getName()}` is required."); } - if (!$value && $Property->getType()?->allowsNull()) { + if (!isset($context[$Property->getName()]) && $Property->getType()?->allowsNull()) { return null; } @@ -289,7 +289,7 @@ public static function isMultiple(mixed $value, array $context, ?ReflectionAttri throw new PropertyRequiredException("Property `\${$Property->getName()}` is required."); } - if (!$value && $Property->getType()?->allowsNull()) { + if (!isset($context[$Property->getName()]) && $Property->getType()?->allowsNull()) { return null; } @@ -336,7 +336,7 @@ public static function when(mixed $value, array $context, ?ReflectionAttribute $ throw new PropertyRequiredException("Property `\${$Property->getName()}` is required."); } - if (!$value && $Property->getType()?->allowsNull()) { + if (!isset($context[$Property->getName()]) && $Property->getType()?->allowsNull()) { return null; }