add a discrimnator type on onbjects

This commit is contained in:
2021-05-12 17:51:37 +02:00
parent f7a807473d
commit 91e4d585ff
17 changed files with 236 additions and 109 deletions

View File

@@ -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)
{