8000 [Validator] add Validation::createCallable() by janvernieuwe · Pull Request #31466 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Validator] add Validation::createCallable() #31466

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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 <jan.vernieuwe@phpro.be>
*/
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;
}
}
36 changes: 36 additions & 0 deletions src/Symfony/Component/Validator/Tests/ValidationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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 <jan.vernieuwe@phpro.be>
*/
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');
}
}
18 changes: 18 additions & 0 deletions src/Symfony/Component/Validator/Validation.php
5196
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Validator;

use Symfony\Component\Validator\Exception\ValidationFailedException;
use Symfony\Component\Validator\Validator\ValidatorInterface;

/**
Expand All @@ -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.
*
Expand Down
0