public function EmailValidator::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/ EmailValidator.php, line 39
Class
- EmailValidator
- @author Bernhard Schussek <bschussek@gmail.com>
Namespace
Symfony\Component\Validator\ConstraintsCode
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)) {
$this
->buildViolation($constraint->message)
->setParameter('{{ value }}', $this
->formatValue($value))
->setCode(Email::INVALID_FORMAT_ERROR)
->addViolation();
return;
}
}
elseif (!preg_match('/^.+\\@\\S+\\.\\S+$/', $value)) {
$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)) {
$this
->buildViolation($constraint->message)
->setParameter('{{ value }}', $this
->formatValue($value))
->setCode(Email::MX_CHECK_FAILED_ERROR)
->addViolation();
}
return;
}
if ($constraint->checkHost && !$this
->checkHost($host)) {
$this
->buildViolation($constraint->message)
->setParameter('{{ value }}', $this
->formatValue($value))
->setCode(Email::HOST_CHECK_FAILED_ERROR)
->addViolation();
}
}