You are here

public function UrlValidator::validate in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/symfony/validator/Constraints/UrlValidator.php \Symfony\Component\Validator\Constraints\UrlValidator::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/UrlValidator.php, line 43

Class

UrlValidator
@author Bernhard Schussek <bschussek@gmail.com>

Namespace

Symfony\Component\Validator\Constraints

Code

public function validate($value, Constraint $constraint) {
  if (!$constraint instanceof Url) {
    throw new UnexpectedTypeException($constraint, __NAMESPACE__ . '\\Url');
  }
  if (null === $value) {
    return;
  }
  if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
    throw new UnexpectedTypeException($value, 'string');
  }
  $value = (string) $value;
  if ('' === $value) {
    return;
  }
  $pattern = sprintf(static::PATTERN, implode('|', $constraint->protocols));
  if (!preg_match($pattern, $value)) {
    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();
    }
    return;
  }
  if ($constraint->checkDNS) {
    $host = parse_url($value, PHP_URL_HOST);
    if (!checkdnsrr($host, 'ANY')) {
      if ($this->context instanceof ExecutionContextInterface) {
        $this->context
          ->buildViolation($constraint->dnsMessage)
          ->setParameter('{{ value }}', $this
          ->formatValue($host))
          ->addViolation();
      }
      else {
        $this
          ->buildViolation($constraint->dnsMessage)
          ->setParameter('{{ value }}', $this
          ->formatValue($host))
          ->addViolation();
      }
    }
  }
}