diff --git a/src/Internal/Marshaller/Type/EnumType.php b/src/Internal/Marshaller/Type/EnumType.php new file mode 100644 index 00000000..0d1fdad7 --- /dev/null +++ b/src/Internal/Marshaller/Type/EnumType.php @@ -0,0 +1,62 @@ +classFQCN = $class; + parent::__construct($marshaller); + } + + public static function match(\ReflectionNamedType $type): bool + { + return $type->getName() === 'enum'; + } + + public function parse($value, $current) + { + if ($value === null) { + return null; + } + + if (is_array($value)) { + $value = $value['name']; + } + + return ($this->classFQCN)::from($value); + } + + /** + * @psalm-suppress UndefinedDocblockClass + * @return \UnitEnum|null + */ + public function serialize($value) + { + return $value; + } +} diff --git a/tests/Unit/DTO/Enum/EnumDTO.php b/tests/Unit/DTO/Enum/EnumDTO.php new file mode 100644 index 00000000..6de9ef62 --- /dev/null +++ b/tests/Unit/DTO/Enum/EnumDTO.php @@ -0,0 +1,24 @@ +markTestSkipped(); + } + + $dto = new EnumDTO(); + $dto->simpleEnum = SimpleEnum::TEST; + $dto->scalarEnum = ScalarEnum::TESTED_ENUM; + + $result = $this->marshal($dto); + $this->assertEquals($dto->simpleEnum, $result['simpleEnum']); + $this->assertEquals($dto->scalarEnum, $result['scalarEnum']); + } +} diff --git a/tests/Unit/DTO/Enum/ScalarEnum.php b/tests/Unit/DTO/Enum/ScalarEnum.php new file mode 100644 index 00000000..60ee9c97 --- /dev/null +++ b/tests/Unit/DTO/Enum/ScalarEnum.php @@ -0,0 +1,17 @@ +