class EmailValidator
Same name in this branch
- 11.1.x vendor/egulias/email-validator/src/EmailValidator.php \Egulias\EmailValidator\EmailValidator
- 11.1.x core/lib/Drupal/Component/Utility/EmailValidator.php \Drupal\Component\Utility\EmailValidator
@author Bernhard Schussek <bschussek@gmail.com>
Hierarchy
- class \Symfony\Component\Validator\ConstraintValidator implements \Symfony\Component\Validator\ConstraintValidatorInterface
- class \Symfony\Component\Validator\Constraints\EmailValidator extends \Symfony\Component\Validator\ConstraintValidator
Expanded class hierarchy of EmailValidator
1 file declares its use of EmailValidator
- EmailConstraint.php in core/
lib/ Drupal/ Core/ Validation/ Plugin/ Validation/ Constraint/ EmailConstraint.php
File
-
vendor/
symfony/ validator/ Constraints/ EmailValidator.php, line 27
Namespace
Symfony\Component\Validator\ConstraintsView source
class EmailValidator extends ConstraintValidator {
private const PATTERN_HTML5_ALLOW_NO_TLD = '/^[a-zA-Z0-9.!#$%&\'*+\\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/D';
private const PATTERN_HTML5 = '/^[a-zA-Z0-9.!#$%&\'*+\\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)+$/D';
private const EMAIL_PATTERNS = [
Email::VALIDATION_MODE_HTML5 => self::PATTERN_HTML5,
Email::VALIDATION_MODE_HTML5_ALLOW_NO_TLD => self::PATTERN_HTML5_ALLOW_NO_TLD,
];
private string $defaultMode;
public function __construct(string $defaultMode = Email::VALIDATION_MODE_HTML5) {
if (!\in_array($defaultMode, Email::VALIDATION_MODES, true)) {
throw new InvalidArgumentException('The "defaultMode" parameter value is not valid.');
}
$this->defaultMode = $defaultMode;
}
public function validate(mixed $value, Constraint $constraint) : void {
if (!$constraint instanceof Email) {
throw new UnexpectedTypeException($constraint, Email::class);
}
if (null === $value || '' === $value) {
return;
}
if (!\is_scalar($value) && !$value instanceof \Stringable) {
throw new UnexpectedValueException($value, 'string');
}
$value = (string) $value;
if ('' === $value) {
return;
}
if (null !== $constraint->normalizer) {
$value = ($constraint->normalizer)($value);
}
if (null === $constraint->mode) {
if (Email::VALIDATION_MODE_STRICT === $this->defaultMode && !class_exists(EguliasEmailValidator::class)) {
throw new LogicException(\sprintf('The "egulias/email-validator" component is required to make the "%s" constraint default to strict mode. Try running "composer require egulias/email-validator".', Email::class));
}
$constraint->mode = $this->defaultMode;
}
if (!\in_array($constraint->mode, Email::VALIDATION_MODES, true)) {
throw new InvalidArgumentException(\sprintf('The "%s::$mode" parameter value is not valid.', get_debug_type($constraint)));
}
if (Email::VALIDATION_MODE_STRICT === $constraint->mode) {
$strictValidator = new EguliasEmailValidator();
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();
}
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();
}
}
elseif (!preg_match(self::EMAIL_PATTERNS[$constraint->mode], $value)) {
$this->context
->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value))
->setCode(Email::INVALID_FORMAT_ERROR)
->addViolation();
}
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overriden Title |
---|---|---|---|---|
ConstraintValidator::$context | protected | property | ||
ConstraintValidator::formatTypeOf | protected | function | Returns a string representation of the type of the value. | |
ConstraintValidator::formatValue | protected | function | Returns a string representation of the value. | |
ConstraintValidator::formatValues | protected | function | Returns a string representation of a list of values. | |
ConstraintValidator::initialize | public | function | Initializes the constraint validator. | Overrides ConstraintValidatorInterface::initialize |
ConstraintValidator::OBJECT_TO_STRING | public | constant | Whether to cast objects with a "__toString()" method to strings. | |
ConstraintValidator::PRETTY_DATE | public | constant | Whether to format {@link \DateTime} objects, either with the {@link \IntlDateFormatter} (if it is available) or as RFC-3339 dates ("Y-m-d H:i:s"). |
|
EmailValidator::$defaultMode | private | property | ||
EmailValidator::EMAIL_PATTERNS | private | constant | ||
EmailValidator::PATTERN_HTML5 | private | constant | ||
EmailValidator::PATTERN_HTML5_ALLOW_NO_TLD | private | constant | ||
EmailValidator::validate | public | function | Checks if the passed value is valid. | Overrides ConstraintValidatorInterface::validate |
EmailValidator::__construct | public | function |