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 {
$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;
}
User Contributed Notes
Leave a comment
Join the conversation to share a note.