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

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\Common\Collections\Collection;
  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\Embedded;
  17. use Doctrine\ORM\Mapping\MappingException as OrmMappingException;
  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.     public function __construct(EntityManagerInterface $entityManager)
  32.     {
  33.         $this->entityManager $entityManager;
  34.     }
  35.     /**
  36.      * {@inheritdoc}
  37.      */
  38.     public function getProperties(string $class, array $context = [])
  39.     {
  40.         if (null === $metadata $this->getMetadata($class)) {
  41.             return null;
  42.         }
  43.         $properties array_merge($metadata->getFieldNames(), $metadata->getAssociationNames());
  44.         if ($metadata instanceof ClassMetadataInfo && class_exists(Embedded::class) && $metadata->embeddedClasses) {
  45.             $properties array_filter($properties, function ($property) {
  46.                 return !str_contains($property'.');
  47.             });
  48.             $properties array_merge($propertiesarray_keys($metadata->embeddedClasses));
  49.         }
  50.         return $properties;
  51.     }
  52.     /**
  53.      * {@inheritdoc}
  54.      */
  55.     public function getTypes(string $classstring $property, array $context = [])
  56.     {
  57.         if (null === $metadata $this->getMetadata($class)) {
  58.             return null;
  59.         }
  60.         if ($metadata->hasAssociation($property)) {
  61.             $class $metadata->getAssociationTargetClass($property);
  62.             if ($metadata->isSingleValuedAssociation($property)) {
  63.                 if ($metadata instanceof ClassMetadataInfo) {
  64.                     $associationMapping $metadata->getAssociationMapping($property);
  65.                     $nullable $this->isAssociationNullable($associationMapping);
  66.                 } else {
  67.                     $nullable false;
  68.                 }
  69.                 return [new Type(Type::BUILTIN_TYPE_OBJECT$nullable$class)];
  70.             }
  71.             $collectionKeyType Type::BUILTIN_TYPE_INT;
  72.             if ($metadata instanceof ClassMetadataInfo) {
  73.                 $associationMapping $metadata->getAssociationMapping($property);
  74.                 if (isset($associationMapping['indexBy'])) {
  75.                     /** @var ClassMetadataInfo $subMetadata */
  76.                     $subMetadata $this->entityManager->getClassMetadata($associationMapping['targetEntity']);
  77.                     // Check if indexBy value is a property
  78.                     $fieldName $associationMapping['indexBy'];
  79.                     if (null === ($typeOfField $subMetadata->getTypeOfField($fieldName))) {
  80.                         $fieldName $subMetadata->getFieldForColumn($associationMapping['indexBy']);
  81.                         //Not a property, maybe a column name?
  82.                         if (null === ($typeOfField $subMetadata->getTypeOfField($fieldName))) {
  83.                             //Maybe the column name is the association join column?
  84.                             $associationMapping $subMetadata->getAssociationMapping($fieldName);
  85.                             /** @var ClassMetadataInfo $subMetadata */
  86.                             $indexProperty $subMetadata->getSingleAssociationReferencedJoinColumnName($fieldName);
  87.                             $subMetadata $this->entityManager->getClassMetadata($associationMapping['targetEntity']);
  88.                             //Not a property, maybe a column name?
  89.                             if (null === ($typeOfField $subMetadata->getTypeOfField($indexProperty))) {
  90.                                 $fieldName $subMetadata->getFieldForColumn($indexProperty);
  91.                                 $typeOfField $subMetadata->getTypeOfField($fieldName);
  92.                             }
  93.                         }
  94.                     }
  95.                     if (!$collectionKeyType $this->getPhpType($typeOfField)) {
  96.                         return null;
  97.                     }
  98.                 }
  99.             }
  100.             return [new Type(
  101.                 Type::BUILTIN_TYPE_OBJECT,
  102.                 false,
  103.                 Collection::class,
  104.                 true,
  105.                 new Type($collectionKeyType),
  106.                 new Type(Type::BUILTIN_TYPE_OBJECTfalse$class)
  107.             )];
  108.         }
  109.         if ($metadata instanceof ClassMetadataInfo && class_exists(Embedded::class) && isset($metadata->embeddedClasses[$property])) {
  110.             return [new Type(Type::BUILTIN_TYPE_OBJECTfalse$metadata->embeddedClasses[$property]['class'])];
  111.         }
  112.         if ($metadata->hasField($property)) {
  113.             $nullable $metadata instanceof ClassMetadataInfo && $metadata->isNullable($property);
  114.             if (null !== $enumClass $metadata->getFieldMapping($property)['enumType'] ?? null) {
  115.                 return [new Type(Type::BUILTIN_TYPE_OBJECT$nullable$enumClass)];
  116.             }
  117.             $typeOfField $metadata->getTypeOfField($property);
  118.             if (!$builtinType $this->getPhpType($typeOfField)) {
  119.                 return null;
  120.             }
  121.             switch ($builtinType) {
  122.                 case Type::BUILTIN_TYPE_OBJECT:
  123.                     switch ($typeOfField) {
  124.                         case Types::DATE_MUTABLE:
  125.                         case Types::DATETIME_MUTABLE:
  126.                         case Types::DATETIMETZ_MUTABLE:
  127.                         case 'vardatetime':
  128.                         case Types::TIME_MUTABLE:
  129.                             return [new Type(Type::BUILTIN_TYPE_OBJECT$nullable'DateTime')];
  130.                         case Types::DATE_IMMUTABLE:
  131.                         case Types::DATETIME_IMMUTABLE:
  132.                         case Types::DATETIMETZ_IMMUTABLE:
  133.                         case Types::TIME_IMMUTABLE:
  134.                             return [new Type(Type::BUILTIN_TYPE_OBJECT$nullable'DateTimeImmutable')];
  135.                         case Types::DATEINTERVAL:
  136.                             return [new Type(Type::BUILTIN_TYPE_OBJECT$nullable'DateInterval')];
  137.                     }
  138.                     break;
  139.                 case Type::BUILTIN_TYPE_ARRAY:
  140.                     switch ($typeOfField) {
  141.                         case Types::ARRAY:
  142.                         case 'json_array':
  143.                             return [new Type(Type::BUILTIN_TYPE_ARRAY$nullablenulltrue)];
  144.                         case 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(string $classstring $property, array $context = [])
  156.     {
  157.         return null;
  158.     }
  159.     /**
  160.      * {@inheritdoc}
  161.      */
  162.     public function isWritable(string $classstring $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->getClassMetadata($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 Types::SMALLINT:
  209.             case Types::INTEGER:
  210.                 return Type::BUILTIN_TYPE_INT;
  211.             case Types::FLOAT:
  212.                 return Type::BUILTIN_TYPE_FLOAT;
  213.             case Types::BIGINT:
  214.             case Types::STRING:
  215.             case Types::TEXT:
  216.             case Types::GUID:
  217.             case Types::DECIMAL:
  218.                 return Type::BUILTIN_TYPE_STRING;
  219.             case Types::BOOLEAN:
  220.                 return Type::BUILTIN_TYPE_BOOL;
  221.             case Types::BLOB:
  222.             case Types::BINARY:
  223.                 return Type::BUILTIN_TYPE_RESOURCE;
  224.             case Types::OBJECT:
  225.             case Types::DATE_MUTABLE:
  226.             case Types::DATETIME_MUTABLE:
  227.             case Types::DATETIMETZ_MUTABLE:
  228.             case 'vardatetime':
  229.             case Types::TIME_MUTABLE:
  230.             case Types::DATE_IMMUTABLE:
  231.             case Types::DATETIME_IMMUTABLE:
  232.             case Types::DATETIMETZ_IMMUTABLE:
  233.             case Types::TIME_IMMUTABLE:
  234.             case Types::DATEINTERVAL:
  235.                 return Type::BUILTIN_TYPE_OBJECT;
  236.             case Types::ARRAY:
  237.             case Types::SIMPLE_ARRAY:
  238.             case 'json_array':
  239.                 return Type::BUILTIN_TYPE_ARRAY;
  240.         }
  241.         return null;
  242.     }
  243. }