Files
chill-bundles/src/Bundle/ChillPersonBundle/Serializer/Normalizer/PersonJsonNormalizer.php

116 lines
5.0 KiB
PHP

<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\PersonBundle\Serializer\Normalizer;
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\ResidentialAddressRepository;
use Doctrine\Common\Collections\Collection;
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
/**
* Serialize a Person entity.
*/
class PersonJsonNormalizer implements NormalizerAwareInterface, NormalizerInterface
{
use NormalizerAwareTrait;
public function __construct(
private readonly ChillEntityRenderExtension $render,
private readonly CenterResolverManagerInterface $centerResolverManager,
private readonly ResidentialAddressRepository $residentialAddressRepository,
private readonly PhoneNumberHelperInterface $phoneNumberHelper,
private readonly \Chill\PersonBundle\PersonIdentifier\Rendering\PersonIdRenderingInterface $personIdRendering,
) {}
public function normalize($data, $format = null, array $context = []): array
{
if (!$data instanceof Person) {
throw new UnexpectedValueException(sprintf('Expected %s, got %s', Person::class, is_object($data) ? get_class($data) : gettype($data)));
}
$groups = $context[AbstractNormalizer::GROUPS] ?? [];
if (\is_string($groups)) {
$groups = [$groups];
}
$household = $data->getCurrentHousehold();
$currentResidentialAddresses = $this->residentialAddressRepository->findCurrentResidentialAddressByPerson($data);
$normalizedData = [
'type' => 'person',
'id' => $data->getId(),
'text' => $this->render->renderString($data, ['addAge' => false]),
'textAge' => $this->render->renderString($data, ['addAge' => true]),
'firstName' => $data->getFirstName(),
'lastName' => $data->getLastName(),
'current_household_address' => $this->normalizer->normalize($data->getCurrentHouseholdAddress(), $format, $context),
'birthdate' => $this->normalizer->normalize($data->getBirthdate(), $format, $context),
'deathdate' => $this->normalizer->normalize($data->getDeathdate(), $format, $context),
'age' => $this->normalizer->normalize($data->getAge(), $format, $context),
'phonenumber' => $this->normalizer->normalize($data->getPhonenumber(), $format, $context),
'mobilenumber' => $this->normalizer->normalize($data->getMobilenumber(), $format, $context),
'email' => $data->getEmail(),
'gender' => $this->normalizer->normalize($data->getGender(), $format, $context),
'civility' => $this->normalizer->normalize($data->getCivility(), $format, $context),
'personId' => $this->personIdRendering->renderPersonId($data),
'identifiers' => $this->normalizer->normalize($data->getIdentifiers(), $format, $context),
];
if (\in_array('minimal', $groups, true) && 1 === \count($groups)) {
return $normalizedData;
}
return [
...$normalizedData,
'centers' => $this->normalizer->normalize($this->centerResolverManager->resolveCenters($data), $format, $context),
'altNames' => $this->normalizeAltNames($data->getAltNames()),
'current_household_id' => null !== $household ? $this->normalizer->normalize($household->getId(), $format, $context) : null,
'current_residential_addresses' => $this->normalizer->normalize($currentResidentialAddresses, $format, $context),
];
}
public function supportsNormalization($data, $format = null, array $context = []): bool
{
return $data instanceof Person && 'json' === $format;
}
/**
* @param Collection<array-key, PersonAltName> $altNames
*
* @return array<array-key, array<string, string>>
*/
private function normalizeAltNames(Collection $altNames): array
{
return $altNames
->map(
static fn (PersonAltName $personAltName): array => [
'key' => $personAltName->getKey(),
'label' => $personAltName->getLabel(),
]
)
->toArray();
}
public function getSupportedTypes(?string $format): array
{
return 'json' === $format ? [Person::class => false] : [];
}
}