vendor/symfony/doctrine-bridge/Validator/DoctrineLoader.php line 39

  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\Validator;
  11. use Doctrine\ORM\EntityManagerInterface;
  12. use Doctrine\ORM\Mapping\ClassMetadataInfo;
  13. use Doctrine\ORM\Mapping\MappingException as OrmMappingException;
  14. use Doctrine\Persistence\Mapping\MappingException;
  15. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  16. use Symfony\Component\PropertyInfo\Type;
  17. use Symfony\Component\Validator\Constraints\Length;
  18. use Symfony\Component\Validator\Constraints\Valid;
  19. use Symfony\Component\Validator\Mapping\AutoMappingStrategy;
  20. use Symfony\Component\Validator\Mapping\ClassMetadata;
  21. use Symfony\Component\Validator\Mapping\Loader\AutoMappingTrait;
  22. use Symfony\Component\Validator\Mapping\Loader\LoaderInterface;
  23. /**
  24.  * Guesses and loads the appropriate constraints using Doctrine's metadata.
  25.  *
  26.  * @author Kévin Dunglas <dunglas@gmail.com>
  27.  */
  28. final class DoctrineLoader implements LoaderInterface
  29. {
  30.     use AutoMappingTrait;
  31.     private EntityManagerInterface $entityManager;
  32.     private ?string $classValidatorRegexp;
  33.     public function __construct(EntityManagerInterface $entityManagerstring $classValidatorRegexp null)
  34.     {
  35.         $this->entityManager $entityManager;
  36.         $this->classValidatorRegexp $classValidatorRegexp;
  37.     }
  38.     public function loadClassMetadata(ClassMetadata $metadata): bool
  39.     {
  40.         $className $metadata->getClassName();
  41.         try {
  42.             $doctrineMetadata $this->entityManager->getClassMetadata($className);
  43.         } catch (MappingException|OrmMappingException) {
  44.             return false;
  45.         }
  46.         if (!$doctrineMetadata instanceof ClassMetadataInfo) {
  47.             return false;
  48.         }
  49.         $loaded false;
  50.         $enabledForClass $this->isAutoMappingEnabledForClass($metadata$this->classValidatorRegexp);
  51.         /* Available keys:
  52.            - type
  53.            - scale
  54.            - length
  55.            - unique
  56.            - nullable
  57.            - precision
  58.          */
  59.         $existingUniqueFields $this->getExistingUniqueFields($metadata);
  60.         // Type and nullable aren't handled here, use the PropertyInfo Loader instead.
  61.         foreach ($doctrineMetadata->fieldMappings as $mapping) {
  62.             $enabledForProperty $enabledForClass;
  63.             $lengthConstraint null;
  64.             foreach ($metadata->getPropertyMetadata($mapping['fieldName']) as $propertyMetadata) {
  65.                 // Enabling or disabling auto-mapping explicitly always takes precedence
  66.                 if (AutoMappingStrategy::DISABLED === $propertyMetadata->getAutoMappingStrategy()) {
  67.                     continue 2;
  68.                 }
  69.                 if (AutoMappingStrategy::ENABLED === $propertyMetadata->getAutoMappingStrategy()) {
  70.                     $enabledForProperty true;
  71.                 }
  72.                 foreach ($propertyMetadata->getConstraints() as $constraint) {
  73.                     if ($constraint instanceof Length) {
  74.                         $lengthConstraint $constraint;
  75.                     }
  76.                 }
  77.             }
  78.             if (!$enabledForProperty) {
  79.                 continue;
  80.             }
  81.             if (true === ($mapping['unique'] ?? false) && !isset($existingUniqueFields[$mapping['fieldName']])) {
  82.                 $metadata->addConstraint(new UniqueEntity(['fields' => $mapping['fieldName']]));
  83.                 $loaded true;
  84.             }
  85.             if (null === ($mapping['length'] ?? null) || null !== ($mapping['enumType'] ?? null) || !\in_array($mapping['type'], ['string''text'], true)) {
  86.                 continue;
  87.             }
  88.             if (null === $lengthConstraint) {
  89.                 if (isset($mapping['originalClass']) && !str_contains($mapping['declaredField'], '.')) {
  90.                     $metadata->addPropertyConstraint($mapping['declaredField'], new Valid());
  91.                     $loaded true;
  92.                 } elseif (property_exists($className$mapping['fieldName'])) {
  93.                     $metadata->addPropertyConstraint($mapping['fieldName'], new Length(['max' => $mapping['length']]));
  94.                     $loaded true;
  95.                 }
  96.             } elseif (null === $lengthConstraint->max) {
  97.                 // If a Length constraint exists and no max length has been explicitly defined, set it
  98.                 $lengthConstraint->max $mapping['length'];
  99.             }
  100.         }
  101.         return $loaded;
  102.     }
  103.     private function getExistingUniqueFields(ClassMetadata $metadata): array
  104.     {
  105.         $fields = [];
  106.         foreach ($metadata->getConstraints() as $constraint) {
  107.             if (!$constraint instanceof UniqueEntity) {
  108.                 continue;
  109.             }
  110.             if (\is_string($constraint->fields)) {
  111.                 $fields[$constraint->fields] = true;
  112.             } elseif (\is_array($constraint->fields) && === \count($constraint->fields)) {
  113.                 $fields[$constraint->fields[0]] = true;
  114.             }
  115.         }
  116.         return $fields;
  117.     }
  118. }