From 7703f36f63ca4fad53db6428a58d5322c7ad3ef1 Mon Sep 17 00:00:00 2001 From: brainshaker Date: Wed, 3 Jan 2024 21:54:07 +0100 Subject: [PATCH] chore(deprecations): implement 'getSupportedTypes' in EnumNormalizer and prepare types for symfony 6.3 --- src/Serializer/Normalizer/EnumNormalizer.php | 34 ++++++++++++++------ 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/src/Serializer/Normalizer/EnumNormalizer.php b/src/Serializer/Normalizer/EnumNormalizer.php index 32b9f2c..87708f6 100644 --- a/src/Serializer/Normalizer/EnumNormalizer.php +++ b/src/Serializer/Normalizer/EnumNormalizer.php @@ -4,6 +4,7 @@ namespace Brainshaker95\PhpToTsBundle\Serializer\Normalizer; +use ArrayObject; use BackedEnum; use Brainshaker95\PhpToTsBundle\Attribute\AsTypeScriptable; use Brainshaker95\PhpToTsBundle\Tool\Attribute; @@ -17,33 +18,48 @@ final class EnumNormalizer implements NormalizerInterface { /** * @param mixed[] $context + * + * @return array|string|int|float|bool|ArrayObject|null */ public function normalize( - mixed $enum, + mixed $data, ?string $format = null, array $context = [], - ): int|string { - if (!is_object($enum)) { + ): array|string|int|float|bool|ArrayObject|null { + if (!is_object($data)) { throw new InvalidArgumentException(sprintf( - 'Expected paramteter 1 ($enum) to be of type "object" but got "%s".', - get_debug_type($enum), + 'Expected paramteter 1 ($data) to be of type "object" but got "%s".', + get_debug_type($data), )); } - if (!$enum instanceof BackedEnum) { + if (!$data instanceof BackedEnum) { throw new InvalidArgumentException(sprintf( 'Expected object to be an instance of "%s". Given instance was of class "%s".', BackedEnum::class, - $enum::class, + $data::class, )); } - return $enum->value; + return $data->value; } - public function supportsNormalization($data, ?string $format = null): bool + /** + * @param mixed[] $context + */ + public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool { return $data instanceof BackedEnum && Attribute::existsOnClass(AsTypeScriptable::class, $data); } + + /** + * @return array + */ + public function getSupportedTypes(?string $format): array + { + return [ + BackedEnum::class => true, + ]; + } }