src/Hitso/Bundle/CommonBundle/Serializer/EntityNormalizer.php line 56

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4.  * @author Piotr Kowalski <piotr.kowalski@autentika.pl>
  5.  * date: 2018-02-15 14:52
  6.  */
  7. namespace Hitso\Bundle\CommonBundle\Serializer;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Hitso\Bundle\CommonBundle\Entity\EntityInterface;
  10. use Hitso\Bundle\CommonBundle\Serializer\Extractor\EntityExtractor;
  11. use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
  12. use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
  13. use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
  14. use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
  15. use Webmozart\Assert\Assert;
  16. /**
  17.  * Class ContentNormalizer.
  18.  */
  19. class EntityNormalizer extends ObjectNormalizer
  20. {
  21.     /**
  22.      * @var EntityManagerInterface
  23.      */
  24.     protected $entityManager;
  25.     /**
  26.      * ContentNormalizer constructor.
  27.      *
  28.      * @param ClassMetadataFactoryInterface|null $classMetadataFactory
  29.      * @param NameConverterInterface|null        $nameConverter
  30.      * @param PropertyAccessorInterface|null     $propertyAccessor
  31.      * @param EntityExtractor|null               $propTypeExtractor
  32.      */
  33.     public function __construct(
  34.         ClassMetadataFactoryInterface $classMetadataFactory null,
  35.         NameConverterInterface $nameConverter null,
  36.         PropertyAccessorInterface $propertyAccessor null,
  37.         EntityExtractor $propTypeExtractor null,
  38.         EntityManagerInterface $entityManager null
  39.     ) {
  40.         parent::__construct($classMetadataFactory$nameConverter$propertyAccessor$propTypeExtractor);
  41.         Assert::notNull($entityManager);
  42.         $this->entityManager $entityManager;
  43.         $this
  44.             ->setCircularReferenceLimit(1)
  45.             ->setCircularReferenceHandler(
  46.                 function (EntityInterface $entity) {
  47.                     return $entity->getId();
  48.                 }
  49.             );
  50.     }
  51.     /**
  52.      * {@inheritdoc}
  53.      */
  54.     public function supportsDenormalization($data$type$format null)
  55.     {
  56.         return is_a($typeEntityInterface::class, true);
  57.     }
  58.     /**
  59.      * {@inheritdoc}
  60.      */
  61.     public function supportsNormalization($data$format null)
  62.     {
  63.         return $data instanceof EntityInterface;
  64.     }
  65.     /**
  66.      * {@inheritdoc}
  67.      */
  68.     public function denormalize($data$class$format null, array $context = [])
  69.     {
  70.         if (!empty($data['attributes'])) {
  71.             $data $data['attributes'];
  72.         }
  73.         $metadata          $this->entityManager->getClassMetadata($class);
  74.         $idFieldName       $metadata->getSingleIdentifierFieldName();
  75.         $allowedAttributes $this->getAllowedAttributes($class$contexttrue);
  76.         if (in_array($idFieldName$allowedAttributes) && !empty($data[$idFieldName])) {
  77.             $idValue $data[$idFieldName];
  78.             $entity $this->entityManager
  79.                 ->getRepository($class)
  80.                 ->findOneBy([$idFieldName => $idValue]);
  81.             if ($entity) {
  82.                 return $entity;
  83.             }
  84.             unset($data[$idFieldName]);
  85.         }
  86.         return parent::denormalize($data$class$format$context);
  87.     }
  88.     public function normalize($object$format null, array $context = [])
  89.     {
  90.         /* @var Element $object */
  91.         return [
  92.             'id'         => $object->getId(),
  93.             'attributes' => parent::normalize($object$format$context),
  94.         ];
  95.     }
  96. }