mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2026-04-09 14:33:44 +00:00
Merge remote-tracking branch 'origin/ticket-app-master' into migrate_to_sf72
# Conflicts: # .gitlab-ci.yml # composer.json # config/services.yaml # phpunit.xml.dist # src/Bundle/ChillAsideActivityBundle/src/Entity/AsideActivity.php # src/Bundle/ChillCalendarBundle/Entity/CancelReason.php # src/Bundle/ChillCalendarBundle/Messenger/Handler/CalendarRemoveHandler.php # src/Bundle/ChillCalendarBundle/RemoteCalendar/DependencyInjection/RemoteCalendarCompilerPass.php # src/Bundle/ChillDocGeneratorBundle/Service/Messenger/OnGenerationFails.php # src/Bundle/ChillJobBundle/src/Entity/Immersion.php # src/Bundle/ChillMainBundle/CRUD/Controller/CRUDController.php # src/Bundle/ChillMainBundle/DataFixtures/ORM/LoadLocationType.php # src/Bundle/ChillMainBundle/Entity/Location.php # src/Bundle/ChillMainBundle/Routing/MenuComposer.php # src/Bundle/ChillMainBundle/Routing/MenuTwig.php # src/Bundle/ChillMainBundle/Security/PasswordRecover/RecoverPasswordHelper.php # src/Bundle/ChillMainBundle/Serializer/Normalizer/DateNormalizer.php # src/Bundle/ChillMainBundle/Tests/Form/Type/ScopePickerTypeTest.php # src/Bundle/ChillMainBundle/Tests/Services/MenuComposerTest.php # src/Bundle/ChillPersonBundle/Controller/PersonController.php # src/Bundle/ChillPersonBundle/Entity/Person.php # src/Bundle/ChillPersonBundle/Form/DataMapper/PersonAltNameDataMapper.php # src/Bundle/ChillPersonBundle/Repository/PersonACLAwareRepository.php # src/Bundle/ChillPersonBundle/Serializer/Normalizer/PersonJsonNormalizer.php # src/Bundle/ChillPersonBundle/Tests/Serializer/Normalizer/PersonJsonNormalizerTest.php # src/Bundle/ChillTaskBundle/Form/SingleTaskType.php
This commit is contained in:
@@ -11,174 +11,38 @@ declare(strict_types=1);
|
||||
|
||||
namespace Chill\PersonBundle\Serializer\Normalizer;
|
||||
|
||||
use Chill\MainBundle\Entity\Center;
|
||||
use Chill\MainBundle\Entity\Civility;
|
||||
use Chill\MainBundle\Entity\Gender;
|
||||
use Chill\MainBundle\Phonenumber\PhoneNumberHelperInterface;
|
||||
use Chill\MainBundle\Security\Resolver\CenterResolverManagerInterface;
|
||||
use Chill\MainBundle\Templating\Entity\ChillEntityRenderExtension;
|
||||
use Chill\PersonBundle\Entity\Person;
|
||||
use Chill\PersonBundle\Entity\PersonAltName;
|
||||
use Chill\PersonBundle\Repository\PersonRepository;
|
||||
use Chill\PersonBundle\Repository\ResidentialAddressRepository;
|
||||
use Doctrine\Common\Collections\Collection;
|
||||
use libphonenumber\PhoneNumber;
|
||||
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
|
||||
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
|
||||
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
|
||||
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait;
|
||||
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
|
||||
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
|
||||
use Symfony\Component\Serializer\Normalizer\ObjectToPopulateTrait;
|
||||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
||||
|
||||
/**
|
||||
* Serialize a Person entity.
|
||||
*/
|
||||
class PersonJsonNormalizer implements DenormalizerAwareInterface, NormalizerAwareInterface, PersonJsonNormalizerInterface
|
||||
class PersonJsonNormalizer implements NormalizerAwareInterface, NormalizerInterface
|
||||
{
|
||||
use DenormalizerAwareTrait;
|
||||
|
||||
use NormalizerAwareTrait;
|
||||
|
||||
use ObjectToPopulateTrait;
|
||||
|
||||
public function __construct(
|
||||
private readonly ChillEntityRenderExtension $render,
|
||||
/* TODO: replace by PersonRenderInterface, as sthis is the only one required */
|
||||
private readonly PersonRepository $repository,
|
||||
private readonly CenterResolverManagerInterface $centerResolverManager,
|
||||
private readonly ResidentialAddressRepository $residentialAddressRepository,
|
||||
private readonly PhoneNumberHelperInterface $phoneNumberHelper,
|
||||
private readonly \Chill\PersonBundle\PersonIdentifier\Rendering\PersonIdRenderingInterface $personIdRendering,
|
||||
) {}
|
||||
|
||||
public function denormalize($data, $type, $format = null, array $context = []): mixed
|
||||
{
|
||||
$person = $this->extractObjectToPopulate($type, $context);
|
||||
|
||||
if (\array_key_exists('id', $data) && null === $person) {
|
||||
$person = $this->repository->find($data['id']);
|
||||
|
||||
if (null === $person) {
|
||||
throw new UnexpectedValueException("The person with id \"{$data['id']}\" does ".'not exists');
|
||||
}
|
||||
|
||||
// currently, not allowed to update a person through api
|
||||
// if instantiated with id
|
||||
return $person;
|
||||
}
|
||||
|
||||
if (null === $person) {
|
||||
$person = new Person();
|
||||
}
|
||||
|
||||
$fields = [
|
||||
'firstName',
|
||||
'lastName',
|
||||
'phonenumber',
|
||||
'mobilenumber',
|
||||
'gender',
|
||||
'birthdate',
|
||||
'deathdate',
|
||||
'center',
|
||||
'altNames',
|
||||
'email',
|
||||
'civility',
|
||||
];
|
||||
|
||||
$fields = array_filter(
|
||||
$fields,
|
||||
static fn (string $field): bool => \array_key_exists($field, $data)
|
||||
);
|
||||
|
||||
foreach ($fields as $item) {
|
||||
switch ($item) {
|
||||
case 'firstName':
|
||||
$person->setFirstName($data[$item]);
|
||||
|
||||
break;
|
||||
|
||||
case 'lastName':
|
||||
$person->setLastName($data[$item]);
|
||||
|
||||
break;
|
||||
|
||||
case 'phonenumber':
|
||||
$person->setPhonenumber($this->denormalizer->denormalize($data[$item], PhoneNumber::class, $format, $context));
|
||||
|
||||
break;
|
||||
|
||||
case 'mobilenumber':
|
||||
$person->setMobilenumber($this->denormalizer->denormalize($data[$item], PhoneNumber::class, $format, $context));
|
||||
|
||||
break;
|
||||
|
||||
case 'gender':
|
||||
$gender = $this->denormalizer->denormalize($data[$item], Gender::class, $format, []);
|
||||
|
||||
$person->setGender($gender);
|
||||
|
||||
break;
|
||||
|
||||
case 'birthdate':
|
||||
$object = $this->denormalizer->denormalize($data[$item], \DateTime::class, $format, $context);
|
||||
|
||||
$person->setBirthdate($object);
|
||||
|
||||
break;
|
||||
|
||||
case 'deathdate':
|
||||
$object = $this->denormalizer->denormalize($data[$item], \DateTimeImmutable::class, $format, $context);
|
||||
|
||||
$person->setDeathdate($object);
|
||||
|
||||
break;
|
||||
|
||||
case 'center':
|
||||
$object = $this->denormalizer->denormalize($data[$item], Center::class, $format, $context);
|
||||
$person->setCenter($object);
|
||||
|
||||
break;
|
||||
|
||||
case 'altNames':
|
||||
foreach ($data[$item] as $altName) {
|
||||
$oldAltName = $person
|
||||
->getAltNames()
|
||||
->filter(static fn (PersonAltName $n): bool => $n->getKey() === $altName['key'])->first();
|
||||
|
||||
if (false === $oldAltName) {
|
||||
$newAltName = new PersonAltName();
|
||||
$newAltName->setKey($altName['key']);
|
||||
$newAltName->setLabel($altName['label']);
|
||||
$person->addAltName($newAltName);
|
||||
} else {
|
||||
$oldAltName->setLabel($altName['label']);
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case 'email':
|
||||
$person->setEmail($data[$item]);
|
||||
|
||||
break;
|
||||
|
||||
case 'civility':
|
||||
$civility = $this->denormalizer->denormalize($data[$item], Civility::class, $format, []);
|
||||
|
||||
$person->setCivility($civility);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return $person;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Person $person
|
||||
* @param string|null $format
|
||||
*/
|
||||
public function normalize($person, $format = null, array $context = []): string|int|float|bool|\ArrayObject|array|null
|
||||
public function normalize($person, $format = null, array $context = [])
|
||||
{
|
||||
$groups = $context[AbstractNormalizer::GROUPS] ?? [];
|
||||
|
||||
@@ -204,35 +68,20 @@ class PersonJsonNormalizer implements DenormalizerAwareInterface, NormalizerAwar
|
||||
'email' => $person->getEmail(),
|
||||
'gender' => $this->normalizer->normalize($person->getGender(), $format, $context),
|
||||
'civility' => $this->normalizer->normalize($person->getCivility(), $format, $context),
|
||||
'personId' => $this->personIdRendering->renderPersonId($person),
|
||||
'identifiers' => $this->normalizer->normalize($person->getIdentifiers(), $format, $context),
|
||||
];
|
||||
|
||||
if (\in_array('minimal', $groups, true) && 1 === \count($groups)) {
|
||||
return $data;
|
||||
}
|
||||
|
||||
return [
|
||||
...$data,
|
||||
'centers' => $this->normalizer->normalize(
|
||||
$this->centerResolverManager->resolveCenters($person),
|
||||
$format,
|
||||
$context
|
||||
),
|
||||
'altNames' => $this->normalizeAltNames($person->getAltNames()),
|
||||
'current_household_id' => null !== $household ?
|
||||
$this->normalizer->normalize($household->getId(), $format, $context) :
|
||||
null,
|
||||
'current_residential_addresses' => [] !== $currentResidentialAddresses ?
|
||||
$this->normalizer->normalize($currentResidentialAddresses, $format, $context) :
|
||||
null,
|
||||
];
|
||||
return [...$data, 'centers' => $this->normalizer->normalize($this->centerResolverManager->resolveCenters($person), $format, $context), 'altNames' => $this->normalizeAltNames($person->getAltNames()), 'current_household_id' => $household ? $this->normalizer->normalize($household->getId(), $format, $context) : null, 'current_residential_addresses' => $currentResidentialAddresses ?
|
||||
$this->normalizer->normalize($currentResidentialAddresses, $format, $context) :
|
||||
null];
|
||||
}
|
||||
|
||||
public function supportsDenormalization($data, $type, $format = null, array $context = []): bool
|
||||
{
|
||||
return Person::class === $type && 'person' === ($data['type'] ?? null);
|
||||
}
|
||||
|
||||
public function supportsNormalization($data, $format = null, array $context = []): bool
|
||||
public function supportsNormalization($data, $format = null): bool
|
||||
{
|
||||
return $data instanceof Person && 'json' === $format;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user