You are here

public function ExpressionValidator::validate in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/symfony/validator/Constraints/ExpressionValidator.php \Symfony\Component\Validator\Constraints\ExpressionValidator::validate()

Checks if the passed value is valid.

Parameters

mixed $value The value that should be validated:

Constraint $constraint The constraint for the validation:

Overrides ConstraintValidatorInterface::validate

File

vendor/symfony/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)) {
    if ($this->context instanceof ExecutionContextInterface) {
      $this->context
        ->buildViolation($constraint->message)
        ->setParameter('{{ value }}', $this
        ->formatValue($value))
        ->addViolation();
    }
    else {
      $this
        ->buildViolation($constraint->message)
        ->setParameter('{{ value }}', $this
        ->formatValue($value))
        ->addViolation();
    }
  }
}