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

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