-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Validator][DoctrineBridge][FWBundle] Automatic data validation
- Loading branch information
Showing
8 changed files
with
291 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bridge\Doctrine\Validator; | ||
|
||
use Doctrine\Common\Persistence\Mapping\MappingException; | ||
use Doctrine\ORM\Mapping\ClassMetadataFactory; | ||
use Doctrine\ORM\Mapping\ClassMetadataInfo; | ||
use Doctrine\ORM\Mapping\MappingException as OrmMappingException; | ||
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; | ||
use Symfony\Component\Validator\Constraints\Length; | ||
use Symfony\Component\Validator\Mapping\ClassMetadata; | ||
use Symfony\Component\Validator\Mapping\ConstraintChecker; | ||
use Symfony\Component\Validator\Mapping\Loader\LoaderInterface; | ||
|
||
/** | ||
* Guesses and loads the appropriate constraints using Doctrine's metadata. | ||
* | ||
* @author Kévin Dunglas <dunglas@gmail.com> | ||
*/ | ||
final class DoctrineLoader implements LoaderInterface | ||
{ | ||
use ConstraintChecker; | ||
|
||
private $classMetadataFactory; | ||
|
||
public function __construct(ClassMetadataFactory $classMetadataFactory) | ||
{ | ||
$this->classMetadataFactory = $classMetadataFactory; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function loadClassMetadata(ClassMetadata $metadata): bool | ||
{ | ||
try { | ||
$doctrineMetadata = $this->classMetadataFactory->getMetadataFor($metadata->getClassName()); | ||
} catch (MappingException $exception) { | ||
return false; | ||
} catch (OrmMappingException $exception) { | ||
return false; | ||
} | ||
|
||
if (!$doctrineMetadata instanceof ClassMetadataInfo) { | ||
return false; | ||
} | ||
|
||
/* Available keys: | ||
- type | ||
- scale | ||
- length | ||
- unique | ||
- nullable | ||
- precision | ||
*/ | ||
|
||
$uniqueFields = array(); | ||
|
||
// Type and nullable aren't handled here, use the PropertyInfo Loader instead. | ||
foreach ($doctrineMetadata->fieldMappings as $mapping) { | ||
// TODO: Currently, I don't add a constraint if one of the same type already exists, but it's maybe safer to add both (min/max)? | ||
if (null !== $mapping['length'] && !$this->propertyHasConstraint($metadata, Length::class, $mapping['fieldName'])) { | ||
$metadata->addPropertyConstraint($mapping['fieldName'], new Length(array('max' => $mapping['length']))); | ||
} | ||
|
||
if (true === $mapping['unique']) { | ||
$uniqueFields[] = $mapping['fieldName']; | ||
} | ||
} | ||
|
||
if ($uniqueFields && !$this->classHasConstraint($metadata, UniqueEntity::class)) { | ||
$metadata->addConstraint(new UniqueEntity(array('fields' => $uniqueFields))); | ||
} | ||
|
||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
src/Symfony/Component/Validator/ExtraLoaderValidatorBuilderInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Validator; | ||
|
||
use Symfony\Component\Validator\Mapping\Loader\LoaderInterface; | ||
|
||
/** | ||
* Allows to register extra metadata loaders (for instance the Doctrine one). | ||
* | ||
* @author Kévin Dunglas <dunglas@gmail.com> | ||
*/ | ||
interface ExtraLoaderValidatorBuilderInterface extends ValidatorBuilderInterface | ||
{ | ||
/** | ||
* Adds a metadata loader at the end of the chain. | ||
* | ||
* @return $this | ||
*/ | ||
public function addLoader(LoaderInterface $loader): self; | ||
} |
46 changes: 46 additions & 0 deletions
46
src/Symfony/Component/Validator/Mapping/ConstraintChecker.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Validator\Mapping; | ||
|
||
/** | ||
* Helper methods to check if a property or a class has a given constraint. | ||
* | ||
* @internal | ||
* | ||
* @author Kévin Dunglas <dunglas@gmail.com> | ||
*/ | ||
trait ConstraintChecker | ||
{ | ||
private function propertyHasConstraint(ClassMetadata $metadata, string $constraintType, string $fieldName): bool | ||
{ | ||
foreach ($metadata->getPropertyMetadata($fieldName) as $propertyMetadata) { | ||
foreach ($propertyMetadata->getConstraints() as $constraint) { | ||
if (is_a($constraint, $constraintType, true)) { | ||
return true; | ||
} | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
private function classHasConstraint(ClassMetadata $metadata, string $constraintType): bool | ||
{ | ||
foreach ($metadata->getConstraints() as $constraint) { | ||
if (is_a($constraint, $constraintType, true)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} |
85 changes: 85 additions & 0 deletions
85
src/Symfony/Component/Validator/Mapping/Loader/PropertyInfoLoader.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Validator\Mapping\Loader; | ||
|
||
use Symfony\Component\PropertyInfo\PropertyListExtractorInterface; | ||
use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface; | ||
use Symfony\Component\PropertyInfo\Type as PropertyInfoType; | ||
use Symfony\Component\Validator\Constraints\NotNull; | ||
use Symfony\Component\Validator\Constraints\Type; | ||
use Symfony\Component\Validator\Mapping\ClassMetadata; | ||
use Symfony\Component\Validator\Mapping\ConstraintChecker; | ||
|
||
/** | ||
* Guesses and loads the appropriate constraints using PropertyInfo. | ||
* | ||
* @author Kévin Dunglas <dunglas@gmail.com> | ||
*/ | ||
class PropertyInfoLoader implements LoaderInterface | ||
{ | ||
use ConstraintChecker; | ||
|
||
private $listExtractor; | ||
private $typeExtractor; | ||
|
||
public function __construct(PropertyListExtractorInterface $listExtractor, PropertyTypeExtractorInterface $typeExtractor) | ||
{ | ||
$this->listExtractor = $listExtractor; | ||
$this->typeExtractor = $typeExtractor; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function loadClassMetadata(ClassMetadata $metadata) | ||
{ | ||
$class = $metadata->getClassName(); | ||
if (!$properties = $this->listExtractor->getProperties($class)) { | ||
return; | ||
} | ||
|
||
foreach ($properties as $property) { | ||
$types = $this->typeExtractor->getTypes($metadata->getClassName(), $property); | ||
if (null === $types) { | ||
continue; | ||
} | ||
|
||
$builtinTypes = array(); | ||
$nullable = false; | ||
$scalar = true; | ||
|
||
foreach ($types as $type) { | ||
$builtinTypes[] = $type->getBuiltinType(); | ||
|
||
if ($scalar && !\in_array($type->getBuiltinType(), array(PropertyInfoType::BUILTIN_TYPE_INT, PropertyInfoType::BUILTIN_TYPE_FLOAT, PropertyInfoType::BUILTIN_TYPE_STRING, PropertyInfoType::BUILTIN_TYPE_BOOL))) { | ||
$scalar = false; | ||
} | ||
|
||
if (!$nullable && $type->isNullable()) { | ||
$nullable = true; | ||
} | ||
} | ||
|
||
if (!$this->propertyHasConstraint($metadata, Type::class, $property)) { | ||
if (1 === \count($builtinTypes)) { | ||
$metadata->addPropertyConstraint($property, new Type(array('type' => $builtinTypes[0]))); | ||
} elseif ($scalar) { | ||
$metadata->addPropertyConstraint($property, new Type(array('type' => 'scalar'))); | ||
} | ||
} | ||
|
||
if (!$nullable && !$this->propertyHasConstraint($metadata, NotNull::class, $property)) { | ||
$metadata->addPropertyConstraint($property, new NotNull()); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters