mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
cs: Update PersonJsonNormalizer
.
This commit is contained in:
parent
aa6b770bfe
commit
c8c5af8d14
@ -19,7 +19,7 @@ use Chill\PersonBundle\Entity\PersonAltName;
|
||||
use Chill\PersonBundle\Repository\PersonRepository;
|
||||
use DateTime;
|
||||
use DateTimeImmutable;
|
||||
use LogicException;
|
||||
use Doctrine\Common\Collections\Collection;
|
||||
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
|
||||
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
|
||||
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait;
|
||||
@ -79,13 +79,24 @@ class PersonJsonNormalizer implements
|
||||
$person = new Person();
|
||||
}
|
||||
|
||||
foreach (['firstName', 'lastName', 'phonenumber', 'mobilenumber', 'gender',
|
||||
'birthdate', 'deathdate', 'center', 'altNames', ]
|
||||
as $item) {
|
||||
if (!array_key_exists($item, $data)) {
|
||||
continue;
|
||||
}
|
||||
$fields = [
|
||||
'firstName',
|
||||
'lastName',
|
||||
'phonenumber',
|
||||
'mobilenumber',
|
||||
'gender',
|
||||
'birthdate',
|
||||
'deathdate',
|
||||
'center',
|
||||
'altNames',
|
||||
];
|
||||
|
||||
$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]);
|
||||
@ -149,9 +160,6 @@ class PersonJsonNormalizer implements
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new LogicException("item not defined: {$item}");
|
||||
}
|
||||
}
|
||||
|
||||
@ -194,14 +202,19 @@ class PersonJsonNormalizer implements
|
||||
return $data instanceof Person && 'json' === $format;
|
||||
}
|
||||
|
||||
protected function normalizeAltNames($altNames): array
|
||||
/**
|
||||
* @param Collection<array-key, PersonAltName> $altNames
|
||||
*
|
||||
* @return array<array-key, array<string, string>>
|
||||
*/
|
||||
protected function normalizeAltNames(Collection $altNames): array
|
||||
{
|
||||
$r = [];
|
||||
|
||||
foreach ($altNames as $n) {
|
||||
$r[] = ['key' => $n->getKey(), 'label' => $n->getLabel()];
|
||||
}
|
||||
|
||||
return $r;
|
||||
return $altNames
|
||||
->map(
|
||||
static function (PersonAltName $personAltName): array {
|
||||
return ['key' => $personAltName->getKey(), 'label' => $personAltName->getLabel()];
|
||||
}
|
||||
)
|
||||
->toArray();
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user