src/Hitso/Bundle/CommonBundle/Serializer/Extractor/EntityExtractor.php line 45

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4.  * @author Piotr Kowalski <piotr.kowalski@autentika.pl>
  5.  * date: 2018-03-16 10:29
  6.  */
  7. namespace Hitso\Bundle\CommonBundle\Serializer\Extractor;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Symfony\Bridge\Doctrine\PropertyInfo\DoctrineExtractor;
  10. use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
  11. use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface;
  12. /**
  13.  * Class EntityExtractor.
  14.  */
  15. class EntityExtractor implements PropertyTypeExtractorInterface
  16. {
  17.     /**
  18.      * @var EntityManagerInterface
  19.      */
  20.     protected $entityManager;
  21.     /**
  22.      * EntityExtractor constructor.
  23.      *
  24.      * @param EntityManagerInterface $entityManager
  25.      */
  26.     public function __construct(EntityManagerInterface $entityManager)
  27.     {
  28.         $this->entityManager $entityManager;
  29.     }
  30.     /**
  31.      * Try extract types from Doctrine associations first,
  32.      * when fail then reflection extractor enter the game.
  33.      *
  34.      * {@inheritdoc}
  35.      */
  36.     public function getTypes($class$property, array $context = []): ?array
  37.     {
  38.         $doctrineExtractor   = new DoctrineExtractor($this->entityManager->getMetadataFactory());
  39.         $reflectionExtractor = new ReflectionExtractor();
  40.         $types $doctrineExtractor->getTypes($class$property$context);
  41.         if (isset($types[0]) && null !== $types[0]->getClassName()) {
  42.             return $types;
  43.         }
  44.         return $reflectionExtractor->getTypes($class$property$context);
  45.     }
  46. }