Extract the email address from the string

me@davidgarcia.cat => me@davidgarcia.cat
David Garcia <me@davidgarcia.cat> => me@davidgarcia.cat
David Garcia me@davidgarcia.cat => me@davidgarcia.cat
David me@davidgarcia.cat Garcia => me@davidgarcia.cat
This commit is contained in:
David Garcia 2017-12-08 01:30:43 +00:00 committed by David Garcia
parent 756b63ef54
commit d15dbc4f69

View File

@ -37,23 +37,35 @@ final class Assert extends \Webmozart\Assert\Assert
self::minLength($address, 3, 'Minimum length for the email address is 3 characters.'); self::minLength($address, 3, 'Minimum length for the email address is 3 characters.');
self::maxLength($address, 512, 'Maximum length for the email address is 512 chatacters.'); self::maxLength($address, 512, 'Maximum length for the email address is 512 chatacters.');
// Extract the email address from the string
preg_match('/[\w\.-]+@[\w\.-]+/', $address, $emailAddress);
if (1 !== count($emailAddress) || !isset($emailAddress[0])) {
static::reportInvalidArgument(sprintf(
'Email address cannot be extracted from `%s`.',
$address
));
}
$emailAddress = $emailAddress[0];
// Provides an initial email validation based on `egulias/EmailValidator` library // Provides an initial email validation based on `egulias/EmailValidator` library
$validator = new EmailValidator(); $validator = new EmailValidator();
if (!$validator->isValid($address, new RFCValidation())) { if (!$validator->isValid($emailAddress, new RFCValidation())) {
static::reportInvalidArgument(sprintf( static::reportInvalidArgument(sprintf(
'Email address `%s` has thrown an error when processing a RFC Validation.', 'Email address `%s` has thrown an error when processing a RFC Validation.',
$address $emailAddress
)); ));
} elseif (!$validator->isValid($address, new DNSCheckValidation())) { } elseif (!$validator->isValid($emailAddress, new DNSCheckValidation())) {
static::reportInvalidArgument(sprintf( static::reportInvalidArgument(sprintf(
'Email address `%s` has thrown an error when processing a DNS Check Validation.', 'Email address `%s` has thrown an error when processing a DNS Check Validation.',
$address $emailAddress
)); ));
} elseif (!$validator->isValid($address, new SpoofCheckValidation())) { } elseif (!$validator->isValid($emailAddress, new SpoofCheckValidation())) {
static::reportInvalidArgument(sprintf( static::reportInvalidArgument(sprintf(
'Email address `%s` has thrown an error when processing a Spoof Check Validation.', 'Email address `%s` has thrown an error when processing a Spoof Check Validation.',
$address $emailAddress
)); ));
} }
} }