0 follower

Final Class Yiisoft\Validator\Rule\EmailHandler

InheritanceYiisoft\Validator\Rule\EmailHandler
ImplementsYiisoft\Validator\RuleHandlerInterface

Validates that the value is a valid email address.

Public Methods

Hide inherited methods

Method Description Defined By
validate() Yiisoft\Validator\Rule\EmailHandler

Method Details

Hide inherited methods

validate() public method

public Yiisoft\Validator\Result validate ( mixed $value, Yiisoft\Validator\RuleInterface $rule, Yiisoft\Validator\ValidationContext $context )
$value mixed
$rule Yiisoft\Validator\RuleInterface
$context Yiisoft\Validator\ValidationContext

                public function validate(mixed $value, RuleInterface $rule, ValidationContext $context): Result
{
    if (!$rule instanceof Email) {
        throw new UnexpectedRuleException(Email::class, $rule);
    }
    $result = new Result();
    if (!is_string($value)) {
        return $result->addError($rule->getIncorrectInputMessage(), [
            'property' => $context->getTranslatedProperty(),
            'Property' => $context->getCapitalizedTranslatedProperty(),
            'type' => get_debug_type($value),
        ]);
    }
    if (!preg_match(
        '/^(?P<name>(?:"?([^"]*)"?\s)?)(?:\s+)?((?P<open><?)((?P<local>.+)@(?P<domain>[^>]+))(?P<close>>?))/',
        $value,
        $matches,
    )) {
        $valid = $rule->isIdnEnabled()
            ? (bool) preg_match($rule->getIdnEmailPattern(), $value)
            : false;
    } else {
        /**
         * @psalm-var array{
         *     name: string,
         *     local: non-empty-string,
         *     open: string,
         *     domain: non-empty-string,
         *     close: string,
         * } $matches
         */
        $valid = $this->validateParsedValue(
            $rule,
            $value,
            $matches['name'],
            $matches['local'],
            $matches['open'],
            $matches['domain'],
            $matches['close'],
        );
    }
    if ($valid === false) {
        $result->addError($rule->getMessage(), [
            'property' => $context->getTranslatedProperty(),
            'Property' => $context->getCapitalizedTranslatedProperty(),
            'value' => $value,
        ]);
    }
    return $result;
}