Symfony\Component\Validator\Constraints\EmailValidator::validate PHP Method

validate() public method

public validate ( $value, Constraint $constraint )
$constraint Symfony\Component\Validator\Constraint
    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 ~1.2|~2.0');
            }
            $strictValidator = new \Egulias\EmailValidator\EmailValidator();
            if (interface_exists(EmailValidation::class) && !$strictValidator->isValid($value, new NoRFCWarningsValidation())) {
                $this->context->buildViolation($constraint->message)->setParameter('{{ value }}', $this->formatValue($value))->setCode(Email::INVALID_FORMAT_ERROR)->addViolation();
                return;
            } elseif (!interface_exists(EmailValidation::class) && !$strictValidator->isValid($value, false, true)) {
                $this->context->buildViolation($constraint->message)->setParameter('{{ value }}', $this->formatValue($value))->setCode(Email::INVALID_FORMAT_ERROR)->addViolation();
                return;
            }
        } elseif (!preg_match('/^.+\\@\\S+\\.\\S+$/', $value)) {
            $this->context->buildViolation($constraint->message)->setParameter('{{ value }}', $this->formatValue($value))->setCode(Email::INVALID_FORMAT_ERROR)->addViolation();
            return;
        }
        $host = substr($value, strrpos($value, '@') + 1);
        // Check for host DNS resource records
        if ($constraint->checkMX) {
            if (!$this->checkMX($host)) {
                $this->context->buildViolation($constraint->message)->setParameter('{{ value }}', $this->formatValue($value))->setCode(Email::MX_CHECK_FAILED_ERROR)->addViolation();
            }
            return;
        }
        if ($constraint->checkHost && !$this->checkHost($host)) {
            $this->context->buildViolation($constraint->message)->setParameter('{{ value }}', $this->formatValue($value))->setCode(Email::HOST_CHECK_FAILED_ERROR)->addViolation();
        }
    }

Usage Example

Beispiel #1
0
 /**
  * @dataProvider validateDataProvider
  * @param mixed $data
  * @param boolean $correct
  */
 public function testValidate($data, $correct)
 {
     if (!$correct) {
         $this->context->expects($this->once())->method('addViolation')->with($this->constraint->message);
     } else {
         $this->context->expects($this->never())->method('addViolation');
     }
     $this->validator->validate($data, $this->constraint);
 }
All Usage Examples Of Symfony\Component\Validator\Constraints\EmailValidator::validate