add route for creating a person, and post api

This commit is contained in:
2021-05-21 18:05:03 +02:00
parent 857298b8b8
commit ebe3bc5f7b
8 changed files with 246 additions and 19 deletions

View File

@@ -18,7 +18,11 @@
*/
namespace Chill\PersonBundle\Serializer\Normalizer;
use Chill\MainBundle\Entity\Center;
use Chill\PersonBundle\Entity\Person;
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
@@ -27,6 +31,7 @@ use Symfony\Component\Serializer\Exception\RuntimeException;
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
use Chill\MainBundle\Templating\Entity\ChillEntityRenderExtension;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
use Symfony\Component\Serializer\Normalizer\ObjectToPopulateTrait;
/**
* Serialize a Person entity
@@ -34,13 +39,18 @@ use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
*/
class PersonNormalizer implements
NormalizerInterface,
NormalizerAwareInterface
NormalizerAwareInterface,
DenormalizerInterface,
DenormalizerAwareInterface
{
protected NormalizerInterface $normalizer;
private ChillEntityRenderExtension $render;
use NormalizerAwareTrait;
use ObjectToPopulateTrait;
use DenormalizerAwareTrait;
public function __construct(ChillEntityRenderExtension $render)
{
$this->render = $render;
@@ -59,7 +69,9 @@ class PersonNormalizer implements
'center' => $this->normalizer->normalize($person->getCenter()),
'phonenumber' => $person->getPhonenumber(),
'mobilenumber' => $person->getMobilenumber(),
'altNames' => $this->normalizeAltNames($person->getAltNames())
'altNames' => $this->normalizeAltNames($person->getAltNames()),
'gender' => $person->getGender(),
'gender_numeric' => $person->getGenderNumeric(),
];
}
@@ -80,9 +92,39 @@ class PersonNormalizer implements
return $data instanceof Person;
}
public function setNormalizer(NormalizerInterface $normalizer)
public function denormalize($data, string $type, string $format = null, array $context = [])
{
$this->normalizer = $normalizer;
$person = $this->extractObjectToPopulate($type, $context);
if (null === $person) {
$person = new Person();
}
foreach (['firstName', 'lastName', 'phonenumber', 'mobilenumber', 'gender']
as $item) {
if (\array_key_exists($item, $data)) {
$person->{'set'.\ucfirst($item)}($data[$item]);
}
}
foreach ([
'birthdate' => \DateTime::class,
'center' => Center::class
] as $item => $class) {
if (\array_key_exists($item, $data)) {
$object = $this->denormalizer->denormalize($data[$item], $class, $format, $context);
dump($item, $data, $object, $class);
if ($object instanceof $class) {
$person->{'set'.\ucfirst($item)}($object);
}
}
}
return $person;
}
public function supportsDenormalization($data, string $type, string $format = null)
{
return $type === Person::class && ($data['type'] ?? NULL) === 'person';
}
}