. */ declare(strict_types=1); namespace App\Serializer\APIPlatform; use ApiPlatform\Serializer\ItemNormalizer; use Symfony\Component\DependencyInjection\Attribute\AsDecorator; use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface; use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; use Symfony\Component\Serializer\Normalizer\NormalizerInterface; use Symfony\Component\Serializer\SerializerAwareInterface; use Symfony\Component\Serializer\SerializerInterface; /** * This class decorates API Platform's ItemNormalizer to allow skipping the normalization process by setting the * DISABLE_ITEM_NORMALIZER context key to true. This is useful for all kind of serialization operations, where the API * Platform subsystem should not be used. */ #[AsDecorator("api_platform.serializer.normalizer.item")] class SkippableItemNormalizer implements NormalizerInterface, DenormalizerInterface, SerializerAwareInterface, CacheableSupportsMethodInterface { public const DISABLE_ITEM_NORMALIZER = 'DISABLE_ITEM_NORMALIZER'; public function __construct(private readonly ItemNormalizer $inner) { } public function hasCacheableSupportsMethod(): bool { return $this->inner->hasCacheableSupportsMethod(); } public function denormalize(mixed $data, string $type, ?string $format = null, array $context = []): mixed { return $this->inner->denormalize($data, $type, $format, $context); } public function supportsDenormalization(mixed $data, string $type, ?string $format = null, array $context = []): bool { if ($context[self::DISABLE_ITEM_NORMALIZER] ?? false) { return false; } return $this->inner->supportsDenormalization($data, $type, $format, $context); } public function normalize(mixed $object, ?string $format = null, array $context = []): float|int|bool|\ArrayObject|array|string|null { return $this->inner->normalize($object, $format, $context); } public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool { if ($context[self::DISABLE_ITEM_NORMALIZER] ?? false) { return false; } return $this->inner->supportsNormalization($data, $format, $context); } public function setSerializer(SerializerInterface $serializer): void { $this->inner->setSerializer($serializer); } public function getSupportedTypes(?string $format): array { //Don't cache results, as we check for the context return [ 'object' => false ]; } }