Skip to content

Commit

Permalink
chore(deprecations): implement 'getSupportedTypes' in EnumNormalizer …
Browse files Browse the repository at this point in the history
…and prepare types for symfony 6.3
  • Loading branch information
Brainshaker95 committed Jan 3, 2024
1 parent 53a026f commit 7703f36
Showing 1 changed file with 25 additions and 9 deletions.
34 changes: 25 additions & 9 deletions src/Serializer/Normalizer/EnumNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Brainshaker95\PhpToTsBundle\Serializer\Normalizer;

use ArrayObject;
use BackedEnum;
use Brainshaker95\PhpToTsBundle\Attribute\AsTypeScriptable;
use Brainshaker95\PhpToTsBundle\Tool\Attribute;
Expand All @@ -17,33 +18,48 @@ final class EnumNormalizer implements NormalizerInterface
{
/**
* @param mixed[] $context
*
* @return array<mixed>|string|int|float|bool|ArrayObject<int|string,mixed>|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<class-string|'*'|'object'|string,bool|null>
*/
public function getSupportedTypes(?string $format): array
{
return [
BackedEnum::class => true,
];
}
}

0 comments on commit 7703f36

Please sign in to comment.