-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Validator] Allow to define a reusable set of constraints #34334
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?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\Constraints; | ||
|
||
use Symfony\Component\Validator\Constraint; | ||
use Symfony\Component\Validator\Exception\ConstraintDefinitionException; | ||
|
||
/** | ||
* Extend this class to create a reusable set of constraints. | ||
* | ||
* @author Maxime Steinhausser <maxime.steinhausser@gmail.com> | ||
*/ | ||
abstract class Compound extends Composite | ||
{ | ||
/** @var Constraint[] */ | ||
public $constraints = []; | ||
|
||
public function __construct($options = null) | ||
{ | ||
if (isset($options[$this->getCompositeOption()])) { | ||
throw new ConstraintDefinitionException(sprintf('You can\'t redefine the "%s" option. Use the %s::getConstraints() method instead.', $this->getCompositeOption(), __CLASS__)); | ||
} | ||
|
||
$this->constraints = $this->getConstraints($this->normalizeOptions($options)); | ||
|
||
parent::__construct($options); | ||
} | ||
|
||
final protected function getCompositeOption() | ||
{ | ||
return 'constraints'; | ||
} | ||
|
||
final public function validatedBy() | ||
{ | ||
return CompoundValidator::class; | ||
} | ||
|
||
/** | ||
* @return Constraint[] | ||
*/ | ||
abstract protected function getConstraints(array $options): array; | ||
} |
35 changes: 35 additions & 0 deletions
35
src/Symfony/Component/Validator/Constraints/CompoundValidator.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?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\Constraints; | ||
|
||
use Symfony\Component\Validator\Constraint; | ||
use Symfony\Component\Validator\ConstraintValidator; | ||
use Symfony\Component\Validator\Exception\UnexpectedTypeException; | ||
|
||
/** | ||
* @author Maxime Steinhausser <maxime.steinhausser@gmail.com> | ||
*/ | ||
class CompoundValidator extends ConstraintValidator | ||
{ | ||
public function validate($value, Constraint $constraint) | ||
{ | ||
if (!$constraint instanceof Compound) { | ||
throw new UnexpectedTypeException($constraint, Compound::class); | ||
} | ||
|
||
$context = $this->context; | ||
|
||
$validator = $context->getValidator()->inContext($context); | ||
|
||
$validator->validate($value, $constraint->constraints); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
src/Symfony/Component/Validator/Tests/Constraints/CompoundTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?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\Constraints; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Validator\Constraints\Compound; | ||
use Symfony\Component\Validator\Constraints\Length; | ||
use Symfony\Component\Validator\Constraints\NotBlank; | ||
use Symfony\Component\Validator\Exception\ConstraintDefinitionException; | ||
|
||
class CompoundTest extends TestCase | ||
{ | ||
public function testItCannotRedefineConstraintsOption() | ||
{ | ||
$this->expectException(ConstraintDefinitionException::class); | ||
$this->expectExceptionMessage('You can\'t redefine the "constraints" option. Use the Symfony\Component\Validator\Constraints\Compound::getConstraints() method instead.'); | ||
new EmptyCompound(['constraints' => [new NotBlank()]]); | ||
} | ||
|
||
public function testCanDependOnNormalizedOptions() | ||
{ | ||
$constraint = new ForwardingOptionCompound($min = 3); | ||
|
||
$this->assertSame($min, $constraint->constraints[0]->min); | ||
} | ||
} | ||
|
||
class EmptyCompound extends Compound | ||
{ | ||
protected function getConstraints(array $options): array | ||
{ | ||
return []; | ||
} | ||
} | ||
|
||
class ForwardingOptionCompound extends Compound | ||
{ | ||
public $min; | ||
|
||
public function getDefaultOption() | ||
{ | ||
return 'min'; | ||
} | ||
|
||
protected function getConstraints(array $options): array | ||
{ | ||
return [ | ||
new Length(['min' => $options['min'] ?? null]), | ||
]; | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
src/Symfony/Component/Validator/Tests/Constraints/CompoundValidatorTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?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\Constraints; | ||
|
||
use Symfony\Component\Validator\Constraints\Compound; | ||
use Symfony\Component\Validator\Constraints\CompoundValidator; | ||
use Symfony\Component\Validator\Constraints\Length; | ||
use Symfony\Component\Validator\Constraints\NotBlank; | ||
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase; | ||
|
||
class CompoundValidatorTest extends ConstraintValidatorTestCase | ||
{ | ||
protected function createValidator() | ||
{ | ||
return new CompoundValidator(); | ||
} | ||
|
||
public function testValidValue() | ||
{ | ||
$this->validator->validate('foo', new DummyCompoundConstraint()); | ||
|
||
$this->assertNoViolation(); | ||
} | ||
|
||
public function testValidateWithConstraints() | ||
{ | ||
$value = 'foo'; | ||
$constraint = new DummyCompoundConstraint(); | ||
|
||
$this->expectValidateValue(0, $value, $constraint->constraints); | ||
|
||
$this->validator->validate($value, $constraint); | ||
|
||
$this->assertNoViolation(); | ||
} | ||
} | ||
|
||
class DummyCompoundConstraint extends Compound | ||
{ | ||
protected function getConstraints(array $options): array | ||
{ | ||
return [ | ||
new NotBlank(), | ||
new Length(['max' => 3]), | ||
]; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.