person: fix altname denormalisation

This commit is contained in:
nobohan 2021-12-02 12:00:57 +01:00
parent bcdddcde9b
commit 49da5fe060
2 changed files with 11 additions and 4 deletions

View File

@ -12,6 +12,7 @@ declare(strict_types=1);
namespace Chill\PersonBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
/**
* PersonAltName.
@ -34,6 +35,7 @@ class PersonAltName
* @var string
*
* @ORM\Column(name="key", type="string", length=255)
* @Groups({"write"})
*/
private $key;
@ -41,6 +43,7 @@ class PersonAltName
* @var string
*
* @ORM\Column(name="label", type="text")
* @Groups({"write"})
*/
private $label;

View File

@ -138,10 +138,14 @@ class PersonJsonNormalizer implements
case 'altNames':
foreach($data[$item] as $altName){
$object = $this->denormalizer->denormalize($altName, PersonAltName::class, $format, $context);
dump($object); //TODO not working
if ($object instanceof PersonAltName) {
$person->addAltName($object);
$oldAltName = $person->getAltNames()->filter(function (PersonAltName $n) use ($altName) { return $n->getKey() === $altName['key']; })->first();
if (false === $oldAltName) {
$newAltName = new PersonAltName();
$newAltName->setKey($altName['key']);
$newAltName->setLabel($altName['label']);
$person->addAltName($newAltName);
} else {
$oldAltName->setLabel($altName['label']);
}
}
break;