mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-21 15:13:50 +00:00
add a discrimnator type on onbjects
This commit is contained in:
@@ -6,15 +6,21 @@ use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
|
||||
use Doctrine\ORM\Mapping\ClassMetadata;
|
||||
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
|
||||
use Symfony\Component\Serializer\Mapping\ClassMetadataInterface as SerializerMetadata;
|
||||
|
||||
|
||||
class DoctrineExistingEntityNormalizer implements DenormalizerInterface
|
||||
{
|
||||
private EntityManagerInterface $em;
|
||||
|
||||
public function __construct(EntityManagerInterface $em)
|
||||
private ClassMetadataFactoryInterface $serializerMetadataFactory;
|
||||
|
||||
|
||||
public function __construct(EntityManagerInterface $em, ClassMetadataFactoryInterface $serializerMetadataFactory)
|
||||
{
|
||||
$this->em = $em;
|
||||
$this->serializerMetadataFactory = $serializerMetadataFactory;
|
||||
}
|
||||
|
||||
public function denormalize($data, string $type, string $format = null, array $context = [])
|
||||
@@ -29,10 +35,37 @@ class DoctrineExistingEntityNormalizer implements DenormalizerInterface
|
||||
|
||||
public function supportsDenormalization($data, string $type, string $format = null)
|
||||
{
|
||||
return \is_array($data)
|
||||
&& count($data) === 1
|
||||
&& \array_key_exists('id', $data)
|
||||
&& $this->em->getClassMetadata($type) instanceof ClassMetadata;
|
||||
if (FALSE === \is_array($data)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (FALSE === \array_key_exists('id', $data)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (FALSE === $this->em->getClassMetadata($type) instanceof ClassMetadata) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// does have serializer metadata, and class discriminator ?
|
||||
if ($this->serializerMetadataFactory->hasMetadataFor($type)) {
|
||||
|
||||
$classDiscriminator = $this->serializerMetadataFactory
|
||||
->getMetadataFor($type)->getClassDiscriminatorMapping();
|
||||
|
||||
if ($classDiscriminator) {
|
||||
$typeProperty = $classDiscriminator->getTypeProperty();
|
||||
|
||||
// check that only 2 keys
|
||||
// that the second key is property
|
||||
// and that the type match the class for given type property
|
||||
return count($data) === 2
|
||||
&& \array_key_exists($typeProperty, $data)
|
||||
&& $type === $classDiscriminator->getClassForType($data[$typeProperty]);
|
||||
}
|
||||
}
|
||||
|
||||
// we do not have any class discriminator. Check that the id is the only one key
|
||||
return count($data) === 1;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@@ -24,7 +24,7 @@ use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @internal we keep this normalizer, because the property 'text' may be replace by a rendering in the future
|
||||
*/
|
||||
class UserNormalizer implements NormalizerInterface
|
||||
{
|
||||
@@ -32,8 +32,10 @@ class UserNormalizer implements NormalizerInterface
|
||||
{
|
||||
/** @var User $user */
|
||||
return [
|
||||
'type' => 'user',
|
||||
'id' => $user->getId(),
|
||||
'username' => $user->getUsername()
|
||||
'username' => $user->getUsername(),
|
||||
'text' => $user->getUsername()
|
||||
];
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user