File tree Expand file tree Collapse file tree 3 files changed +94
-0
lines changed Expand file tree Collapse file tree 3 files changed +94
-0
lines changed Original file line number Diff line number Diff line change 1+ <?php
2+
3+ /*
4+ * This file is part of the Symfony package.
5+ *
6+ * (c) Fabien Potencier <fabien@symfony.com>
7+ *
8+ * For the full copyright and license information, please view the LICENSE
9+ * file that was distributed with this source code.
10+ */
11+
12+ namespace Symfony \Component \Validator \Exception ;
13+
14+ use Symfony \Component \Validator \ConstraintViolationListInterface ;
15+
16+ /**
17+ * @author Jan Vernieuwe <jan.vernieuwe@phpro.be>
18+ */
19+ class ValidationFailedException extends RuntimeException
20+ {
21+ private $ violations ;
22+ private $ value ;
23+
24+ public function __construct ($ value , ConstraintViolationListInterface $ violations )
25+ {
26+ $ this ->violations = $ violations ;
27+ $ this ->value = $ value ;
28+ parent ::__construct ($ violations );
29+ }
30+
31+ public function getValue ()
32+ {
33+ return $ this ->value ;
34+ }
35+
36+ public function getViolations (): ConstraintViolationListInterface
37+ {
38+ return $ this ->violations ;
39+ }
40+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ /*
4+ * This file is part of the Symfony package.
5+ *
6+ * (c) Fabien Potencier <fabien@symfony.com>
7+ *
8+ * For the full copyright and license information, please view the LICENSE
9+ * file that was distributed with this source code.
10+ */
11+
12+ namespace Symfony \Component \Validator \Tests \Validator ;
13+
14+ use PHPUnit \Framework \TestCase ;
15+ use Symfony \Component \Validator \Constraints \Email ;
16+ use Symfony \Component \Validator \Exception \ValidationFailedException ;
17+ use Symfony \Component \Validator \Validation ;
18+
19+ /**
20+ * @author Jan Vernieuwe <jan.vernieuwe@phpro.be>
21+ */
22+ class ValidationTest extends TestCase
23+ {
24+ public function testCreateCallableValid ()
25+ {
26+ $ validator = Validation::createCallable (new Email ());
27+ $ this ->assertEquals ('test@example.com ' , $ validator ('test@example.com ' ));
28+ }
29+
30+ public function testCreateCallableInvalid ()
31+ {
32+ $ validator = Validation::createCallable (new Email ());
33+ $ this ->expectException (ValidationFailedException::class);
34+ $ validator ('test ' );
35+ }
36+ }
Original file line number Diff line number Diff line change 1111
1212namespace Symfony \Component \Validator ;
1313
14+ use Symfony \Component \Validator \Exception \ValidationFailedException ;
1415use Symfony \Component \Validator \Validator \ValidatorInterface ;
1516
1617/**
2021 */
2122final class Validation
2223{
24+ /**
25+ * Creates a callable chain of constraints.
26+ */
27+ public static function createCallable (Constraint ...$ constraints ): callable
28+ {
29+ $ validator = self ::createValidator ();
30+
31+ return static function ($ value ) use ($ constraints , $ validator ) {
32+ $ violations = $ validator ->validate ($ value , $ constraints );
33+ if (0 !== $ violations ->count ()) {
34+ throw new ValidationFailedException ($ value , $ violations );
35+ }
36+
37+ return $ value ;
38+ };
39+ }
40+
2341 /**
2442 * Creates a new validator.
2543 *
You can’t perform that action at this time.
0 commit comments