2013-09-10 16:14:04 +01:00
|
|
|
<?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;
|
|
|
|
|
2014-03-17 15:44:19 +00:00
|
|
|
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
|
2013-09-10 16:14:04 +01:00
|
|
|
use Symfony\Component\Validator\Constraint;
|
|
|
|
use Symfony\Component\Validator\ConstraintValidator;
|
2018-09-21 12:50:11 +01:00
|
|
|
use Symfony\Component\Validator\Exception\LogicException;
|
2014-03-10 12:51:04 +00:00
|
|
|
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
|
2013-09-10 16:14:04 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Fabien Potencier <fabien@symfony.com>
|
|
|
|
* @author Bernhard Schussek <bschussek@symfony.com>
|
|
|
|
*/
|
|
|
|
class ExpressionValidator extends ConstraintValidator
|
|
|
|
{
|
|
|
|
private $expressionLanguage;
|
|
|
|
|
2016-01-16 03:28:49 +00:00
|
|
|
public function __construct($propertyAccessor = null, ExpressionLanguage $expressionLanguage = null)
|
2013-09-10 16:14:04 +01:00
|
|
|
{
|
2015-10-07 09:29:03 +01:00
|
|
|
$this->expressionLanguage = $expressionLanguage;
|
2013-09-10 16:14:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-04-16 09:03:46 +01:00
|
|
|
* {@inheritdoc}
|
2013-09-10 16:14:04 +01:00
|
|
|
*/
|
|
|
|
public function validate($value, Constraint $constraint)
|
|
|
|
{
|
2014-03-10 12:51:04 +00:00
|
|
|
if (!$constraint instanceof Expression) {
|
|
|
|
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Expression');
|
|
|
|
}
|
|
|
|
|
2017-12-15 00:11:15 +00:00
|
|
|
$variables = $constraint->values;
|
2016-01-16 03:28:49 +00:00
|
|
|
$variables['value'] = $value;
|
|
|
|
$variables['this'] = $this->context->getObject();
|
2013-09-10 16:14:04 +01:00
|
|
|
|
|
|
|
if (!$this->getExpressionLanguage()->evaluate($constraint->expression, $variables)) {
|
2016-01-14 00:18:51 +00:00
|
|
|
$this->context->buildViolation($constraint->message)
|
2017-12-04 12:17:10 +00:00
|
|
|
->setParameter('{{ value }}', $this->formatValue($value, self::OBJECT_TO_STRING))
|
2016-01-14 00:18:51 +00:00
|
|
|
->setCode(Expression::EXPRESSION_FAILED_ERROR)
|
|
|
|
->addViolation();
|
2013-09-10 16:14:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getExpressionLanguage()
|
|
|
|
{
|
|
|
|
if (null === $this->expressionLanguage) {
|
|
|
|
if (!class_exists('Symfony\Component\ExpressionLanguage\ExpressionLanguage')) {
|
2018-09-21 12:50:11 +01:00
|
|
|
throw new LogicException('Unable to use expressions as the Symfony ExpressionLanguage component is not installed.');
|
2013-09-10 16:14:04 +01:00
|
|
|
}
|
|
|
|
$this->expressionLanguage = new ExpressionLanguage();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->expressionLanguage;
|
|
|
|
}
|
|
|
|
}
|