Add getSupportedTypes method for (de)normalizerInterface implementations

This commit is contained in:
2025-05-26 11:21:59 +02:00
parent 17db59d221
commit 6e21f2688f
34 changed files with 277 additions and 2 deletions

View File

@@ -16,7 +16,6 @@ use Chill\MainBundle\Entity\Address;
use Chill\MainBundle\Templating\Entity\AddressRender;
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
@@ -137,4 +136,21 @@ class AddressNormalizer implements \Symfony\Component\Serializer\Normalizer\Norm
return false;
}
public function getSupportedTypes(?string $format): array
{
if ('json' === $format) {
return [
Address::class => true,
];
}
if ('docgen' === $format) {
return [
Address::class => false,
];
}
return [];
}
}

View File

@@ -69,4 +69,15 @@ class CenterNormalizer implements DenormalizerInterface, NormalizerInterface
{
return $data instanceof Center && 'json' === $format;
}
public function getSupportedTypes(?string $format): array
{
if ('json' !== $format && null !== $format) {
return [];
}
return [
Center::class => true,
];
}
}

View File

@@ -45,4 +45,11 @@ class CollectionNormalizer implements NormalizerAwareInterface, NormalizerInterf
{
return $data instanceof Collection;
}
public function getSupportedTypes(?string $format): array
{
return [
Collection::class => true,
];
}
}

View File

@@ -82,4 +82,9 @@ class CommentEmbeddableDocGenNormalizer implements \Symfony\Component\Serializer
return false;
}
public function getSupportedTypes(?string $format): array
{
return 'docgen' === $format ? [CommentEmbeddable::class => true] : [];
}
}

View File

@@ -121,4 +121,25 @@ class DateNormalizer implements \Symfony\Component\Serializer\Normalizer\Normali
return false;
}
public function getSupportedTypes(?string $format): array
{
if ('json' === $format) {
return [
\DateTimeInterface::class => true,
\DateTime::class => true,
\DateTimeImmutable::class => true,
];
}
if ('docgen' === $format) {
return [
\DateTimeInterface::class => true,
\DateTime::class => true,
\DateTimeImmutable::class => true,
];
}
return [];
}
}

View File

@@ -69,4 +69,11 @@ class DiscriminatedObjectDenormalizer implements \Symfony\Component\Serializer\N
return false;
}
public function getSupportedTypes(?string $format): array
{
return [
self::TYPE => true,
];
}
}

View File

@@ -65,4 +65,11 @@ class DoctrineExistingEntityNormalizer implements DenormalizerInterface
// we do not have any class discriminator. Check that the id is the only one key
return 1 === \count($data);
}
public function getSupportedTypes(?string $format): array
{
return [
'object' => false,
];
}
}

View File

@@ -79,4 +79,9 @@ class EntityWorkflowStepNormalizer implements NormalizerAwareInterface, Normaliz
{
return $data instanceof EntityWorkflowStep && 'json' === $format;
}
public function getSupportedTypes(?string $format): array
{
return 'json' === $format ? [EntityWorkflowStep::class => true] : [];
}
}

View File

@@ -37,4 +37,11 @@ class GenderDocGenNormalizer implements \Symfony\Component\Serializer\Normalizer
'type' => 'chill_main_gender',
];
}
public function getSupportedTypes(?string $format): array
{
return [
Gender::class => true,
];
}
}

View File

@@ -55,4 +55,9 @@ class NotificationNormalizer implements NormalizerAwareInterface, NormalizerInte
{
return $data instanceof Notification && 'json' === $format;
}
public function getSupportedTypes(?string $format): array
{
return 'json' === $format ? [Notification::class => true] : [];
}
}

View File

@@ -78,4 +78,15 @@ class PhonenumberNormalizer implements \Symfony\Component\Serializer\Normalizer\
return false;
}
public function getSupportedTypes(?string $format): array
{
if ('json' === $format || 'docgen' === $format) {
return [
PhoneNumber::class => true,
];
}
return [];
}
}

View File

@@ -34,4 +34,11 @@ class PointNormalizer implements DenormalizerInterface
{
return Point::class === $type;
}
public function getSupportedTypes(?string $format): array
{
return [
Point::class => true,
];
}
}

View File

@@ -34,4 +34,15 @@ class UserGroupDenormalizer implements DenormalizerInterface
&& 2 === count(array_keys($data))
;
}
public function getSupportedTypes(?string $format): array
{
if ('json' !== $format) {
return [];
}
return [
UserGroup::class => false,
];
}
}

View File

@@ -122,4 +122,15 @@ class UserNormalizer implements \Symfony\Component\Serializer\Normalizer\Normali
return false;
}
public function getSupportedTypes(?string $format): array
{
if ('json' === $format || 'docgen' === $format) {
return [
User::class => true,
];
}
return [];
}
}

View File

@@ -136,6 +136,11 @@ final class UserNormalizerTest extends TestCase
{
return true;
}
public function getSupportedTypes(?string $format): array
{
return ['*' => true];
}
});
$this->assertEquals($expected, $normalizer->normalize($user, $format, $context));