render = $render; $this->repository = $repository; $this->centerResolverManager = $centerResolverManager; } public function denormalize($data, $type, $format = null, array $context = []) { $person = $this->extractObjectToPopulate($type, $context); if (array_key_exists('id', $data) && null === $person) { $person = $this->repository->find($data['id']); if (null === $person) { throw new UnexpectedValueException("The person with id \"{$data['id']}\" does " . 'not exists'); } // currently, not allowed to update a person through api // if instantiated with id return $person; } if (null === $person) { $person = new Person(); } $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]); 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); $person->setBirthdate($object); break; case 'deathdate': $object = $this->denormalizer->denormalize($data[$item], DateTimeImmutable::class, $format, $context); $person->setDeathdate($object); break; case 'center': $object = $this->denormalizer->denormalize($data[$item], Center::class, $format, $context); $person->setCenter($object); break; case 'altNames': foreach ($data[$item] as $altName) { $oldAltName = $person ->getAltNames() ->filter(static fn (PersonAltName $n): bool => $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; } } return $person; } /** * @param Person $person * @param string|null $format */ public function normalize($person, $format = null, array $context = []) { $household = $person->getCurrentHousehold(); return [ 'type' => 'person', 'id' => $person->getId(), 'text' => $this->render->renderString($person), 'firstName' => $person->getFirstName(), 'lastName' => $person->getLastName(), 'birthdate' => $this->normalizer->normalize($person->getBirthdate(), $format, $context), 'deathdate' => $this->normalizer->normalize($person->getDeathdate(), $format, $context), 'centers' => $this->normalizer->normalize($this->centerResolverManager->resolveCenters($person), $format, $context), 'phonenumber' => $person->getPhonenumber(), 'mobilenumber' => $person->getMobilenumber(), 'altNames' => $this->normalizeAltNames($person->getAltNames()), 'gender' => $person->getGender(), 'current_household_address' => $this->normalizer->normalize($person->getCurrentHouseholdAddress(), $format, $context), 'current_household_id' => $household ? $this->normalizer->normalize($household->getId(), $format, $context) : null, ]; } public function supportsDenormalization($data, $type, $format = null) { return Person::class === $type && 'person' === ($data['type'] ?? null); } public function supportsNormalization($data, $format = null): bool { return $data instanceof Person && 'json' === $format; } /** * @param Collection $altNames * * @return array> */ protected function normalizeAltNames(Collection $altNames): array { return $altNames ->map( static function (PersonAltName $personAltName): array { return [ 'key' => $personAltName->getKey(), 'label' => $personAltName->getLabel(), ]; } ) ->toArray(); } }