src/Normalizer/PublicWebsite/AgentNormalizer.php line 39

Open in your IDE?
  1. <?php
  2. namespace App\Normalizer\PublicWebsite;
  3. use App\Entity\Agent;
  4. use App\Entity\Mandate;
  5. use ArrayObject;
  6. use Symfony\Component\Security\Core\Security;
  7. use Symfony\Component\Serializer\Exception\ExceptionInterface;
  8. use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface;
  9. use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
  10. class AgentNormalizer implements ContextAwareNormalizerInterface
  11. {
  12.     private ObjectNormalizer $normalizer;
  13.     private Security $security;
  14.     /**
  15.      * @param ObjectNormalizer $normalizer
  16.      * @param Security $security
  17.      */
  18.     public function __construct(
  19.         ObjectNormalizer $normalizer,
  20.         Security $security
  21.     ) {
  22.         $this->normalizer $normalizer;
  23.         $this->security $security;
  24.     }
  25.     /**
  26.      * @param Mandate $object
  27.      * @param $format
  28.      * @param array $context
  29.      *
  30.      * @return array|ArrayObject|bool|float|int|mixed|string|null
  31.      *
  32.      * @throws ExceptionInterface
  33.      */
  34.     public function normalize($object$format null, array $context = [])
  35.     {
  36.         $data $this->normalizer->normalize($object$format$context);
  37.         $data['code'] = $data['gender']['code'];
  38.         $data['email'] = $data['professionalEmail'];
  39.         $data['level'] = $data['qualificationLevel']['level'];
  40.         $data['filePath'] = isset($data['avatar']) ? $data['avatar']['filePath'] : null;
  41.         unset($data['professionalEmail']);
  42.         unset($data['qualificationLevel']);
  43.         unset($data['avatar']);
  44.         unset($data['gender']);
  45.         return $data;
  46.     }
  47.     /**
  48.      * @param $data
  49.      * @param $format
  50.      * @param array $context
  51.      *
  52.      * @return bool
  53.      */
  54.     public function supportsNormalization($data$format null, array $context = []): bool
  55.     {
  56.         return $data instanceof Agent && in_array('ROLE_PUBLIC_ACCESS'$this->security->getUser()->getRoles());
  57.     }
  58. }