vendor/api-platform/core/src/Core/Bridge/Symfony/Validator/Metadata/Property/ValidatorPropertyMetadataFactory.php line 88

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the API Platform project.
  4.  *
  5.  * (c) Kévin Dunglas <dunglas@gmail.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. declare(strict_types=1);
  11. namespace ApiPlatform\Core\Bridge\Symfony\Validator\Metadata\Property;
  12. use ApiPlatform\Core\Bridge\Symfony\Validator\Metadata\Property\Restriction\PropertySchemaRestrictionMetadataInterface;
  13. use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
  14. use ApiPlatform\Core\Metadata\Property\PropertyMetadata;
  15. use Symfony\Component\Validator\Constraint;
  16. use Symfony\Component\Validator\Constraints\Bic;
  17. use Symfony\Component\Validator\Constraints\CardScheme;
  18. use Symfony\Component\Validator\Constraints\Compound;
  19. use Symfony\Component\Validator\Constraints\Currency;
  20. use Symfony\Component\Validator\Constraints\Date;
  21. use Symfony\Component\Validator\Constraints\DateTime;
  22. use Symfony\Component\Validator\Constraints\Email;
  23. use Symfony\Component\Validator\Constraints\File;
  24. use Symfony\Component\Validator\Constraints\Iban;
  25. use Symfony\Component\Validator\Constraints\Image;
  26. use Symfony\Component\Validator\Constraints\Isbn;
  27. use Symfony\Component\Validator\Constraints\Issn;
  28. use Symfony\Component\Validator\Constraints\NotBlank;
  29. use Symfony\Component\Validator\Constraints\NotNull;
  30. use Symfony\Component\Validator\Constraints\Sequentially;
  31. use Symfony\Component\Validator\Constraints\Time;
  32. use Symfony\Component\Validator\Constraints\Url;
  33. use Symfony\Component\Validator\Constraints\Uuid;
  34. use Symfony\Component\Validator\Mapping\ClassMetadataInterface as ValidatorClassMetadataInterface;
  35. use Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface as ValidatorMetadataFactoryInterface;
  36. use Symfony\Component\Validator\Mapping\PropertyMetadataInterface as ValidatorPropertyMetadataInterface;
  37. /**
  38.  * Decorates a metadata loader using the validator.
  39.  *
  40.  * @author Kévin Dunglas <dunglas@gmail.com>
  41.  */
  42. final class ValidatorPropertyMetadataFactory implements PropertyMetadataFactoryInterface
  43. {
  44.     /**
  45.      * @var string[] A list of constraint classes making the entity required
  46.      */
  47.     public const REQUIRED_CONSTRAINTS = [NotBlank::class, NotNull::class];
  48.     public const SCHEMA_MAPPED_CONSTRAINTS = [
  49.         Url::class => 'http://schema.org/url',
  50.         Email::class => 'http://schema.org/email',
  51.         Uuid::class => 'http://schema.org/identifier',
  52.         CardScheme::class => 'http://schema.org/identifier',
  53.         Bic::class => 'http://schema.org/identifier',
  54.         Iban::class => 'http://schema.org/identifier',
  55.         Date::class => 'http://schema.org/Date',
  56.         DateTime::class => 'http://schema.org/DateTime',
  57.         Time::class => 'http://schema.org/Time',
  58.         Image::class => 'http://schema.org/image',
  59.         File::class => 'http://schema.org/MediaObject',
  60.         Currency::class => 'http://schema.org/priceCurrency',
  61.         Isbn::class => 'http://schema.org/isbn',
  62.         Issn::class => 'http://schema.org/issn',
  63.     ];
  64.     private $decorated;
  65.     private $validatorMetadataFactory;
  66.     /**
  67.      * @var iterable<PropertySchemaRestrictionMetadataInterface>
  68.      */
  69.     private $restrictionsMetadata;
  70.     /**
  71.      * @param PropertySchemaRestrictionMetadataInterface[] $restrictionsMetadata
  72.      */
  73.     public function __construct(ValidatorMetadataFactoryInterface $validatorMetadataFactoryPropertyMetadataFactoryInterface $decoratediterable $restrictionsMetadata = [])
  74.     {
  75.         $this->validatorMetadataFactory $validatorMetadataFactory;
  76.         $this->decorated $decorated;
  77.         $this->restrictionsMetadata $restrictionsMetadata;
  78.     }
  79.     public function create(string $resourceClassstring $property, array $options = []): PropertyMetadata
  80.     {
  81.         $propertyMetadata $this->decorated->create($resourceClass$property$options);
  82.         $required $propertyMetadata->isRequired();
  83.         $iri $propertyMetadata->getIri();
  84.         $schema $propertyMetadata->getSchema();
  85.         if (null !== $required && null !== $iri && null !== $schema) {
  86.             return $propertyMetadata;
  87.         }
  88.         $validatorClassMetadata $this->validatorMetadataFactory->getMetadataFor($resourceClass);
  89.         if (!$validatorClassMetadata instanceof ValidatorClassMetadataInterface) {
  90.             throw new \UnexpectedValueException(sprintf('Validator class metadata expected to be of type "%s".'ValidatorClassMetadataInterface::class));
  91.         }
  92.         $validationGroups $this->getValidationGroups($validatorClassMetadata$options);
  93.         $restrictions = [];
  94.         foreach ($validatorClassMetadata->getPropertyMetadata($property) as $validatorPropertyMetadata) {
  95.             foreach ($this->getPropertyConstraints($validatorPropertyMetadata$validationGroups) as $constraint) {
  96.                 if (null === $required && $this->isRequired($constraint)) {
  97.                     $required true;
  98.                 }
  99.                 if (null === $iri) {
  100.                     $iri self::SCHEMA_MAPPED_CONSTRAINTS[\get_class($constraint)] ?? null;
  101.                 }
  102.                 foreach ($this->restrictionsMetadata as $restrictionMetadata) {
  103.                     if ($restrictionMetadata->supports($constraint$propertyMetadata)) {
  104.                         $restrictions[] = $restrictionMetadata->create($constraint$propertyMetadata);
  105.                     }
  106.                 }
  107.             }
  108.         }
  109.         $propertyMetadata $propertyMetadata->withIri($iri)->withRequired($required ?? false);
  110.         if (!empty($restrictions)) {
  111.             if (null === $schema) {
  112.                 $schema = [];
  113.             }
  114.             $schema += array_merge(...$restrictions);
  115.             $propertyMetadata $propertyMetadata->withSchema($schema);
  116.         }
  117.         return $propertyMetadata;
  118.     }
  119.     /**
  120.      * Returns the list of validation groups.
  121.      */
  122.     private function getValidationGroups(ValidatorClassMetadataInterface $classMetadata, array $options): array
  123.     {
  124.         if (isset($options['validation_groups'])) {
  125.             return $options['validation_groups'];
  126.         }
  127.         if (!method_exists($classMetadata'getDefaultGroup')) {
  128.             throw new \UnexpectedValueException(sprintf('Validator class metadata expected to have method "%s".''getDefaultGroup'));
  129.         }
  130.         return [$classMetadata->getDefaultGroup()];
  131.     }
  132.     /**
  133.      * Tests if the property is required because of its validation groups.
  134.      */
  135.     private function getPropertyConstraints(
  136.         ValidatorPropertyMetadataInterface $validatorPropertyMetadata,
  137.         array $groups
  138.     ): array {
  139.         $constraints = [];
  140.         foreach ($groups as $validationGroup) {
  141.             if (!\is_string($validationGroup)) {
  142.                 continue;
  143.             }
  144.             foreach ($validatorPropertyMetadata->findConstraints($validationGroup) as $propertyConstraint) {
  145.                 if ($propertyConstraint instanceof Sequentially || $propertyConstraint instanceof Compound) {
  146.                     $constraints[] = method_exists($propertyConstraint'getNestedContraints') ? $propertyConstraint->getNestedContraints() : $propertyConstraint->getNestedConstraints();
  147.                 } else {
  148.                     $constraints[] = [$propertyConstraint];
  149.                 }
  150.             }
  151.         }
  152.         return array_merge([], ...$constraints);
  153.     }
  154.     /**
  155.      * Is this constraint making the related property required?
  156.      */
  157.     private function isRequired(Constraint $constraint): bool
  158.     {
  159.         foreach (self::REQUIRED_CONSTRAINTS as $requiredConstraint) {
  160.             if ($constraint instanceof $requiredConstraint) {
  161.                 return true;
  162.             }
  163.         }
  164.         return false;
  165.     }
  166. }