Skip to content

Commit

Permalink
[RequestMapper]: Fix enum mapping when value is empty or null
Browse files Browse the repository at this point in the history
  • Loading branch information
fractalzombie committed Aug 8, 2022
1 parent 83ccab9 commit 604a629
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions Extractor/ParametersExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use FRZB\Component\PhpDocReader\Reader\ReaderInterface as PhpDocReader;
use FRZB\Component\RequestMapper\Helper\ClassHelper;
use FRZB\Component\RequestMapper\Helper\PropertyHelper;
use JetBrains\PhpStorm\Pure;

#[AsService]
class ParametersExtractor
Expand Down Expand Up @@ -39,11 +40,12 @@ private function mapProperties(array $properties, array $parameters): array
;
}

#[Pure]
private function mapEnum(string $enumClassName, mixed $value = null): ?\BackedEnum
{
return match (true) {
is_subclass_of($enumClassName, \IntBackedEnum::class) && \is_int($value) => $enumClassName::tryFrom($value),
is_subclass_of($enumClassName, \StringBackedEnum::class) && \is_string($value) => $enumClassName::tryFrom($value),
is_subclass_of($enumClassName, \IntBackedEnum::class) && \is_int($value) && !empty($value) => $enumClassName::tryFrom($value),
is_subclass_of($enumClassName, \StringBackedEnum::class) && \is_string($value) && !empty($value) => $enumClassName::tryFrom($value),
default => null,
};
}
Expand Down

0 comments on commit 604a629

Please sign in to comment.