mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
185 lines
7.4 KiB
PHP
185 lines
7.4 KiB
PHP
<?php
|
|
|
|
/**
|
|
* 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.
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Chill\PersonBundle\Serializer\Normalizer;
|
|
|
|
use Chill\DocGeneratorBundle\Serializer\Helper\NormalizeNullValueHelper;
|
|
use Chill\MainBundle\Entity\Address;
|
|
use Chill\MainBundle\Entity\Civility;
|
|
use Chill\MainBundle\Templating\TranslatableStringHelper;
|
|
use Chill\PersonBundle\Entity\Household\Household;
|
|
use Chill\PersonBundle\Entity\Person;
|
|
use Chill\PersonBundle\Entity\PersonAltName;
|
|
use Chill\PersonBundle\Repository\PersonResourceRepository;
|
|
use Chill\PersonBundle\Repository\Relationships\RelationshipRepository;
|
|
use Chill\PersonBundle\Templating\Entity\PersonRenderInterface;
|
|
use DateTimeInterface;
|
|
use Doctrine\Common\Collections\ArrayCollection;
|
|
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
|
|
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface;
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
|
|
use Symfony\Contracts\Translation\TranslatorInterface;
|
|
|
|
use function array_map;
|
|
use function implode;
|
|
|
|
class PersonDocGenNormalizer implements
|
|
ContextAwareNormalizerInterface,
|
|
NormalizerAwareInterface
|
|
{
|
|
use NormalizerAwareTrait;
|
|
|
|
private PersonRenderInterface $personRender;
|
|
|
|
private RelationshipRepository $relationshipRepository;
|
|
|
|
private PersonResourceRepository $personResourceRepository;
|
|
|
|
private TranslatableStringHelper $translatableStringHelper;
|
|
|
|
private TranslatorInterface $translator;
|
|
|
|
public function __construct(
|
|
PersonRenderInterface $personRender,
|
|
RelationshipRepository $relationshipRepository,
|
|
PersonResourceRepository $personResourceRepository,
|
|
TranslatorInterface $translator,
|
|
TranslatableStringHelper $translatableStringHelper
|
|
) {
|
|
$this->personRender = $personRender;
|
|
$this->relationshipRepository = $relationshipRepository;
|
|
$this->personResourceRepository = $personResourceRepository;
|
|
$this->translator = $translator;
|
|
$this->translatableStringHelper = $translatableStringHelper;
|
|
}
|
|
|
|
public function normalize($person, $format = null, array $context = [])
|
|
{
|
|
/** @var Person $person */
|
|
$dateContext = $context;
|
|
$dateContext['docgen:expects'] = DateTimeInterface::class;
|
|
$addressContext = array_merge($context, ['docgen:expects' => Address::class]);
|
|
|
|
if (null === $person) {
|
|
return $this->normalizeNullValue($format, $context);
|
|
}
|
|
|
|
if (!$person instanceof Person) {
|
|
throw new UnexpectedValueException();
|
|
}
|
|
|
|
$data = [
|
|
'type' => 'person',
|
|
'id' => $person->getId(),
|
|
'isNull' => false,
|
|
'civility' => $this->normalizer->normalize($person->getCivility(), $format, array_merge($context, ['docgen:expects' => Civility::class])),
|
|
'firstName' => $person->getFirstName(),
|
|
'lastName' => $person->getLastName(),
|
|
'altNames' => implode(
|
|
', ',
|
|
array_map(
|
|
static function (PersonAltName $altName) {
|
|
return $altName->getLabel();
|
|
},
|
|
$person->getAltNames()->toArray()
|
|
)
|
|
),
|
|
'text' => $this->personRender->renderString($person, []),
|
|
'age' => (int) $person->getAge(),
|
|
'birthdate' => $this->normalizer->normalize($person->getBirthdate(), $format, $dateContext),
|
|
'deathdate' => $this->normalizer->normalize($person->getDeathdate(), $format, $dateContext),
|
|
'gender' => $this->translator->trans($person->getGender()),
|
|
'maritalStatus' => null !== ($ms = $person->getMaritalStatus()) ? $this->translatableStringHelper->localize($ms->getName()) : '',
|
|
'maritalStatusDate' => $this->normalizer->normalize($person->getMaritalStatusDate(), $format, $dateContext),
|
|
'email' => $person->getEmail(),
|
|
'firstPhoneNumber' => $this->normalizer->normalize($person->getPhonenumber() ?? $person->getMobilenumber(), $format, $context),
|
|
'fixPhoneNumber' => $this->normalizer->normalize($person->getPhonenumber(), $format, $context),
|
|
'mobilePhoneNumber' => $this->normalizer->normalize($person->getMobilenumber(), $format, $context),
|
|
'nationality' => null !== ($c = $person->getNationality()) ? $this->translatableStringHelper->localize($c->getName()) : '',
|
|
'placeOfBirth' => $person->getPlaceOfBirth(),
|
|
'memo' => $person->getMemo(),
|
|
'numberOfChildren' => (string) $person->getNumberOfChildren(),
|
|
'address' => $this->normalizer->normalize($person->getCurrentPersonAddress(), $format, $addressContext),
|
|
'resources' => $this->personResourceRepository->findBy(['personOwner' => $person]),
|
|
];
|
|
dump($data);
|
|
if ($context['docgen:person:with-household'] ?? false) {
|
|
$data['household'] = $this->normalizer->normalize(
|
|
$person->getCurrentHousehold(),
|
|
$format,
|
|
array_merge($context, [
|
|
'docgen:expects' => Household::class,
|
|
'docgen:person:with-household' => false,
|
|
'docgen:person:with-relations' => false,
|
|
])
|
|
);
|
|
}
|
|
|
|
if ($context['docgen:person:with-relations'] ?? false) {
|
|
$data['relations'] = $this->normalizer->normalize(
|
|
new ArrayCollection($this->relationshipRepository->findByPerson($person)),
|
|
$format,
|
|
array_merge($context, [
|
|
'docgen:person:with-household' => false,
|
|
'docgen:person:with-relation' => false,
|
|
'docgen:relationship:counterpart' => $person,
|
|
])
|
|
);
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
|
|
public function supportsNormalization($data, $format = null, array $context = [])
|
|
{
|
|
if ('docgen' !== $format) {
|
|
return false;
|
|
}
|
|
|
|
return
|
|
$data instanceof Person
|
|
|| (
|
|
null === $data
|
|
&& Person::class === ($context['docgen:expects'] ?? null)
|
|
);
|
|
}
|
|
|
|
private function normalizeNullValue(string $format, array $context)
|
|
{
|
|
$normalizer = new NormalizeNullValueHelper($this->normalizer, 'type', 'person');
|
|
|
|
$attributes = [
|
|
'id', 'firstName', 'lastName', 'age', 'altNames', 'text',
|
|
'civility' => Civility::class,
|
|
'birthdate' => DateTimeInterface::class,
|
|
'deathdate' => DateTimeInterface::class,
|
|
'gender', 'maritalStatus',
|
|
'maritalStatusDate' => DateTimeInterface::class,
|
|
'email', 'firstPhoneNumber', 'fixPhoneNumber', 'mobilePhoneNumber', 'nationality',
|
|
'placeOfBirth', 'memo', 'numberOfChildren',
|
|
'address' => Address::class,
|
|
];
|
|
|
|
if ($context['docgen:person:with-household'] ?? false) {
|
|
$attributes['household'] = Household::class;
|
|
}
|
|
|
|
$data = $normalizer->normalize($attributes, $format, $context);
|
|
|
|
if ($context['docgen:person:with-relations'] ?? false) {
|
|
$data['relations'] = [];
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
}
|