You are here

public function EmailValidator::validate in Zircon Profile 8.0

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

Class

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

Namespace

Symfony\Component\Validator\Constraints

Code

public function validate($value, Constraint $constraint) {
  if (!$constraint instanceof Email) {
    throw new UnexpectedTypeException($constraint, __NAMESPACE__ . '\\Email');
  }
  if (null === $value || '' === $value) {
    return;
  }
  if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
    throw new UnexpectedTypeException($value, 'string');
  }
  $value = (string) $value;
  if (null === $constraint->strict) {
    $constraint->strict = $this->isStrict;
  }
  if ($constraint->strict) {
    if (!class_exists('\\Egulias\\EmailValidator\\EmailValidator')) {
      throw new RuntimeException('Strict email validation requires egulias/email-validator');
    }
    $strictValidator = new \Egulias\EmailValidator\EmailValidator();
    if (!$strictValidator
      ->isValid($value, false, true)) {
      if ($this->context instanceof ExecutionContextInterface) {
        $this->context
          ->buildViolation($constraint->message)
          ->setParameter('{{ value }}', $this
          ->formatValue($value))
          ->setCode(Email::INVALID_FORMAT_ERROR)
          ->addViolation();
      }
      else {
        $this
          ->buildViolation($constraint->message)
          ->setParameter('{{ value }}', $this
          ->formatValue($value))
          ->setCode(Email::INVALID_FORMAT_ERROR)
          ->addViolation();
      }
      return;
    }
  }
  elseif (!preg_match('/^.+\\@\\S+\\.\\S+$/', $value)) {
    if ($this->context instanceof ExecutionContextInterface) {
      $this->context
        ->buildViolation($constraint->message)
        ->setParameter('{{ value }}', $this
        ->formatValue($value))
        ->setCode(Email::INVALID_FORMAT_ERROR)
        ->addViolation();
    }
    else {
      $this
        ->buildViolation($constraint->message)
        ->setParameter('{{ value }}', $this
        ->formatValue($value))
        ->setCode(Email::INVALID_FORMAT_ERROR)
        ->addViolation();
    }
    return;
  }
  $host = substr($value, strpos($value, '@') + 1);

  // Check for host DNS resource records
  if ($constraint->checkMX) {
    if (!$this
      ->checkMX($host)) {
      if ($this->context instanceof ExecutionContextInterface) {
        $this->context
          ->buildViolation($constraint->message)
          ->setParameter('{{ value }}', $this
          ->formatValue($value))
          ->setCode(Email::MX_CHECK_FAILED_ERROR)
          ->addViolation();
      }
      else {
        $this
          ->buildViolation($constraint->message)
          ->setParameter('{{ value }}', $this
          ->formatValue($value))
          ->setCode(Email::MX_CHECK_FAILED_ERROR)
          ->addViolation();
      }
    }
    return;
  }
  if ($constraint->checkHost && !$this
    ->checkHost($host)) {
    if ($this->context instanceof ExecutionContextInterface) {
      $this->context
        ->buildViolation($constraint->message)
        ->setParameter('{{ value }}', $this
        ->formatValue($value))
        ->setCode(Email::HOST_CHECK_FAILED_ERROR)
        ->addViolation();
    }
    else {
      $this
        ->buildViolation($constraint->message)
        ->setParameter('{{ value }}', $this
        ->formatValue($value))
        ->setCode(Email::HOST_CHECK_FAILED_ERROR)
        ->addViolation();
    }
  }
}