DefaultNormalizer.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Serializer;
  4. use App\Entity\Access\Access;
  5. use App\Service\Utils\EntityUtils;
  6. use ArrayObject;
  7. use ReflectionException;
  8. use Symfony\Component\Serializer\Exception\ExceptionInterface;
  9. use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
  10. use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
  11. use Symfony\Component\Serializer\SerializerAwareInterface;
  12. use Symfony\Component\Serializer\SerializerInterface;
  13. use Symfony\Component\Security\Core\Security;
  14. /**
  15. * Serializer par défaut
  16. */
  17. final class DefaultNormalizer implements NormalizerInterface, DenormalizerInterface, SerializerAwareInterface
  18. {
  19. public function __construct(
  20. private NormalizerInterface $decorated,
  21. private EntityUtils $entityUtils,
  22. private Security $security
  23. )
  24. {
  25. if (!$this->decorated instanceof DenormalizerInterface) {
  26. throw new \InvalidArgumentException(sprintf('The decorated normalizer must implement the %s.', DenormalizerInterface::class));
  27. }
  28. }
  29. /**
  30. * @param $data
  31. * @param $format
  32. * @param mixed[] $context
  33. * @return bool
  34. */
  35. public function supportsNormalization($data, $format = null, array $context = []): bool
  36. {
  37. return $this->decorated->supportsNormalization($data, $format);
  38. }
  39. /**
  40. * @param $object
  41. * @param $format
  42. * @param mixed[] $context
  43. * @return mixed
  44. * @throws ExceptionInterface
  45. */
  46. public function normalize($object, $format = null, array $context = []): mixed
  47. {
  48. return $this->decorated->normalize($object, $format, $context);
  49. }
  50. /**
  51. * @param $data
  52. * @param $type
  53. * @param $format
  54. * @param mixed[] $context
  55. * @return bool
  56. */
  57. public function supportsDenormalization($data, $type, $format = null, array $context = []): bool
  58. {
  59. /** @phpstan-ignore-next-line */
  60. return $this->decorated->supportsDenormalization($data, $type, $format);
  61. }
  62. /**
  63. * @param $data
  64. * @param $class
  65. * @param $format
  66. * @param mixed[] $context
  67. * @return mixed
  68. * @throws ReflectionException
  69. */
  70. public function denormalize($data, $class, $format = null, array $context = []): mixed
  71. {
  72. /** @phpstan-ignore-next-line */
  73. $entity = $this->decorated->denormalize($data, $class, $format, $context);
  74. /** @var Access $access */
  75. $access = $this->security->getUser();
  76. $this->entityUtils->defaultValueSettersByAccess($entity, $access);
  77. return $entity;
  78. }
  79. public function setSerializer(SerializerInterface $serializer): void
  80. {
  81. if($this->decorated instanceof SerializerAwareInterface) {
  82. $this->decorated->setSerializer($serializer);
  83. }
  84. }
  85. }