Skip to content

Commit 70f111a

Browse files
janvernieuwenicolas-grekas
authored andcommitted
[Validator] add Validation::createCallable()
1 parent dd4db61 commit 70f111a

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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 <[email protected]>
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+
}

Tests/ValidationTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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 <[email protected]>
21+
*/
22+
class ValidationTest extends TestCase
23+
{
24+
public function testCreateCallableValid()
25+
{
26+
$validator = Validation::createCallable(new Email());
27+
$this->assertEquals('[email protected]', $validator('[email protected]'));
28+
}
29+
30+
public function testCreateCallableInvalid()
31+
{
32+
$validator = Validation::createCallable(new Email());
33+
$this->expectException(ValidationFailedException::class);
34+
$validator('test');
35+
}
36+
}

Validation.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Validator;
1313

14+
use Symfony\Component\Validator\Exception\ValidationFailedException;
1415
use Symfony\Component\Validator\Validator\ValidatorInterface;
1516

1617
/**
@@ -20,6 +21,23 @@
2021
*/
2122
final 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
*

0 commit comments

Comments
 (0)