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 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();
}
}