|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Validator\Mapping\Loader; |
| 13 | + |
| 14 | +use Symfony\Component\PropertyInfo\PropertyListExtractorInterface; |
| 15 | +use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface; |
| 16 | +use Symfony\Component\PropertyInfo\Type as PropertyInfoType; |
| 17 | +use Symfony\Component\Validator\Constraints\All; |
| 18 | +use Symfony\Component\Validator\Constraints\NotBlank; |
| 19 | +use Symfony\Component\Validator\Constraints\NotNull; |
| 20 | +use Symfony\Component\Validator\Constraints\Type; |
| 21 | +use Symfony\Component\Validator\Mapping\ClassMetadata; |
| 22 | + |
| 23 | +/** |
| 24 | + * Guesses and loads the appropriate constraints using PropertyInfo. |
| 25 | + * |
| 26 | + * @author Kévin Dunglas <dunglas@gmail.com> |
| 27 | + */ |
| 28 | +final class PropertyInfoLoader implements LoaderInterface |
| 29 | +{ |
| 30 | + private $listExtractor; |
| 31 | + private $typeExtractor; |
| 32 | + private $classValidatorRegexp; |
| 33 | + |
| 34 | + public function __construct(PropertyListExtractorInterface $listExtractor, PropertyTypeExtractorInterface $typeExtractor, string $classValidatorRegexp = null) |
| 35 | + { |
| 36 | + $this->listExtractor = $listExtractor; |
| 37 | + $this->typeExtractor = $typeExtractor; |
| 38 | + $this->classValidatorRegexp = $classValidatorRegexp; |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * {@inheritdoc} |
| 43 | + */ |
| 44 | + public function loadClassMetadata(ClassMetadata $metadata) |
| 45 | + { |
| 46 | + $className = $metadata->getClassName(); |
| 47 | + if (null !== $this->classValidatorRegexp && !preg_match($this->classValidatorRegexp, $className)) { |
| 48 | + return false; |
| 49 | + } |
| 50 | + |
| 51 | + if (!$properties = $this->listExtractor->getProperties($className)) { |
| 52 | + return false; |
| 53 | + } |
| 54 | + |
| 55 | + foreach ($properties as $property) { |
| 56 | + $types = $this->typeExtractor->getTypes($className, $property); |
| 57 | + if (null === $types) { |
| 58 | + continue; |
| 59 | + } |
| 60 | + |
| 61 | + $hasTypeConstraint = false; |
| 62 | + $hasNotNullConstraint = false; |
| 63 | + $hasNotBlankConstraint = false; |
| 64 | + $allConstraint = null; |
| 65 | + foreach ($metadata->getPropertyMetadata($property) as $propertyMetadata) { |
| 66 | + foreach ($propertyMetadata->getConstraints() as $constraint) { |
| 67 | + if ($constraint instanceof Type) { |
| 68 | + $hasTypeConstraint = true; |
| 69 | + } elseif ($constraint instanceof NotNull) { |
| 70 | + $hasNotNullConstraint = true; |
| 71 | + } elseif ($constraint instanceof NotBlank) { |
| 72 | + $hasNotBlankConstraint = true; |
| 73 | + } elseif ($constraint instanceof All) { |
| 74 | + $allConstraint = $constraint; |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + $builtinTypes = []; |
| 80 | + $nullable = false; |
| 81 | + $scalar = true; |
| 82 | + foreach ($types as $type) { |
| 83 | + $builtinTypes[] = $type->getBuiltinType(); |
| 84 | + |
| 85 | + if ($scalar && !\in_array($type->getBuiltinType(), [PropertyInfoType::BUILTIN_TYPE_INT, PropertyInfoType::BUILTIN_TYPE_FLOAT, PropertyInfoType::BUILTIN_TYPE_STRING, PropertyInfoType::BUILTIN_TYPE_BOOL], true)) { |
| 86 | + $scalar = false; |
| 87 | + } |
| 88 | + |
| 89 | + if (!$nullable && $type->isNullable()) { |
| 90 | + $nullable = true; |
| 91 | + } |
| 92 | + } |
| 93 | + if (!$hasTypeConstraint) { |
| 94 | + if (1 === \count($builtinTypes)) { |
| 95 | + if ($types[0]->isCollection() && (null !== $collectionValueType = $types[0]->getCollectionValueType())) { |
| 96 | + $this->handleAllConstraint($property, $allConstraint, $collectionValueType, $metadata); |
| 97 | + } |
| 98 | + |
| 99 | + $metadata->addPropertyConstraint($property, $this->getTypeConstraint($builtinTypes[0], $types[0])); |
| 100 | + } elseif ($scalar) { |
| 101 | + $metadata->addPropertyConstraint($property, new Type(['type' => 'scalar'])); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + if (!$nullable && !$hasNotBlankConstraint && !$hasNotNullConstraint) { |
| 106 | + $metadata->addPropertyConstraint($property, new NotNull()); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + return true; |
| 111 | + } |
| 112 | + |
| 113 | + private function getTypeConstraint(string $builtinType, PropertyInfoType $type): Type |
| 114 | + { |
| 115 | + if (PropertyInfoType::BUILTIN_TYPE_OBJECT === $builtinType && null !== $className = $type->getClassName()) { |
| 116 | + return new Type(['type' => $className]); |
| 117 | + } |
| 118 | + |
| 119 | + return new Type(['type' => $builtinType]); |
| 120 | + } |
| 121 | + |
| 122 | + private function handleAllConstraint(string $property, ?All $allConstraint, PropertyInfoType $propertyInfoType, ClassMetadata $metadata) |
| 123 | + { |
| 124 | + $containsTypeConstraint = false; |
| 125 | + $containsNotNullConstraint = false; |
| 126 | + if (null !== $allConstraint) { |
| 127 | + foreach ($allConstraint->constraints as $constraint) { |
| 128 | + if ($constraint instanceof Type) { |
| 129 | + $containsTypeConstraint = true; |
| 130 | + } elseif ($constraint instanceof NotNull) { |
| 131 | + $containsNotNullConstraint = true; |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + $constraints = []; |
| 137 | + if (!$containsNotNullConstraint && !$propertyInfoType->isNullable()) { |
| 138 | + $constraints[] = new NotNull(); |
| 139 | + } |
| 140 | + |
| 141 | + if (!$containsTypeConstraint) { |
| 142 | + $constraints[] = $this->getTypeConstraint($propertyInfoType->getBuiltinType(), $propertyInfoType); |
| 143 | + } |
| 144 | + |
| 145 | + if (null === $allConstraint) { |
| 146 | + $metadata->addPropertyConstraint($property, new All(['constraints' => $constraints])); |
| 147 | + } else { |
| 148 | + $allConstraint->constraints = array_merge($allConstraint->constraints, $constraints); |
| 149 | + } |
| 150 | + } |
| 151 | +} |
0 commit comments