EmailValidator.php in Plug 7
File
lib/Symfony/validator/Symfony/Component/Validator/Constraints/EmailValidator.php
View source
<?php
namespace Symfony\Component\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\RuntimeException;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
class EmailValidator extends ConstraintValidator {
private $isStrict;
public function __construct($strict = false) {
$this->isStrict = $strict;
}
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);
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();
}
}
private function checkMX($host) {
return checkdnsrr($host, 'MX');
}
private function checkHost($host) {
return $this
->checkMX($host) || (checkdnsrr($host, 'A') || checkdnsrr($host, 'AAAA'));
}
}