You are here

public function ExpressionValidator::validate in Plug 7

Checks if the passed value is valid.

@api

Parameters

mixed $value The value that should be validated:

Constraint $constraint The constraint for the validation:

Overrides ConstraintValidatorInterface::validate

File

lib/Symfony/validator/Symfony/Component/Validator/Constraints/ExpressionValidator.php, line 57

Class

ExpressionValidator
@author Fabien Potencier <fabien@symfony.com> @author Bernhard Schussek <bschussek@symfony.com>

Namespace

Symfony\Component\Validator\Constraints

Code

public function validate($value, Constraint $constraint) {
  if (!$constraint instanceof Expression) {
    throw new UnexpectedTypeException($constraint, __NAMESPACE__ . '\\Expression');
  }
  $variables = array();

  // Symfony 2.5+
  if ($this->context instanceof ExecutionContextInterface) {
    $variables['value'] = $value;
    $variables['this'] = $this->context
      ->getObject();
  }
  elseif (null === $this->context
    ->getPropertyName()) {
    $variables['value'] = $value;
    $variables['this'] = $value;
  }
  else {
    $root = $this->context
      ->getRoot();
    $variables['value'] = $value;
    if (is_object($root)) {

      // Extract the object that the property belongs to from the object
      // graph
      $path = new PropertyPath($this->context
        ->getPropertyPath());
      $parentPath = $path
        ->getParent();
      $variables['this'] = $parentPath ? $this
        ->getPropertyAccessor()
        ->getValue($root, $parentPath) : $root;
    }
    else {
      $variables['this'] = null;
    }
  }
  if (!$this
    ->getExpressionLanguage()
    ->evaluate($constraint->expression, $variables)) {
    $this
      ->buildViolation($constraint->message)
      ->setParameter('{{ value }}', $this
      ->formatValue($value))
      ->addViolation();
  }
}