vendor/symfony/doctrine-bridge/PropertyInfo/DoctrineExtractor.php line 197

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Bridge\Doctrine\PropertyInfo;
  11. use Doctrine\DBAL\Types\Type as DBALType;
  12. use Doctrine\DBAL\Types\Types;
  13. use Doctrine\ORM\EntityManagerInterface;
  14. use Doctrine\ORM\Mapping\ClassMetadata;
  15. use Doctrine\ORM\Mapping\ClassMetadataInfo;
  16. use Doctrine\ORM\Mapping\MappingException as OrmMappingException;
  17. use Doctrine\Persistence\Mapping\ClassMetadataFactory;
  18. use Doctrine\Persistence\Mapping\MappingException;
  19. use Symfony\Component\PropertyInfo\PropertyAccessExtractorInterface;
  20. use Symfony\Component\PropertyInfo\PropertyListExtractorInterface;
  21. use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface;
  22. use Symfony\Component\PropertyInfo\Type;
  23. /**
  24.  * Extracts data using Doctrine ORM and ODM metadata.
  25.  *
  26.  * @author Kévin Dunglas <dunglas@gmail.com>
  27.  */
  28. class DoctrineExtractor implements PropertyListExtractorInterfacePropertyTypeExtractorInterfacePropertyAccessExtractorInterface
  29. {
  30.     private $entityManager;
  31.     private $classMetadataFactory;
  32.     private static $useDeprecatedConstants;
  33.     /**
  34.      * @param EntityManagerInterface $entityManager
  35.      */
  36.     public function __construct($entityManager)
  37.     {
  38.         if ($entityManager instanceof EntityManagerInterface) {
  39.             $this->entityManager $entityManager;
  40.         } elseif ($entityManager instanceof ClassMetadataFactory) {
  41.             @trigger_error(sprintf('Injecting an instance of "%s" in "%s" is deprecated since Symfony 4.2, inject an instance of "%s" instead.'ClassMetadataFactory::class, __CLASS__EntityManagerInterface::class), \E_USER_DEPRECATED);
  42.             $this->classMetadataFactory $entityManager;
  43.         } else {
  44.             throw new \TypeError(sprintf('$entityManager must be an instance of "%s", "%s" given.'EntityManagerInterface::class, \is_object($entityManager) ? \get_class($entityManager) : \gettype($entityManager)));
  45.         }
  46.         if (null === self::$useDeprecatedConstants) {
  47.             self::$useDeprecatedConstants = !class_exists(Types::class);
  48.         }
  49.     }
  50.     /**
  51.      * {@inheritdoc}
  52.      */
  53.     public function getProperties($class, array $context = [])
  54.     {
  55.         if (null === $metadata $this->getMetadata($class)) {
  56.             return null;
  57.         }
  58.         $properties array_merge($metadata->getFieldNames(), $metadata->getAssociationNames());
  59.         if ($metadata instanceof ClassMetadataInfo && class_exists('Doctrine\ORM\Mapping\Embedded') && $metadata->embeddedClasses) {
  60.             $properties array_filter($properties, function ($property) {
  61.                 return false === strpos($property'.');
  62.             });
  63.             $properties array_merge($propertiesarray_keys($metadata->embeddedClasses));
  64.         }
  65.         return $properties;
  66.     }
  67.     /**
  68.      * {@inheritdoc}
  69.      */
  70.     public function getTypes($class$property, array $context = [])
  71.     {
  72.         if (null === $metadata $this->getMetadata($class)) {
  73.             return null;
  74.         }
  75.         if ($metadata->hasAssociation($property)) {
  76.             $class $metadata->getAssociationTargetClass($property);
  77.             if ($metadata->isSingleValuedAssociation($property)) {
  78.                 if ($metadata instanceof ClassMetadataInfo) {
  79.                     $associationMapping $metadata->getAssociationMapping($property);
  80.                     $nullable $this->isAssociationNullable($associationMapping);
  81.                 } else {
  82.                     $nullable false;
  83.                 }
  84.                 return [new Type(Type::BUILTIN_TYPE_OBJECT$nullable$class)];
  85.             }
  86.             $collectionKeyType Type::BUILTIN_TYPE_INT;
  87.             if ($metadata instanceof ClassMetadataInfo) {
  88.                 $associationMapping $metadata->getAssociationMapping($property);
  89.                 if (isset($associationMapping['indexBy'])) {
  90.                     $indexColumn $associationMapping['indexBy'];
  91.                     /** @var ClassMetadataInfo $subMetadata */
  92.                     $subMetadata $this->entityManager $this->entityManager->getClassMetadata($associationMapping['targetEntity']) : $this->classMetadataFactory->getMetadataFor($associationMapping['targetEntity']);
  93.                     $typeOfField $subMetadata->getTypeOfField($subMetadata->getFieldForColumn($indexColumn));
  94.                     if (!$collectionKeyType $this->getPhpType($typeOfField)) {
  95.                         return null;
  96.                     }
  97.                 }
  98.             }
  99.             return [new Type(
  100.                 Type::BUILTIN_TYPE_OBJECT,
  101.                 false,
  102.                 'Doctrine\Common\Collections\Collection',
  103.                 true,
  104.                 new Type($collectionKeyType),
  105.                 new Type(Type::BUILTIN_TYPE_OBJECTfalse$class)
  106.             )];
  107.         }
  108.         if ($metadata instanceof ClassMetadataInfo && class_exists('Doctrine\ORM\Mapping\Embedded') && isset($metadata->embeddedClasses[$property])) {
  109.             return [new Type(Type::BUILTIN_TYPE_OBJECTfalse$metadata->embeddedClasses[$property]['class'])];
  110.         }
  111.         if ($metadata->hasField($property)) {
  112.             $typeOfField $metadata->getTypeOfField($property);
  113.             if (!$builtinType $this->getPhpType($typeOfField)) {
  114.                 return null;
  115.             }
  116.             $nullable $metadata instanceof ClassMetadataInfo && $metadata->isNullable($property);
  117.             switch ($builtinType) {
  118.                 case Type::BUILTIN_TYPE_OBJECT:
  119.                     switch ($typeOfField) {
  120.                         case self::$useDeprecatedConstants DBALType::DATE Types::DATE_MUTABLE:
  121.                         // no break
  122.                         case self::$useDeprecatedConstants DBALType::DATETIME Types::DATETIME_MUTABLE:
  123.                         // no break
  124.                         case self::$useDeprecatedConstants DBALType::DATETIMETZ Types::DATETIMETZ_MUTABLE:
  125.                         // no break
  126.                         case 'vardatetime':
  127.                         case self::$useDeprecatedConstants DBALType::TIME Types::TIME_MUTABLE:
  128.                             return [new Type(Type::BUILTIN_TYPE_OBJECT$nullable'DateTime')];
  129.                         case 'date_immutable':
  130.                         case 'datetime_immutable':
  131.                         case 'datetimetz_immutable':
  132.                         case 'time_immutable':
  133.                             return [new Type(Type::BUILTIN_TYPE_OBJECT$nullable'DateTimeImmutable')];
  134.                         case 'dateinterval':
  135.                             return [new Type(Type::BUILTIN_TYPE_OBJECT$nullable'DateInterval')];
  136.                     }
  137.                     break;
  138.                 case Type::BUILTIN_TYPE_ARRAY:
  139.                     switch ($typeOfField) {
  140.                         case self::$useDeprecatedConstants DBALType::TARRAY Types::ARRAY:
  141.                         // no break
  142.                         case 'json_array':
  143.                             return [new Type(Type::BUILTIN_TYPE_ARRAY$nullablenulltrue)];
  144.                         case self::$useDeprecatedConstants DBALType::SIMPLE_ARRAY Types::SIMPLE_ARRAY:
  145.                             return [new Type(Type::BUILTIN_TYPE_ARRAY$nullablenulltrue, new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_STRING))];
  146.                     }
  147.             }
  148.             return [new Type($builtinType$nullable)];
  149.         }
  150.         return null;
  151.     }
  152.     /**
  153.      * {@inheritdoc}
  154.      */
  155.     public function isReadable($class$property, array $context = [])
  156.     {
  157.         return null;
  158.     }
  159.     /**
  160.      * {@inheritdoc}
  161.      */
  162.     public function isWritable($class$property, array $context = [])
  163.     {
  164.         if (
  165.             null === ($metadata $this->getMetadata($class))
  166.             || ClassMetadata::GENERATOR_TYPE_NONE === $metadata->generatorType
  167.             || !\in_array($property$metadata->getIdentifierFieldNames(), true)
  168.         ) {
  169.             return null;
  170.         }
  171.         return false;
  172.     }
  173.     private function getMetadata(string $class): ?ClassMetadata
  174.     {
  175.         try {
  176.             return $this->entityManager $this->entityManager->getClassMetadata($class) : $this->classMetadataFactory->getMetadataFor($class);
  177.         } catch (MappingException OrmMappingException $exception) {
  178.             return null;
  179.         }
  180.     }
  181.     /**
  182.      * Determines whether an association is nullable.
  183.      *
  184.      * @see https://github.com/doctrine/doctrine2/blob/v2.5.4/lib/Doctrine/ORM/Tools/EntityGenerator.php#L1221-L1246
  185.      */
  186.     private function isAssociationNullable(array $associationMapping): bool
  187.     {
  188.         if (isset($associationMapping['id']) && $associationMapping['id']) {
  189.             return false;
  190.         }
  191.         if (!isset($associationMapping['joinColumns'])) {
  192.             return true;
  193.         }
  194.         $joinColumns $associationMapping['joinColumns'];
  195.         foreach ($joinColumns as $joinColumn) {
  196.             if (isset($joinColumn['nullable']) && !$joinColumn['nullable']) {
  197.                 return false;
  198.             }
  199.         }
  200.         return true;
  201.     }
  202.     /**
  203.      * Gets the corresponding built-in PHP type.
  204.      */
  205.     private function getPhpType(string $doctrineType): ?string
  206.     {
  207.         switch ($doctrineType) {
  208.             case self::$useDeprecatedConstants DBALType::SMALLINT Types::SMALLINT:
  209.             // no break
  210.             case self::$useDeprecatedConstants DBALType::INTEGER Types::INTEGER:
  211.                 return Type::BUILTIN_TYPE_INT;
  212.             case self::$useDeprecatedConstants DBALType::FLOAT Types::FLOAT:
  213.                 return Type::BUILTIN_TYPE_FLOAT;
  214.             case self::$useDeprecatedConstants DBALType::BIGINT Types::BIGINT:
  215.             // no break
  216.             case self::$useDeprecatedConstants DBALType::STRING Types::STRING:
  217.             // no break
  218.             case self::$useDeprecatedConstants DBALType::TEXT Types::TEXT:
  219.             // no break
  220.             case self::$useDeprecatedConstants DBALType::GUID Types::GUID:
  221.             // no break
  222.             case self::$useDeprecatedConstants DBALType::DECIMAL Types::DECIMAL:
  223.                 return Type::BUILTIN_TYPE_STRING;
  224.             case self::$useDeprecatedConstants DBALType::BOOLEAN Types::BOOLEAN:
  225.                 return Type::BUILTIN_TYPE_BOOL;
  226.             case self::$useDeprecatedConstants DBALType::BLOB Types::BLOB:
  227.             // no break
  228.             case 'binary':
  229.                 return Type::BUILTIN_TYPE_RESOURCE;
  230.             case self::$useDeprecatedConstants DBALType::OBJECT Types::OBJECT:
  231.             // no break
  232.             case self::$useDeprecatedConstants DBALType::DATE Types::DATE_MUTABLE:
  233.             // no break
  234.             case self::$useDeprecatedConstants DBALType::DATETIME Types::DATETIME_MUTABLE:
  235.             // no break
  236.             case self::$useDeprecatedConstants DBALType::DATETIMETZ Types::DATETIMETZ_MUTABLE:
  237.             // no break
  238.             case 'vardatetime':
  239.             case self::$useDeprecatedConstants DBALType::TIME Types::TIME_MUTABLE:
  240.             // no break
  241.             case 'date_immutable':
  242.             case 'datetime_immutable':
  243.             case 'datetimetz_immutable':
  244.             case 'time_immutable':
  245.             case 'dateinterval':
  246.                 return Type::BUILTIN_TYPE_OBJECT;
  247.             case self::$useDeprecatedConstants DBALType::TARRAY Types::ARRAY:
  248.             // no break
  249.             case self::$useDeprecatedConstants DBALType::SIMPLE_ARRAY Types::SIMPLE_ARRAY:
  250.             // no break
  251.             case 'json_array':
  252.                 return Type::BUILTIN_TYPE_ARRAY;
  253.         }
  254.         return null;
  255.     }
  256. }