|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace App\Api\Normalizer; |
| 6 | + |
| 7 | +use App\Api\Transformer\ArchiveOutputTransformer; |
| 8 | +use App\Entity\Archive; |
| 9 | +use Symfony\Component\Serializer\SerializerInterface; |
| 10 | +use Symfony\Component\Serializer\SerializerAwareInterface; |
| 11 | +use Symfony\Component\DependencyInjection\Attribute\AsDecorator; |
| 12 | +use Symfony\Component\Serializer\Normalizer\NormalizerInterface; |
| 13 | +use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; |
| 14 | + |
| 15 | +#[AsDecorator('api_platform.jsonld.normalizer.item')] |
| 16 | +final class OutputNormalizer implements NormalizerInterface, DenormalizerInterface, SerializerAwareInterface |
| 17 | +{ |
| 18 | + private $decorated; |
| 19 | + private ArchiveOutputTransformer $archiveOutputTransformer; |
| 20 | + |
| 21 | + public function __construct(NormalizerInterface $decorated, ArchiveOutputTransformer $archiveOutputTransformer) |
| 22 | + { |
| 23 | + if (!$decorated instanceof DenormalizerInterface) { |
| 24 | + throw new \InvalidArgumentException(sprintf('The decorated normalizer must implement the %s.', DenormalizerInterface::class)); |
| 25 | + } |
| 26 | + |
| 27 | + $this->decorated = $decorated; |
| 28 | + $this->archiveOutputTransformer = $archiveOutputTransformer; |
| 29 | + } |
| 30 | + |
| 31 | + public function supportsNormalization($data, $format = null) |
| 32 | + { |
| 33 | + return $this->decorated->supportsNormalization($data, $format); |
| 34 | + } |
| 35 | + |
| 36 | + public function normalize($object, $format = null, array $context = []) |
| 37 | + { |
| 38 | + if ($object instanceof Archive) { |
| 39 | + $outputArchive = $this->archiveOutputTransformer->transform($object); |
| 40 | + |
| 41 | + return $this->decorated->normalize($outputArchive, $format, $context); |
| 42 | + } |
| 43 | + |
| 44 | + return $this->decorated->normalize($object, $format, $context); |
| 45 | + } |
| 46 | + |
| 47 | + public function supportsDenormalization($data, $type, $format = null) |
| 48 | + { |
| 49 | + return $this->decorated->supportsDenormalization($data, $type, $format); |
| 50 | + } |
| 51 | + |
| 52 | + public function denormalize($data, string $type, string $format = null, array $context = []) |
| 53 | + { |
| 54 | + return $this->decorated->denormalize($data, $type, $format, $context); |
| 55 | + } |
| 56 | + |
| 57 | + public function setSerializer(SerializerInterface $serializer) |
| 58 | + { |
| 59 | + if($this->decorated instanceof SerializerAwareInterface) { |
| 60 | + $this->decorated->setSerializer($serializer); |
| 61 | + } |
| 62 | + } |
| 63 | +} |
0 commit comments