EmailValidator.php in Zircon Profile 8
File
vendor/symfony/validator/Constraints/EmailValidator.php
View source
<?php
namespace Symfony\Component\Validator\Constraints;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
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)) {
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);
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();
}
}
}
private function checkMX($host) {
return checkdnsrr($host, 'MX');
}
private function checkHost($host) {
return $this
->checkMX($host) || (checkdnsrr($host, 'A') || checkdnsrr($host, 'AAAA'));
}
}