diff --git a/src/Symfony/Component/Validator/CHANGELOG.md b/src/Symfony/Component/Validator/CHANGELOG.md index d874ed07de..f5ae1ee390 100644 --- a/src/Symfony/Component/Validator/CHANGELOG.md +++ b/src/Symfony/Component/Validator/CHANGELOG.md @@ -4,7 +4,8 @@ CHANGELOG 2.4.0 ----- - * added `minRatio`, `maxRatio`, `allowSquare`, `allowLandscape`, and `allowPortrait` to Image validator + * added a constraint the uses the expression language + * added `minRatio`, `maxRatio`, `allowSquare`, `allowLandscape`, and `allowPortrait` to Image validator 2.3.0 ----- diff --git a/src/Symfony/Component/Validator/Constraints/Condition.php b/src/Symfony/Component/Validator/Constraints/Condition.php new file mode 100644 index 0000000000..88c1a96279 --- /dev/null +++ b/src/Symfony/Component/Validator/Constraints/Condition.php @@ -0,0 +1,48 @@ + + * + * 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; + +/** + * @Annotation + * + * @author Fabien Potencier + */ +class Condition extends Constraint +{ + public $message = 'This value is not valid.'; + public $condition; + + /** + * {@inheritDoc} + */ + public function getDefaultOption() + { + return 'condition'; + } + + /** + * {@inheritDoc} + */ + public function getRequiredOptions() + { + return array('condition'); + } + /** + * {@inheritDoc} + */ + public function getTargets() + { + return self::CLASS_CONSTRAINT; + } +} diff --git a/src/Symfony/Component/Validator/Constraints/ConditionValidator.php b/src/Symfony/Component/Validator/Constraints/ConditionValidator.php new file mode 100644 index 0000000000..6f7f572a5c --- /dev/null +++ b/src/Symfony/Component/Validator/Constraints/ConditionValidator.php @@ -0,0 +1,50 @@ + + * + * 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\ExpressionLanguage\ExpressionLanguage; + +/** + * @author Fabien Potencier + */ +class ConditionValidator extends ConstraintValidator +{ + private $expressionLanguage; + + /** + * {@inheritDoc} + */ + public function validate($object, Constraint $constraint) + { + if (null === $object) { + return; + } + + if (!$this->getExpressionLanguage()->evaluate($constraint->condition, array('this' => $object))) { + $this->context->addViolation($constraint->message); + } + } + + private function getExpressionLanguage() + { + if (null === $this->expressionLanguage) { + if (!class_exists('Symfony\Component\ExpressionLanguage\ExpressionLanguage')) { + throw new RuntimeException('Unable to use expressions as the Symfony ExpressionLanguage component is not installed.'); + } + $this->expressionLanguage = new ExpressionLanguage(); + } + + return $this->expressionLanguage; + } +}