コード例 #1
0
 /**
  * Process validation
  *
  * @param   string  $value          The local part of an email address to validate
  * @param   bool    $send_errors    Does the function must throw exceptions on validation failures ?
  * @return  bool    TRUE if `$value` pass the Email validation
  * @throws  \Exception for each invalid part if `$send_errors` is true
  */
 public function validate($value, $send_errors = false)
 {
     // check for length compliance (max 255 chars.)
     $lengthValidator = new StringLengthValidator(0, 255);
     try {
         if (false === ($length_valid = $lengthValidator->validate($value, false))) {
             if (true === $send_errors) {
                 throw new \Exception(sprintf('The hostname [%s] must not be up to 255 characters long!', $value));
             }
             return false;
         }
     } catch (\Exception $e) {
         throw $e;
     }
     // check for double-dots
     if (false !== strpos($value, '..')) {
         if (true === $send_errors) {
             throw new \Exception(sprintf('The hostname [%s] must not contain double-dots!', $value));
         }
         return false;
     }
     // check for each labels validity
     // => max length of 63 chars.
     $labelLengthValidator = new StringLengthValidator(1, 63);
     // => AlphaDigit compliance for a single char.
     $singlechar_labelMaskValidator = new StringMaskValidator('^' . $this->getMask('AlphaDigit') . '$');
     // => $this->must_pass compliance
     $labelMaskValidator = new StringMaskValidator('^' . $this->getMask($this->must_pass) . '$', $this->must_pass);
     try {
         $labels = explode('.', $value);
         foreach ($labels as $_label) {
             if (strlen($_label) && false === ($label_length_valid = $labelLengthValidator->validate($_label, false))) {
                 if (true === $send_errors) {
                     throw new \Exception(sprintf('The label [%s] in the hostname [%s] must not be up to 63 characters long!', $_label, $value));
                 }
                 return false;
             }
             if (strlen($_label) > 1 && false === ($label_mask_valid = $labelMaskValidator->validate($_label, $send_errors))) {
                 return false;
             } elseif (strlen($_label) == 1 && false === ($label_mask_valid = $singlechar_labelMaskValidator->validate($_label, false))) {
                 if (true === $send_errors) {
                     throw new \Exception(sprintf('The single character label [%s] in the hostname [%s] must be an alpha-numeric string!', $_label, $value));
                 }
                 return false;
             }
         }
     } catch (\Exception $e) {
         throw $e;
     }
     return true;
 }
コード例 #2
0
 /**
  * Process local part validation
  *
  * @param   string  $value          The local part of an email address to validate
  * @param   bool    $send_errors    Does the function must throw exceptions on validation failures ?
  * @return  bool    TRUE if `$value` pass the Email validation
  * @throws  \Exception for each invalid part if `$send_errors` is true
  */
 public function validateLocalPart($value, $send_errors = false)
 {
     // check for local part length compliance (max 64 chars.)
     $lengthValidator = new StringLengthValidator(0, 64);
     try {
         if (false === ($local_length_valid = $lengthValidator->validate($value, false))) {
             if (true === $send_errors) {
                 throw new \Exception(sprintf('The local part of the email address [%s] must not be up to 64 characters!', $value));
             }
             return false;
         }
     } catch (\Exception $e) {
         throw $e;
     }
     // check for dots in local part
     // => not the first character
     if (substr($value, 0, 1) == '.') {
         if (true === $send_errors) {
             throw new \Exception(sprintf('The local part of the email address [%s] must not begin with a dot!', $value));
         }
         return false;
     }
     // => not the last character
     if (substr($value, -1, 1) == '.') {
         if (true === $send_errors) {
             throw new \Exception(sprintf('The local part of the email address [%s] must not end with a dot!', $value));
         }
         return false;
     }
     // => no double-dots
     if (false !== strpos($value, '..')) {
         if (true === $send_errors) {
             throw new \Exception(sprintf('The local part of the email address [%s] must not contain double-dots!', $value));
         }
         return false;
     }
     // the local part must match the standards as wanted
     $last = 0;
     foreach (self::$standards_list as $standard) {
         if (1 === $last) {
             continue;
         }
         if ($standard == $this->must_pass) {
             $last = 1;
         }
         $maskValidator = new StringMaskValidator('^' . $this->getMask($standard) . '$', $standard);
         try {
             if (false === ($local_part_mask_valid = $maskValidator->validate($value, $send_errors))) {
                 return false;
             }
         } catch (\Exception $e) {
             throw $e;
         }
     }
     return true;
 }