Skip to content

Commit 4913da6

Browse files
committed
fix(mapper): make EnumCaster nullable-aware for empty string and null-like input
1 parent db2f45a commit 4913da6

1 file changed

Lines changed: 9 additions & 1 deletion

File tree

packages/mapper/src/Casters/EnumCaster.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
*/
2323
public function __construct(
2424
private string $enum,
25+
private bool $nullable = false,
2526
) {}
2627

2728
public static function accepts(PropertyReflector|TypeReflector $input): bool
@@ -35,11 +36,18 @@ public static function accepts(PropertyReflector|TypeReflector $input): bool
3536

3637
public static function configure(PropertyReflector $property, Context $context): self
3738
{
38-
return new self(enum: $property->getType()->getName());
39+
return new self(
40+
enum: $property->getType()->getName(),
41+
nullable: $property->isNullable(),
42+
);
3943
}
4044

4145
public function cast(mixed $input): ?object
4246
{
47+
if ($this->nullable && ($input === null || $input === '' || is_string($input) && mb_strtolower($input) === 'null')) {
48+
return null;
49+
}
50+
4351
if ($input === null) {
4452
return null;
4553
}

0 commit comments

Comments
 (0)