/**
  * {@inheritdoc}
  */
 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();
         }
     }
 }
Ejemplo n.º 2
0
 /**
  * Process
  *
  * @param mixed $value
  * @param array $options
  *
  * @return string|null
  */
 public function process($value, array $options = array())
 {
     if ($value === null) {
         return;
     }
     if (is_object($value) || is_resource($value) || is_array($value)) {
         $this->throwException($options, 'error');
     }
     $options = array_replace(array('check_dns' => true, 'strict' => true), $options);
     $validators = array();
     if ($options['strict']) {
         $validators[] = new \Egulias\EmailValidator\Validation\RFCValidation();
     }
     if ($options['check_dns']) {
         $validators[] = new \Egulias\EmailValidator\Validation\DNSCheckValidation();
     }
     $validations = new \Egulias\EmailValidator\Validation\MultipleValidationWithAnd($validators);
     $validator = new \Egulias\EmailValidator\EmailValidator();
     if (!$validator->isValid($value, $validations)) {
         $this->throwException($options, 'error');
     }
     return (string) $value;
 }
Ejemplo n.º 3
0
     $message = "Veuillez saisir un email par ligne.";
 } elseif ($count > MAX_EMAILS && MAX_EMAILS > 0) {
     $message = "La liste peut contenir au maximum " . MAX_EMAILS . " emails.";
 } elseif ($can === false) {
     $message = "Veuillez attendre " . SPAM_TIME . " secondes entre chaque essai.";
 } else {
     $process = true;
 }
 // Nettoie sommairement le tableau... mais vraiment sommairement
 $raw = array_map(function ($e) {
     return trim($e);
 }, $raw);
 // Zou !
 if ($process === true) {
     // Le vrai gars ici c'est lui :)
     $validator = new Egulias\EmailValidator\EmailValidator();
     // Brrbrbrbrrrrbrr (bruit de moteur pour les incultes !)
     foreach ($raw as $email) {
         // La ligne est vide :(
         if (empty($email)) {
             continue;
         }
         // L'email est doublon... Ca peut devenir vilain sur les grosses listes.
         if (in_array($email, $cleaned, true)) {
             continue;
         }
         if ($validator->isValid($email, CHECK_DNS, true)) {
             $cleaned[] = $email;
         } else {
             // On stocke les erreurs mais elles ne servent nulle part...
             $errors[$email] = ['error' => $validator->getError(), 'warning' => $validator->getWarnings()];