chill-bundles/src/Bundle/ChillPersonBundle/Serializer/Normalizer/AccompanyingPeriodResourceNormalizer.php
2022-01-27 17:08:00 +01:00

109 lines
3.4 KiB
PHP

<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Chill\PersonBundle\Serializer\Normalizer;
use Chill\MainBundle\Serializer\Normalizer\DiscriminatedObjectDenormalizer;
use Chill\PersonBundle\Entity\AccompanyingPeriod\Resource;
use Chill\PersonBundle\Entity\Person;
use Chill\PersonBundle\Repository\AccompanyingPeriod\ResourceRepository;
use Chill\ThirdPartyBundle\Entity\ThirdParty;
use Symfony\Component\Serializer\Exception;
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\ObjectToPopulateTrait;
use function array_key_exists;
use function array_merge;
use function count;
class AccompanyingPeriodResourceNormalizer implements DenormalizerAwareInterface, DenormalizerInterface
{
use DenormalizerAwareTrait;
use ObjectToPopulateTrait;
private ResourceRepository $repository;
public function __construct(ResourceRepository $repository)
{
$this->repository = $repository;
}
public function denormalize($data, $type, $format = null, array $context = [])
{
$resource = $this->extractObjectToPopulate($type, $context);
if ('accompanying_period_resource' !== ($data['type'] ?? null)) {
throw new Exception\InvalidArgumentException("the key type must be present in data and set to 'accompanying_period_resource'");
}
if (null === $resource && array_key_exists('id', $data)) {
$resource = $this->repository->find($data['id']);
if (null === $resource) {
throw new Exception\UnexpectedValueException(sprintf('the resource with' .
'id %d is not found', $data['id']));
}
// if resource found, available only for read-only
if (count($data) > 2) {
unset($data['id'], $data['type']);
throw new Exception\ExtraAttributesException($data);
}
}
if (null === $resource) {
$resource = new Resource();
}
if (array_key_exists('resource', $data)) {
$res = $this->denormalizer->denormalize(
$data['resource'],
DiscriminatedObjectDenormalizer::TYPE,
$format,
array_merge(
$context,
[
DiscriminatedObjectDenormalizer::ALLOWED_TYPES => [
Person::class, ThirdParty::class,
],
]
)
);
$resource->setResource($res);
}
if (array_key_exists('comment', $data)) {
$resource->setComment($data['comment']);
}
return $resource;
}
public function normalize($resource, $format = null, array $context = [])
{
return [
'type' => 'accompanying_period_resource',
'id' => $resource->getId(),
'comment' => $resource->getComment(),
];
}
public function supportsDenormalization($data, $type, $format = null)
{
return Resource::class === $type;
}
}