-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Validator] Constraint validators now use the 2.5 API #11485
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
webmozart
merged 5 commits into
symfony:2.5
from
webmozart:fix-validator-legacy-dependency
Aug 4, 2014
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8e461af
[Validator] Constraint validators now use the 2.5 API. For incompatib…
webmozart 7504448
[Validator] Added extensive test coverage for the constraint validato…
webmozart 870a41a
[FrameworkBundle] Made ConstraintValidatorFactory aware of the legacy…
webmozart 3bd6d80
[Validator] CS fixes
webmozart 295e5bb
[Validator] Fixed failing tests
webmozart 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
91 changes: 91 additions & 0 deletions
91
src/Symfony/Bundle/FrameworkBundle/Validator/LegacyConstraintValidatorFactory.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,91 @@ | ||
<?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\Bundle\FrameworkBundle\Validator; | ||
|
||
use Symfony\Component\DependencyInjection\ContainerInterface; | ||
use Symfony\Component\Validator\Constraint; | ||
use Symfony\Component\Validator\ConstraintValidatorFactoryInterface; | ||
use Symfony\Component\Validator\ConstraintValidatorInterface; | ||
use Symfony\Component\Validator\Exception\UnexpectedTypeException; | ||
|
||
/** | ||
* Like {@link ConstraintValidatorFactory}, but aware of services compatible | ||
* with the 2.4 API. | ||
* | ||
* @author Bernhard Schussek <bschussek@gmail.com> | ||
* @author Kris Wallsmith <kris@symfony.com> | ||
* | ||
* @see ConstraintValidatorFactory | ||
*/ | ||
class LegacyConstraintValidatorFactory implements ConstraintValidatorFactoryInterface | ||
{ | ||
const BASE_NAMESPACE = 'Symfony\\Component\\Validator\\Constraints'; | ||
|
||
protected $container; | ||
protected $validators; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param ContainerInterface $container The service container | ||
* @param array $validators An array of validators | ||
*/ | ||
public function __construct(ContainerInterface $container, array $validators = array()) | ||
{ | ||
$this->container = $container; | ||
$this->validators = $validators; | ||
} | ||
|
||
/** | ||
* Returns the validator for the supplied constraint. | ||
* | ||
* @param Constraint $constraint A constraint | ||
* | ||
* @return ConstraintValidatorInterface A validator for the supplied constraint | ||
* | ||
* @throws UnexpectedTypeException When the validator is not an instance of ConstraintValidatorInterface | ||
*/ | ||
public function getInstance(Constraint $constraint) | ||
{ | ||
$name = $constraint->validatedBy(); | ||
|
||
if (!isset($this->validators[$name])) { | ||
switch (get_class($constraint)) { | ||
case self::BASE_NAMESPACE.'\\All': | ||
$name = self::BASE_NAMESPACE.'\\LegacyAllValidator'; | ||
break; | ||
case self::BASE_NAMESPACE.'\\Choice': | ||
$name = self::BASE_NAMESPACE.'\\LegacyChoiceValidator'; | ||
break; | ||
case self::BASE_NAMESPACE.'\\Collection': | ||
$name = self::BASE_NAMESPACE.'\\LegacyCollectionValidator'; | ||
break; | ||
case self::BASE_NAMESPACE.'\\Count': | ||
$name = self::BASE_NAMESPACE.'\\LegacyCountValidator'; | ||
break; | ||
case self::BASE_NAMESPACE.'\\Length': | ||
$name = self::BASE_NAMESPACE.'\\LegacyLengthValidator'; | ||
break; | ||
} | ||
|
||
$this->validators[$name] = new $name(); | ||
} elseif (is_string($this->validators[$name])) { | ||
$this->validators[$name] = $this->container->get($this->validators[$name]); | ||
} | ||
|
||
if (!$this->validators[$name] instanceof ConstraintValidatorInterface) { | ||
throw new UnexpectedTypeException($this->validators[$name], 'Symfony\Component\Validator\ConstraintValidatorInterface'); | ||
} | ||
|
||
return $this->validators[$name]; | ||
} | ||
} |
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
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
49 changes: 49 additions & 0 deletions
49
src/Symfony/Component/Validator/Constraints/LegacyAllValidator.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,49 @@ | ||
<?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 Bernhard Schussek <bschussek@gmail.com> | ||
* | ||
* @deprecated Deprecated since version 2.5.3, to be removed in 3.0. | ||
*/ | ||
class LegacyAllValidator extends ConstraintValidator | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function validate($value, Constraint $constraint) | ||
{ | ||
if (!$constraint instanceof All) { | ||
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\All'); | ||
} | ||
|
||
if (null === $value) { | ||
return; | ||
} | ||
|
||
if (!is_array($value) && !$value instanceof \Traversable) { | ||
throw new UnexpectedTypeException($value, 'array or Traversable'); | ||
} | ||
|
||
$context = $this->context; | ||
$group = $context->getGroup(); | ||
|
||
foreach ($value as $key => $element) { | ||
$context->validateValue($element, $constraint->constraints, '['.$key.']', $group); | ||
} | ||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
src/Symfony/Component/Validator/Constraints/LegacyChoiceValidator.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,87 @@ | ||
<?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\ConstraintDefinitionException; | ||
use Symfony\Component\Validator\Exception\UnexpectedTypeException; | ||
|
||
/** | ||
* ChoiceValidator validates that the value is one of the expected values. | ||
* | ||
* @author Fabien Potencier <fabien@symfony.com> | ||
* @author Florian Eckerstorfer <florian@eckerstorfer.org> | ||
* @author Bernhard Schussek <bschussek@gmail.com> | ||
* | ||
* @deprecated Deprecated since version 2.5.3, to be removed in 3.0. | ||
*/ | ||
class LegacyChoiceValidator extends ConstraintValidator | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function validate($value, Constraint $constraint) | ||
{ | ||
if (!$constraint instanceof Choice) { | ||
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Choice'); | ||
} | ||
|
||
if (!$constraint->choices && !$constraint->callback) { | ||
throw new ConstraintDefinitionException('Either "choices" or "callback" must be specified on constraint Choice'); | ||
} | ||
|
||
if (null === $value) { | ||
return; | ||
} | ||
|
||
if ($constraint->multiple && !is_array($value)) { | ||
throw new UnexpectedTypeException($value, 'array'); | ||
} | ||
|
||
if ($constraint->callback) { | ||
if (is_callable(array($this->context->getClassName(), $constraint->callback))) { | ||
$choices = call_user_func(array($this->context->getClassName(), $constraint->callback)); | ||
} elseif (is_callable($constraint->callback)) { | ||
$choices = call_user_func($constraint->callback); | ||
} else { | ||
throw new ConstraintDefinitionException('The Choice constraint expects a valid callback'); | ||
} | ||
} else { | ||
$choices = $constraint->choices; | ||
} | ||
|
||
if ($constraint->multiple) { | ||
foreach ($value as $_value) { | ||
if (!in_array($_value, $choices, $constraint->strict)) { | ||
$this->context->addViolation($constraint->multipleMessage, array('{{ value }}' => $_value)); | ||
} | ||
} | ||
|
||
$count = count($value); | ||
|
||
if ($constraint->min !== null && $count < $constraint->min) { | ||
$this->context->addViolation($constraint->minMessage, array('{{ limit }}' => $constraint->min), $value, (int) $constraint->min); | ||
|
||
return; | ||
} | ||
|
||
if ($constraint->max !== null && $count > $constraint->max) { | ||
$this->context->addViolation($constraint->maxMessage, array('{{ limit }}' => $constraint->max), $value, (int) $constraint->max); | ||
|
||
return; | ||
} | ||
} elseif (!in_array($value, $choices, $constraint->strict)) { | ||
$this->context->addViolation($constraint->message, array('{{ value }}' => $value)); | ||
} | ||
} | ||
} |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know this is correct at the moment. But it looks kinda strange from an API point of view.
Would it not make more sense if
$context->getValidator()
automatically returns aContextualValidatorInterface
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, because you don't always want to validate in the same context. For example, imagine validating some constraints and, depending on the result, adding one single combined violation. Then you need to validate in a different context.
This was in fact one of the motivations for the API change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See #9888 for example