mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-20 22:53:49 +00:00
Merge branch '139_demandeur' of gitlab.com:Chill-Projet/chill-bundles into 139_demandeur
This commit is contained in:
@@ -7,6 +7,7 @@ use Doctrine\Common\Collections\Collection;
|
||||
use Doctrine\Common\Collections\ArrayCollection;
|
||||
use Symfony\Component\Security\Core\User\AdvancedUserInterface;
|
||||
use Symfony\Component\Validator\Context\ExecutionContextInterface;
|
||||
use Symfony\Component\Serializer\Annotation\DiscriminatorMap;
|
||||
|
||||
/**
|
||||
* User
|
||||
@@ -14,6 +15,9 @@ use Symfony\Component\Validator\Context\ExecutionContextInterface;
|
||||
* @ORM\Entity(repositoryClass="Chill\MainBundle\Repository\UserRepository")
|
||||
* @ORM\Table(name="users")
|
||||
* @ORM\Cache(usage="NONSTRICT_READ_WRITE", region="acl_cache_region")
|
||||
* @DiscriminatorMap(typeProperty="type", mapping={
|
||||
* "user"=User::class
|
||||
* })
|
||||
*/
|
||||
class User implements AdvancedUserInterface {
|
||||
|
||||
|
@@ -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()
|
||||
];
|
||||
}
|
||||
|
||||
|
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace Chill\MainBundle\Tests\Serializer\Normalizer;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||||
use Chill\MainBundle\Serializer\Normalizer\DoctrineExistingEntityNormalizer;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
|
||||
use Chill\MainBundle\Entity\User;
|
||||
|
||||
|
||||
class DoctrineExistingEntityNormalizerTest extends KernelTestCase
|
||||
{
|
||||
protected DoctrineExistingEntityNormalizer $normalizer;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
self::bootKernel();
|
||||
$em = self::$container->get(EntityManagerInterface::class);
|
||||
$serializerFactory = self::$container->get(ClassMetadataFactoryInterface::class);
|
||||
|
||||
$this->normalizer = new DoctrineExistingEntityNormalizer($em, $serializerFactory);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider dataProviderUserId
|
||||
*/
|
||||
public function testGetMappedClass($userId)
|
||||
{
|
||||
$data = [ 'type' => 'user', 'id' => $userId];
|
||||
$supports = $this->normalizer->supportsDenormalization($data, User::class);
|
||||
|
||||
$this->assertTrue($supports);
|
||||
}
|
||||
|
||||
public function dataProviderUserId()
|
||||
{
|
||||
self::bootKernel();
|
||||
|
||||
$userIds = self::$container->get(EntityManagerInterface::class)
|
||||
->getRepository(User::class)
|
||||
->createQueryBuilder('u')
|
||||
->select('u.id')
|
||||
->setMaxResults(1)
|
||||
->getQuery()
|
||||
->getResult()
|
||||
;
|
||||
|
||||
yield [ $userIds[0]['id'] ];
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user