mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-21 07:03:49 +00:00
add a discrimnator type on onbjects
This commit is contained in:
@@ -26,6 +26,7 @@ use Chill\PersonBundle\Repository\PersonRepository;
|
||||
use Symfony\Component\Serializer\Exception\RuntimeException;
|
||||
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
|
||||
use Chill\MainBundle\Templating\Entity\ChillEntityRenderExtension;
|
||||
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
|
||||
|
||||
/**
|
||||
* Serialize a Person entity
|
||||
@@ -33,21 +34,15 @@ use Chill\MainBundle\Templating\Entity\ChillEntityRenderExtension;
|
||||
*/
|
||||
class PersonNormalizer implements
|
||||
NormalizerInterface,
|
||||
NormalizerAwareInterface,
|
||||
DenormalizerInterface
|
||||
NormalizerAwareInterface
|
||||
{
|
||||
|
||||
protected NormalizerInterface $normalizer;
|
||||
|
||||
protected PersonRepository $repository;
|
||||
|
||||
private ChillEntityRenderExtension $render;
|
||||
|
||||
public const GET_PERSON = 'get_person';
|
||||
|
||||
public function __construct(PersonRepository $repository, ChillEntityRenderExtension $render)
|
||||
public function __construct(ChillEntityRenderExtension $render)
|
||||
{
|
||||
$this->repository = $repository;
|
||||
$this->render = $render;
|
||||
}
|
||||
|
||||
@@ -56,7 +51,7 @@ class PersonNormalizer implements
|
||||
/** @var Person $person */
|
||||
return [
|
||||
'type' => 'person',
|
||||
'person_id' => $person->getId(),
|
||||
'id' => $person->getId(),
|
||||
'text' => $this->render->renderString($person),
|
||||
'firstName' => $person->getFirstName(),
|
||||
'lastName' => $person->getLastName(),
|
||||
@@ -80,33 +75,11 @@ class PersonNormalizer implements
|
||||
}
|
||||
|
||||
|
||||
public function denormalize($data, string $type, string $format = null, array $context = []): Person
|
||||
{
|
||||
if ($context[self::GET_PERSON] ?? true) {
|
||||
$id = $data['person_id'] ?? null;
|
||||
if (NULL === $id) {
|
||||
throw new RuntimeException("missing person_id into person object");
|
||||
}
|
||||
}
|
||||
/** var Person $person */
|
||||
$person = $this->repository->findOneById($id);
|
||||
|
||||
if (NULL === $person) {
|
||||
throw new UnexpectedValueException("person id not found");
|
||||
}
|
||||
|
||||
return $person;
|
||||
}
|
||||
|
||||
public function supportsNormalization($data, string $format = null): bool
|
||||
{
|
||||
return $data instanceof Person;
|
||||
}
|
||||
|
||||
public function supportsDenormalization($data, string $type, ?string $format = NULL): bool
|
||||
{
|
||||
return Person::class === $type;
|
||||
}
|
||||
|
||||
public function setNormalizer(NormalizerInterface $normalizer)
|
||||
{
|
||||
|
Reference in New Issue
Block a user