cs: Update PersonJsonNormalizer.

This commit is contained in:
Pol Dellaiera 2022-01-04 15:17:50 +01:00
parent aa6b770bfe
commit c8c5af8d14
No known key found for this signature in database
GPG Key ID: D476DFE9C67467CA

View File

@ -19,7 +19,7 @@ use Chill\PersonBundle\Entity\PersonAltName;
use Chill\PersonBundle\Repository\PersonRepository; use Chill\PersonBundle\Repository\PersonRepository;
use DateTime; use DateTime;
use DateTimeImmutable; use DateTimeImmutable;
use LogicException; use Doctrine\Common\Collections\Collection;
use Symfony\Component\Serializer\Exception\UnexpectedValueException; use Symfony\Component\Serializer\Exception\UnexpectedValueException;
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface; use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait; use Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait;
@ -79,13 +79,24 @@ class PersonJsonNormalizer implements
$person = new Person(); $person = new Person();
} }
foreach (['firstName', 'lastName', 'phonenumber', 'mobilenumber', 'gender', $fields = [
'birthdate', 'deathdate', 'center', 'altNames', ] 'firstName',
as $item) { 'lastName',
if (!array_key_exists($item, $data)) { 'phonenumber',
continue; '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) { switch ($item) {
case 'firstName': case 'firstName':
$person->setFirstName($data[$item]); $person->setFirstName($data[$item]);
@ -149,9 +160,6 @@ class PersonJsonNormalizer implements
} }
break; break;
default:
throw new LogicException("item not defined: {$item}");
} }
} }
@ -194,14 +202,19 @@ class PersonJsonNormalizer implements
return $data instanceof Person && 'json' === $format; 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 = []; return $altNames
->map(
foreach ($altNames as $n) { static function (PersonAltName $personAltName): array {
$r[] = ['key' => $n->getKey(), 'label' => $n->getLabel()]; return ['key' => $personAltName->getKey(), 'label' => $personAltName->getLabel()];
} }
)
return $r; ->toArray();
} }
} }