<?php
namespace App\Normalizer\PublicWebsite;
use App\Entity\Agent;
use App\Entity\Mandate;
use ArrayObject;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Serializer\Exception\ExceptionInterface;
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
class AgentNormalizer implements ContextAwareNormalizerInterface
{
private ObjectNormalizer $normalizer;
private Security $security;
/**
* @param ObjectNormalizer $normalizer
* @param Security $security
*/
public function __construct(
ObjectNormalizer $normalizer,
Security $security
) {
$this->normalizer = $normalizer;
$this->security = $security;
}
/**
* @param Mandate $object
* @param $format
* @param array $context
*
* @return array|ArrayObject|bool|float|int|mixed|string|null
*
* @throws ExceptionInterface
*/
public function normalize($object, $format = null, array $context = [])
{
$data = $this->normalizer->normalize($object, $format, $context);
$data['code'] = $data['gender']['code'];
$data['email'] = $data['professionalEmail'];
$data['level'] = $data['qualificationLevel']['level'];
$data['filePath'] = isset($data['avatar']) ? $data['avatar']['filePath'] : null;
unset($data['professionalEmail']);
unset($data['qualificationLevel']);
unset($data['avatar']);
unset($data['gender']);
return $data;
}
/**
* @param $data
* @param $format
* @param array $context
*
* @return bool
*/
public function supportsNormalization($data, $format = null, array $context = []): bool
{
return $data instanceof Agent && in_array('ROLE_PUBLIC_ACCESS', $this->security->getUser()->getRoles());
}
}