Example #1
0
 protected function execute($value, EntityInterface $entity = null)
 {
     if (!is_scalar($value) || !is_string($value)) {
         $this->throwError('invalid_type', [], IncidentInterface::CRITICAL);
         return false;
     }
     $null_value = $this->getOption(AttributeInterface::OPTION_NULL_VALUE, '');
     if ($value === $null_value) {
         $this->setSanitizedValue($null_value);
         return true;
     }
     $warnings = [];
     $reason = null;
     try {
         $parser = new EmailParser(new EmailLexer());
         $parser->parse($value);
         $warnings = $parser->getWarnings();
     } catch (InvalidArgumentException $parse_error) {
         $error_const = $parse_error->getMessage();
         $validator_reflection = new ReflectionClass(new EmailValidator());
         if ($validator_reflection->hasConstant($error_const)) {
             $reason = $error_const;
         }
         $this->throwError('invalid_format', ['reason' => $reason], IncidentInterface::ERROR);
         return false;
     }
     if (count($warnings) > 0) {
         // @todo map warnings to errors and raise critical
         // @todo raise critical as soon as max number of warnings reached
         // @todo non-mapped warnings are raised as notice
     }
     $this->setSanitizedValue($value);
     return true;
 }
Example #2
0
 protected function execute($values, EntityInterface $entity = null)
 {
     $cast_to_array = $this->getOption(self::OPTION_CAST_TO_ARRAY, true);
     if (!$cast_to_array && !is_array($values)) {
         $this->throwError('not_an_array');
         return false;
     }
     $emails = [];
     if (is_array($values)) {
         $emails = $values;
     } else {
         $emails = [$values => ''];
     }
     if (!empty($emails) && !$this->isAssoc($emails)) {
         $this->throwError('non_assoc_array', []);
         return false;
     }
     $count = count($emails);
     // minimum number of emails
     if ($this->hasOption(self::OPTION_MIN_COUNT)) {
         $min_count = $this->getOption(self::OPTION_MIN_COUNT, 0);
         if ($count < (int) $min_count) {
             $this->throwError('min_count', ['count' => $count, 'min_count' => $min_count]);
             return false;
         }
     }
     // maximum number of emails
     if ($this->hasOption(self::OPTION_MAX_COUNT)) {
         $max_count = $this->getOption(self::OPTION_MAX_COUNT, 0);
         if ($count > (int) $max_count) {
             $this->throwError('max_count', ['count' => $count, 'max_count' => $max_count]);
             return false;
         }
     }
     $allowed_labels = [];
     if ($this->hasOption(self::OPTION_ALLOWED_EMAIL_LABELS)) {
         $allowed_labels = $this->getAllowedLabels();
     }
     $allowed_emails = [];
     if ($this->hasOption(self::OPTION_ALLOWED_EMAILS)) {
         $allowed_emails = $this->getAllowedEmails();
     }
     $allowed_pairs = [];
     if ($this->hasOption(self::OPTION_ALLOWED_EMAIL_PAIRS)) {
         $allowed_pairs = $this->getAllowedPairs();
     }
     $sanitized = [];
     $parser = new EmailParser(new EmailLexer());
     foreach ($emails as $email => $label) {
         $email = trim($email);
         if (empty($email)) {
             $this->throwError('empty_email', ['email' => $email, 'label' => $label]);
             return false;
         }
         // check for valid email address
         try {
             $parser->parse($email);
             $warnings = $parser->getWarnings();
         } catch (InvalidArgumentException $parse_error) {
             $error_const = $parse_error->getMessage();
             $validator_reflection = new ReflectionClass(new EmailValidator());
             if ($validator_reflection->hasConstant($error_const)) {
                 $reason = $error_const;
             }
             $this->throwError('invalid_email', ['reason' => $reason, 'email' => $email, 'label' => $label]);
             return false;
         }
         // check for allowed email address formats
         if ($this->hasOption(self::OPTION_ALLOWED_EMAILS)) {
             if (!in_array($email, $allowed_emails, true)) {
                 $this->throwError(self::OPTION_ALLOWED_EMAILS, [self::OPTION_ALLOWED_EMAILS => $allowed_emails, 'email' => $email]);
                 return false;
             }
         }
         if (!is_string($label)) {
             $this->throwError('non_string_label', ['email' => $email]);
             return false;
         }
         // check minimum label string length
         if ($this->hasOption(self::OPTION_MIN_EMAIL_LABEL_LENGTH)) {
             $min = filter_var($this->getOption(self::OPTION_MIN_EMAIL_LABEL_LENGTH), FILTER_VALIDATE_INT);
             if ($min === false) {
                 throw new InvalidConfigException('Minimum label length is not interpretable as integer.');
             }
             if (mb_strlen($label) < $min) {
                 $this->throwError(self::OPTION_MIN_EMAIL_LABEL_LENGTH, [self::OPTION_MIN_EMAIL_LABEL_LENGTH => $min, 'email' => $email, 'label' => $label]);
                 return false;
             }
         }
         // check maximum label string length
         if ($this->hasOption(self::OPTION_MAX_EMAIL_LABEL_LENGTH)) {
             $max = filter_var($this->getOption(self::OPTION_MAX_EMAIL_LABEL_LENGTH), FILTER_VALIDATE_INT);
             if ($max === false) {
                 throw new InvalidConfigException('Maximum label length is not interpretable as integer.');
             }
             if (mb_strlen($label) > $max) {
                 $this->throwError(self::OPTION_MAX_EMAIL_LABEL_LENGTH, [self::OPTION_MAX_EMAIL_LABEL_LENGTH => $max, 'email' => $email, 'label' => $label]);
                 return false;
             }
         }
         // check for allowed labels
         if ($this->hasOption(self::OPTION_ALLOWED_EMAIL_LABELS)) {
             if (!in_array($label, $allowed_labels, true)) {
                 $this->throwError(self::OPTION_ALLOWED_EMAIL_LABELS, [self::OPTION_ALLOWED_EMAIL_LABELS => $allowed_labels, 'email' => $email, 'label' => $label]);
                 return false;
             }
         }
         // check for allowed email => label pairs
         if ($this->hasOption(self::OPTION_ALLOWED_EMAIL_PAIRS)) {
             if (!(array_key_exists($email, $allowed_pairs) && $allowed_pairs[$email] === $label)) {
                 $this->throwError(self::OPTION_ALLOWED_EMAIL_PAIRS, [self::OPTION_ALLOWED_EMAIL_PAIRS => $allowed_pairs, 'email' => $email, 'label' => $label]);
                 return false;
             }
         }
         $sanitized[$email] = $label;
     }
     $this->setSanitizedValue($sanitized);
     return true;
 }