vendor/symfony/validator/Validation.php line 56

  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\Component\Validator;
  11. use Symfony\Component\Validator\Exception\ValidationFailedException;
  12. use Symfony\Component\Validator\Validator\ValidatorInterface;
  13. /**
  14.  * Entry point for the Validator component.
  15.  *
  16.  * @author Bernhard Schussek <bschussek@gmail.com>
  17.  */
  18. final class Validation
  19. {
  20.     /**
  21.      * Creates a callable chain of constraints.
  22.      */
  23.     public static function createCallable(Constraint|ValidatorInterface $constraintOrValidator nullConstraint ...$constraints): callable
  24.     {
  25.         $validator self::createIsValidCallable($constraintOrValidator, ...$constraints);
  26.         return static function ($value) use ($validator) {
  27.             if (!$validator($value$violations)) {
  28.                 throw new ValidationFailedException($value$violations);
  29.             }
  30.             return $value;
  31.         };
  32.     }
  33.     /**
  34.      * Creates a callable that returns true/false instead of throwing validation exceptions.
  35.      *
  36.      * @return callable(mixed $value, ConstraintViolationListInterface &$violations = null): bool
  37.      */
  38.     public static function createIsValidCallable(Constraint|ValidatorInterface $constraintOrValidator nullConstraint ...$constraints): callable
  39.     {
  40.         $validator $constraintOrValidator;
  41.         if ($constraintOrValidator instanceof Constraint) {
  42.             $constraints \func_get_args();
  43.             $validator null;
  44.         }
  45.         $validator ??= self::createValidator();
  46.         return static function (mixed $valueConstraintViolationListInterface &$violations null) use ($constraints$validator): bool {
  47.             $violations $validator->validate($value$constraints);
  48.             return === $violations->count();
  49.         };
  50.     }
  51.     /**
  52.      * Creates a new validator.
  53.      *
  54.      * If you want to configure the validator, use
  55.      * {@link createValidatorBuilder()} instead.
  56.      */
  57.     public static function createValidator(): ValidatorInterface
  58.     {
  59.         return self::createValidatorBuilder()->getValidator();
  60.     }
  61.     /**
  62.      * Creates a configurable builder for validator objects.
  63.      */
  64.     public static function createValidatorBuilder(): ValidatorBuilder
  65.     {
  66.         return new ValidatorBuilder();
  67.     }
  68.     /**
  69.      * This class cannot be instantiated.
  70.      */
  71.     private function __construct()
  72.     {
  73.     }
  74. }