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

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\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
  13. use ApiPlatform\Core\Metadata\Property\PropertyMetadata;
  14. use Symfony\Component\Validator\Constraint;
  15. use Symfony\Component\Validator\Constraints\Bic;
  16. use Symfony\Component\Validator\Constraints\CardScheme;
  17. use Symfony\Component\Validator\Constraints\Currency;
  18. use Symfony\Component\Validator\Constraints\Date;
  19. use Symfony\Component\Validator\Constraints\DateTime;
  20. use Symfony\Component\Validator\Constraints\Email;
  21. use Symfony\Component\Validator\Constraints\File;
  22. use Symfony\Component\Validator\Constraints\Iban;
  23. use Symfony\Component\Validator\Constraints\Image;
  24. use Symfony\Component\Validator\Constraints\Isbn;
  25. use Symfony\Component\Validator\Constraints\Issn;
  26. use Symfony\Component\Validator\Constraints\NotBlank;
  27. use Symfony\Component\Validator\Constraints\NotNull;
  28. use Symfony\Component\Validator\Constraints\Time;
  29. use Symfony\Component\Validator\Constraints\Url;
  30. use Symfony\Component\Validator\Constraints\Uuid;
  31. use Symfony\Component\Validator\Mapping\ClassMetadataInterface as ValidatorClassMetadataInterface;
  32. use Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface as ValidatorMetadataFactoryInterface;
  33. use Symfony\Component\Validator\Mapping\PropertyMetadataInterface as ValidatorPropertyMetadataInterface;
  34. /**
  35.  * Decorates a metadata loader using the validator.
  36.  *
  37.  * @author Kévin Dunglas <dunglas@gmail.com>
  38.  */
  39. final class ValidatorPropertyMetadataFactory implements PropertyMetadataFactoryInterface
  40. {
  41.     /**
  42.      * @var string[] A list of constraint classes making the entity required
  43.      */
  44.     public const REQUIRED_CONSTRAINTS = [NotBlank::class, NotNull::class];
  45.     public const SCHEMA_MAPPED_CONSTRAINTS = [
  46.         Url::class => 'http://schema.org/url',
  47.         Email::class => 'http://schema.org/email',
  48.         Uuid::class => 'http://schema.org/identifier',
  49.         CardScheme::class => 'http://schema.org/identifier',
  50.         Bic::class => 'http://schema.org/identifier',
  51.         Iban::class => 'http://schema.org/identifier',
  52.         Date::class => 'http://schema.org/Date',
  53.         DateTime::class => 'http://schema.org/DateTime',
  54.         Time::class => 'http://schema.org/Time',
  55.         Image::class => 'http://schema.org/image',
  56.         File::class => 'http://schema.org/MediaObject',
  57.         Currency::class => 'http://schema.org/priceCurrency',
  58.         Isbn::class => 'http://schema.org/isbn',
  59.         Issn::class => 'http://schema.org/issn',
  60.     ];
  61.     private $decorated;
  62.     private $validatorMetadataFactory;
  63.     public function __construct(ValidatorMetadataFactoryInterface $validatorMetadataFactoryPropertyMetadataFactoryInterface $decorated)
  64.     {
  65.         $this->validatorMetadataFactory $validatorMetadataFactory;
  66.         $this->decorated $decorated;
  67.     }
  68.     /**
  69.      * {@inheritdoc}
  70.      */
  71.     public function create(string $resourceClassstring $name, array $options = []): PropertyMetadata
  72.     {
  73.         $propertyMetadata $this->decorated->create($resourceClass$name$options);
  74.         $required $propertyMetadata->isRequired();
  75.         $iri $propertyMetadata->getIri();
  76.         if (null !== $required && null !== $iri) {
  77.             return $propertyMetadata;
  78.         }
  79.         $validatorClassMetadata $this->validatorMetadataFactory->getMetadataFor($resourceClass);
  80.         if (!$validatorClassMetadata instanceof ValidatorClassMetadataInterface) {
  81.             throw new \UnexpectedValueException(sprintf('Validator class metadata expected to be of type "%s".'ValidatorClassMetadataInterface::class));
  82.         }
  83.         foreach ($validatorClassMetadata->getPropertyMetadata($name) as $validatorPropertyMetadata) {
  84.             if (null === $required && isset($options['validation_groups'])) {
  85.                 $required $this->isRequiredByGroups($validatorPropertyMetadata$options);
  86.             }
  87.             if (!method_exists($validatorClassMetadata'getDefaultGroup')) {
  88.                 throw new \UnexpectedValueException(sprintf('Validator class metadata expected to have method "%s".''getDefaultGroup'));
  89.             }
  90.             foreach ($validatorPropertyMetadata->findConstraints($validatorClassMetadata->getDefaultGroup()) as $constraint) {
  91.                 if (null === $required && $this->isRequired($constraint)) {
  92.                     $required true;
  93.                 }
  94.                 if (null === $iri) {
  95.                     $iri self::SCHEMA_MAPPED_CONSTRAINTS[\get_class($constraint)] ?? null;
  96.                 }
  97.                 if (null !== $required && null !== $iri) {
  98.                     break 2;
  99.                 }
  100.             }
  101.         }
  102.         return $propertyMetadata->withIri($iri)->withRequired($required ?? false);
  103.     }
  104.     /**
  105.      * Tests if the property is required because of its validation groups.
  106.      */
  107.     private function isRequiredByGroups(ValidatorPropertyMetadataInterface $validatorPropertyMetadata, array $options): bool
  108.     {
  109.         foreach ($options['validation_groups'] as $validationGroup) {
  110.             if (!\is_string($validationGroup)) {
  111.                 continue;
  112.             }
  113.             foreach ($validatorPropertyMetadata->findConstraints($validationGroup) as $constraint) {
  114.                 if ($this->isRequired($constraint)) {
  115.                     return true;
  116.                 }
  117.             }
  118.         }
  119.         return false;
  120.     }
  121.     /**
  122.      * Is this constraint making the related property required?
  123.      */
  124.     private function isRequired(Constraint $constraint): bool
  125.     {
  126.         foreach (self::REQUIRED_CONSTRAINTS as $requiredConstraint) {
  127.             if ($constraint instanceof $requiredConstraint) {
  128.                 return true;
  129.             }
  130.         }
  131.         return false;
  132.     }
  133. }