fix backend error of data transformer

This commit is contained in:
Julie Lenaerts 2022-01-25 14:43:51 +01:00
parent cf041cf49e
commit 2ccc6f1976
2 changed files with 27 additions and 2 deletions

View File

@ -12,11 +12,16 @@ declare(strict_types=1);
namespace Chill\MainBundle\Form\Type\DataTransformer; namespace Chill\MainBundle\Form\Type\DataTransformer;
use Chill\MainBundle\Entity\User; use Chill\MainBundle\Entity\User;
use Chill\PersonBundle\Entity\Person;
use Chill\ThirdPartyBundle\Entity\ThirdParty;
use Symfony\Component\Form\DataTransformerInterface; use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\TransformationFailedException; use Symfony\Component\Form\Exception\TransformationFailedException;
use Symfony\Component\Serializer\Exception\UnexpectedValueException as ExceptionUnexpectedValueException;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer; use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\SerializerInterface; use Symfony\Component\Serializer\SerializerInterface;
use UnexpectedValueException;
use function array_key_exists; use function array_key_exists;
class EntityToJsonTransformer implements DataTransformerInterface class EntityToJsonTransformer implements DataTransformerInterface
@ -41,6 +46,8 @@ class EntityToJsonTransformer implements DataTransformerInterface
{ {
$denormalized = json_decode($value, true); $denormalized = json_decode($value, true);
dump($value);
if ($this->multiple) { if ($this->multiple) {
if (null === $denormalized) { if (null === $denormalized) {
return []; return [];
@ -52,6 +59,10 @@ class EntityToJsonTransformer implements DataTransformerInterface
); );
} }
if ($value === '') {
return null;
}
return $this->denormalizeOne($denormalized); return $this->denormalizeOne($denormalized);
} }
@ -69,7 +80,7 @@ class EntityToJsonTransformer implements DataTransformerInterface
]); ]);
} }
private function denormalizeOne(array $item): User private function denormalizeOne(array $item)
{ {
if (!array_key_exists('type', $item)) { if (!array_key_exists('type', $item)) {
throw new TransformationFailedException('the key "type" is missing on element'); throw new TransformationFailedException('the key "type" is missing on element');
@ -79,10 +90,24 @@ class EntityToJsonTransformer implements DataTransformerInterface
throw new TransformationFailedException('the key "id" is missing on element'); throw new TransformationFailedException('the key "id" is missing on element');
} }
switch ($this->type) {
case 'user':
$class = User::class;
break;
case 'person':
$class = Person::class;
break;
case 'thirdparty':
$class = ThirdParty::class;
break;
default:
throw new \UnexpectedValueException('This type is not supported');
}
return return
$this->denormalizer->denormalize( $this->denormalizer->denormalize(
['type' => $item['type'], 'id' => $item['id']], ['type' => $item['type'], 'id' => $item['id']],
$this->type, $class,
'json', 'json',
[AbstractNormalizer::GROUPS => ['read']], [AbstractNormalizer::GROUPS => ['read']],
); );