| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- <?php
- declare(strict_types=1);
- namespace App\Serializer;
- use App\Entity\Access\Access;
- use App\Service\Utils\EntityUtils;
- use ArrayObject;
- use ReflectionException;
- use Symfony\Bundle\SecurityBundle\Security;
- use Symfony\Component\Serializer\Exception\ExceptionInterface;
- use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
- use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
- use Symfony\Component\Serializer\SerializerAwareInterface;
- use Symfony\Component\Serializer\SerializerInterface;
- /**
- * Serializer par défaut.
- */
- final class DefaultNormalizer implements NormalizerInterface, DenormalizerInterface, SerializerAwareInterface
- {
- public function __construct(
- private NormalizerInterface $decorated,
- private EntityUtils $entityUtils,
- private Security $security
- ) {
- if (!$this->decorated instanceof DenormalizerInterface) {
- throw new \InvalidArgumentException(sprintf('The decorated normalizer must implement the %s.', DenormalizerInterface::class));
- }
- }
- /**
- * @param mixed[] $context
- */
- public function supportsNormalization($data, $format = null, array $context = []): bool
- {
- return $this->decorated->supportsNormalization($data, $format);
- }
- /**
- * @param mixed[] $context
- *
- * @throws ExceptionInterface
- */
- public function normalize($object, $format = null, array $context = []): mixed
- {
- return $this->decorated->normalize($object, $format, $context);
- }
- /**
- * @param mixed[] $context
- */
- public function supportsDenormalization($data, $type, $format = null, array $context = []): bool
- {
- /* @phpstan-ignore-next-line */
- return $this->decorated->supportsDenormalization($data, $type, $format);
- }
- /**
- * @param mixed[] $context
- *
- * @throws \ReflectionException
- */
- public function denormalize($data, $class, $format = null, array $context = []): mixed
- {
- /** @phpstan-ignore-next-line */
- $entity = $this->decorated->denormalize($data, $class, $format, $context);
- /** @var Access $access */
- $access = $this->security->getUser();
- $this->entityUtils->defaultValueSettersByAccess($entity, $access);
- return $entity;
- }
- public function setSerializer(SerializerInterface $serializer): void
- {
- if ($this->decorated instanceof SerializerAwareInterface) {
- $this->decorated->setSerializer($serializer);
- }
- }
- }
|