From 2e4f2ac32238c771d6358c16d63e8d6882c141c4 Mon Sep 17 00:00:00 2001 From: Jan Vernieuwe Date: Mon, 13 May 2019 09:29:58 +0200 Subject: [PATCH] [Validator] add Validation::createCallable() --- .../Exception/ValidationFailedException.php | 40 +++++++++++++++++++ .../Validator/Tests/ValidationTest.php | 36 +++++++++++++++++ .../Component/Validator/Validation.php | 18 +++++++++ 3 files changed, 94 insertions(+) create mode 100644 src/Symfony/Component/Validator/Exception/ValidationFailedException.php create mode 100644 src/Symfony/Component/Validator/Tests/ValidationTest.php diff --git a/src/Symfony/Component/Validator/Exception/ValidationFailedException.php b/src/Symfony/Component/Validator/Exception/ValidationFailedException.php new file mode 100644 index 000000000000..ca0314cf6e77 --- /dev/null +++ b/src/Symfony/Component/Validator/Exception/ValidationFailedException.php @@ -0,0 +1,40 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Validator\Exception; + +use Symfony\Component\Validator\ConstraintViolationListInterface; + +/** + * @author Jan Vernieuwe + */ +class ValidationFailedException extends RuntimeException +{ + private $violations; + private $value; + + public function __construct($value, ConstraintViolationListInterface $violations) + { + $this->violations = $violations; + $this->value = $value; + parent::__construct($violations); + } + + public function getValue() + { + return $this->value; + } + + public function getViolations(): ConstraintViolationListInterface + { + return $this->violations; + } +} diff --git a/src/Symfony/Component/Validator/Tests/ValidationTest.php b/src/Symfony/Component/Validator/Tests/ValidationTest.php new file mode 100644 index 000000000000..0b8888a5606e --- /dev/null +++ b/src/Symfony/Component/Validator/Tests/ValidationTest.php @@ -0,0 +1,36 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Validator\Tests\Validator; + +use PHPUnit\Framework\TestCase; +use Symfony\Component\Validator\Constraints\Email; +use Symfony\Component\Validator\Exception\ValidationFailedException; +use Symfony\Component\Validator\Validation; + +/** + * @author Jan Vernieuwe + */ +class ValidationTest extends TestCase +{ + public function testCreateCallableValid() + { + $validator = Validation::createCallable(new Email()); + $this->assertEquals('test@example.com', $validator('test@example.com')); + } + + public function testCreateCallableInvalid() + { + $validator = Validation::createCallable(new Email()); + $this->expectException(ValidationFailedException::class); + $validator('test'); + } +} diff --git a/src/Symfony/Component/Validator/Validation.php b/src/Symfony/Component/Validator/Validation.php index dbf1dbc0ed7d..a070242e60de 100644 --- a/src/Symfony/Component/Validator/Validation.php +++ b/src/Symfony/Component/Validator/Validation.php @@ -11,6 +11,7 @@ namespace Symfony\Component\Validator; +use Symfony\Component\Validator\Exception\ValidationFailedException; use Symfony\Component\Validator\Validator\ValidatorInterface; /** @@ -20,6 +21,23 @@ */ final class Validation { + /** + * Creates a callable chain of constraints. + */ + public static function createCallable(Constraint ...$constraints): callable + { + $validator = self::createValidator(); + + return static function ($value) use ($constraints, $validator) { + $violations = $validator->validate($value, $constraints); + if (0 !== $violations->count()) { + throw new ValidationFailedException($value, $violations); + } + + return $value; + }; + } + /** * Creates a new validator. *