src/Normalizer/AgentsOptionsNormalizer.php line 60

Open in your IDE?
  1. <?php
  2. namespace App\Normalizer;
  3. use App\Entity\AbstractCollaborator;
  4. use App\Entity\AgentsOptions;
  5. use Symfony\Contracts\Translation\TranslatorInterface;
  6. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  7. use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
  8. use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
  9. use Symfony\Component\Serializer\Exception\ExceptionInterface;
  10. use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface;
  11. use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
  12. use Symfony\Component\Security\Core\Security;
  13. use Symfony\Component\HttpFoundation\Request;
  14. class AgentsOptionsNormalizer implements NormalizerInterfaceCacheableSupportsMethodInterface
  15. {
  16.     /**
  17.      * @var ObjectNormalizer
  18.      */
  19.     private ObjectNormalizer $normalizer;
  20.     /**
  21.      * @var TranslatorInterface
  22.      */
  23.     private TranslatorInterface $translator;
  24.     /**
  25.      * @var Security
  26.      */
  27.     private Security $security;
  28.     /**
  29.      * @var SessionInterface
  30.      */
  31.     private SessionInterface $session;
  32.     /**
  33.      * @param ObjectNormalizer $normalizer
  34.      * @param Security $security
  35.      */
  36.     public function __construct(
  37.         ObjectNormalizer $normalizer,
  38.         Security $security,
  39.         TranslatorInterface $translator,
  40.         SessionInterface $session
  41.     ) {
  42.         $this->normalizer $normalizer;
  43.         $this->security $security;
  44.         $this->translator $translator;
  45.         $this->session $session;
  46.     }
  47.     public function normalize($objectstring $format null, array $context = []): array
  48.     {
  49.         $data $this->normalizer->normalize($object$format$context);
  50.         $request Request::createFromGlobals();
  51.         $locale = isset($request->getLanguages()[0]) ? $request->getLanguages()[0] : $this->session->get('_locale');
  52.         foreach ($data as $item) {
  53.             $data['name'] = $this->translator->trans($data['option']['name']?? null, [], 'option'$locale);
  54.             $data['code'] = $data['option']['code'] ?? null;
  55.             $data['type'] = $data['option']['type'] ?? null;
  56.             $data['value'] = filter_var($data['optionValue'], FILTER_VALIDATE_BOOLEAN) ?? null;
  57.         }
  58.         $data array_filter($data, function ($value) {
  59.             return $value !== null;
  60.         });
  61.         unset($data['option']);
  62.         unset($data['agent']);
  63.         unset($data['optionValue']);
  64.         return $data;
  65.     }
  66.     public function supportsNormalization($datastring $format null, array $context = []): bool
  67.     {
  68.         return $data instanceof AgentsOptions;
  69.     }
  70.     public function hasCacheableSupportsMethod(): bool
  71.     {
  72.         return true;
  73.     }
  74. }