mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-20 14:43:49 +00:00
Bootstrap encoder for documents
This commit is contained in:
@@ -0,0 +1,116 @@
|
||||
<?php
|
||||
|
||||
namespace Chill\PersonBundle\Serializer\Normalizer;
|
||||
|
||||
use Chill\DocGeneratorBundle\Serializer\Helper\NormalizeNullValueHelper;
|
||||
use Chill\MainBundle\Templating\TranslatableStringHelper;
|
||||
use Chill\PersonBundle\Entity\Person;
|
||||
use Chill\PersonBundle\Entity\PersonAltName;
|
||||
use Chill\PersonBundle\Templating\Entity\PersonRender;
|
||||
use Symfony\Component\Serializer\Exception\CircularReferenceException;
|
||||
use Symfony\Component\Serializer\Exception\ExceptionInterface;
|
||||
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
|
||||
use Symfony\Component\Serializer\Exception\LogicException;
|
||||
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface;
|
||||
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
|
||||
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
|
||||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||
|
||||
class PersonDocGenNormalizer implements
|
||||
ContextAwareNormalizerInterface,
|
||||
NormalizerAwareInterface
|
||||
{
|
||||
|
||||
use NormalizerAwareTrait;
|
||||
|
||||
private PersonRender $personRender;
|
||||
private TranslatorInterface $translator;
|
||||
private TranslatableStringHelper $translatableStringHelper;
|
||||
|
||||
/**
|
||||
* @param PersonRender $personRender
|
||||
* @param TranslatorInterface $translator
|
||||
* @param TranslatableStringHelper $translatableStringHelper
|
||||
*/
|
||||
public function __construct(
|
||||
PersonRender $personRender,
|
||||
TranslatorInterface $translator,
|
||||
TranslatableStringHelper $translatableStringHelper
|
||||
) {
|
||||
$this->personRender = $personRender;
|
||||
$this->translator = $translator;
|
||||
$this->translatableStringHelper = $translatableStringHelper;
|
||||
}
|
||||
|
||||
public function normalize($person, string $format = null, array $context = [])
|
||||
{
|
||||
/** @var Person $person */
|
||||
|
||||
$dateContext = $context;
|
||||
$dateContext['docgen:expects'] = \DateTimeInterface::class;
|
||||
|
||||
if (null === $person) {
|
||||
return $this->normalizeNullValue($format, $context);
|
||||
}
|
||||
|
||||
return [
|
||||
'firstname' => $person->getFirstName(),
|
||||
'lastname' => $person->getLastName(),
|
||||
'altNames' => \implode(
|
||||
', ',
|
||||
\array_map(
|
||||
function (PersonAltName $altName) {
|
||||
return $altName->getLabel();
|
||||
},
|
||||
$person->getAltNames()->toArray()
|
||||
)
|
||||
),
|
||||
'text' => $this->personRender->renderString($person, []),
|
||||
'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' => $person->getPhonenumber() ?? $person->getMobilenumber(),
|
||||
'fixPhoneNumber' => $person->getPhonenumber(),
|
||||
'mobilePhoneNumber' => $person->getMobilenumber(),
|
||||
'nationality' => null !== ($c = $person->getNationality()) ? $this->translatableStringHelper->localize($c->getName()) : '',
|
||||
'placeOfBirth' => $person->getPlaceOfBirth(),
|
||||
'memo' => $person->getMemo(),
|
||||
'numberOfChildren' => (string) $person->getNumberOfChildren(),
|
||||
];
|
||||
}
|
||||
|
||||
private function normalizeNullValue(string $format, array $context)
|
||||
{
|
||||
$normalizer = new NormalizeNullValueHelper($this->normalizer);
|
||||
|
||||
$attributes = [
|
||||
'firstname', 'lastname', 'altNames', 'text',
|
||||
'birthdate' => \DateTimeInterface::class,
|
||||
'deathdate' => \DateTimeInterface::class,
|
||||
'gender', 'maritalStatus',
|
||||
'maritalStatusDate' => \DateTimeInterface::class,
|
||||
'email', 'firstPhoneNumber', 'fixPhoneNumber', 'mobilePhoneNumber', 'nationality',
|
||||
'placeOfBirth', 'memo', 'numberOfChildren'
|
||||
];
|
||||
|
||||
return $normalizer->normalize($attributes, $format, $context);
|
||||
}
|
||||
|
||||
public function supportsNormalization($data, string $format = null, array $context = [])
|
||||
{
|
||||
if ($format !== 'docgen') {
|
||||
return false;
|
||||
}
|
||||
|
||||
return
|
||||
$data instanceof Person
|
||||
|| (
|
||||
\array_key_exists('docgen:expects', $context)
|
||||
&& $context['docgen:expects'] === Person::class
|
||||
);
|
||||
}
|
||||
}
|
@@ -39,7 +39,7 @@ use Symfony\Component\Serializer\Normalizer\ObjectToPopulateTrait;
|
||||
* Serialize a Person entity
|
||||
*
|
||||
*/
|
||||
class PersonNormalizer implements
|
||||
class PersonJsonNormalizer implements
|
||||
NormalizerInterface,
|
||||
NormalizerAwareInterface,
|
||||
DenormalizerInterface,
|
||||
@@ -105,7 +105,7 @@ class PersonNormalizer implements
|
||||
|
||||
public function supportsNormalization($data, string $format = null): bool
|
||||
{
|
||||
return $data instanceof Person;
|
||||
return $data instanceof Person && $format === 'json';
|
||||
}
|
||||
|
||||
public function denormalize($data, string $type, string $format = null, array $context = [])
|
||||
@@ -128,45 +128,48 @@ class PersonNormalizer implements
|
||||
$person = new Person();
|
||||
}
|
||||
|
||||
$properties = ['firstName', 'lastName', 'phonenumber', 'mobilenumber', 'gender'];
|
||||
foreach (['firstName', 'lastName', 'phonenumber', 'mobilenumber', 'gender',
|
||||
'birthdate', 'deathdate', 'center']
|
||||
as $item) {
|
||||
|
||||
$properties = array_filter(
|
||||
$properties,
|
||||
static fn (string $property): bool => array_key_exists($property, $data)
|
||||
);
|
||||
|
||||
foreach ($properties as $item) {
|
||||
$callable = [$person, sprintf('set%s', ucfirst($item))];
|
||||
|
||||
if (is_callable($callable)) {
|
||||
$closure = \Closure::fromCallable($callable);
|
||||
|
||||
$closure($data[$item]);
|
||||
if (!\array_key_exists($item, $data)) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
$propertyToClassMapping = [
|
||||
'birthdate' => \DateTime::class,
|
||||
'deathdate' => \DateTime::class,
|
||||
'center' => Center::class,
|
||||
];
|
||||
|
||||
$propertyToClassMapping = array_filter(
|
||||
$propertyToClassMapping,
|
||||
static fn (string $item): bool => array_key_exists($item, $data)
|
||||
);
|
||||
|
||||
foreach ($propertyToClassMapping as $item => $class) {
|
||||
$object = $this->denormalizer->denormalize($data[$item], $class, $format, $context);
|
||||
|
||||
if ($object instanceof $class) {
|
||||
$callable = [$object, sprintf('set%s', ucfirst($item))];
|
||||
|
||||
if (is_callable($callable)) {
|
||||
$closure = \Closure::fromCallable($callable);
|
||||
|
||||
$closure($object);
|
||||
}
|
||||
switch ($item) {
|
||||
case 'firstName':
|
||||
$person->setFirstName($data[$item]);
|
||||
break;
|
||||
case 'lastName':
|
||||
$person->setLastName($data[$item]);
|
||||
break;
|
||||
case 'phonenumber':
|
||||
$person->setPhonenumber($data[$item]);
|
||||
break;
|
||||
case 'mobilenumber':
|
||||
$person->setMobilenumber($data[$item]);
|
||||
break;
|
||||
case 'gender':
|
||||
$person->setGender($data[$item]);
|
||||
break;
|
||||
case 'birthdate':
|
||||
$object = $this->denormalizer->denormalize($data[$item], \DateTime::class, $format, $context);
|
||||
if ($object instanceof \DateTime) {
|
||||
$person->setBirthdate($object);
|
||||
}
|
||||
break;
|
||||
case 'deathdate':
|
||||
$object = $this->denormalizer->denormalize($data[$item], \DateTime::class, $format, $context);
|
||||
if ($object instanceof \DateTime) {
|
||||
$person->setDeathdate($object);
|
||||
}
|
||||
break;
|
||||
case 'center':
|
||||
$object = $this->denormalizer->denormalize($data[$item], Center::class, $format, $context);
|
||||
$person->setCenter($object);
|
||||
break;
|
||||
default:
|
||||
throw new \LogicException("item not defined: $item");
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user