mirror of
https://github.com/retailcrm/mailgun-php.git
synced 2024-11-22 12:36:02 +03:00
Provide new Assert to validate an email address
This commit is contained in:
parent
77b50d3d17
commit
e94334b4f1
@ -11,6 +11,10 @@ declare(strict_types=1);
|
||||
|
||||
namespace Mailgun;
|
||||
|
||||
use Egulias\EmailValidator\EmailValidator;
|
||||
use Egulias\EmailValidator\Validation\DNSCheckValidation;
|
||||
use Egulias\EmailValidator\Validation\RFCValidation;
|
||||
use Egulias\EmailValidator\Validation\SpoofCheckValidation;
|
||||
use Mailgun\Exception\InvalidArgumentException;
|
||||
|
||||
/**
|
||||
@ -20,6 +24,44 @@ use Mailgun\Exception\InvalidArgumentException;
|
||||
*/
|
||||
final class Assert extends \Webmozart\Assert\Assert
|
||||
{
|
||||
/**
|
||||
* Validates the given email address.
|
||||
*
|
||||
* @param string $address
|
||||
*/
|
||||
public static function email($address)
|
||||
{
|
||||
// Validates the given value as a string with a minimum and maximum length.
|
||||
self::stringNotEmpty($address, 'Email address must be a non-empty string.');
|
||||
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.');
|
||||
|
||||
// Provides an initial email validation based on `egulias/EmailValidator` library
|
||||
$validator = new EmailValidator();
|
||||
|
||||
if (!$validator->isValid($address, new RFCValidation())) {
|
||||
static::reportInvalidArgument(sprintf(
|
||||
'Email address `%s` has thrown an error when processing a RFC Validation',
|
||||
$address
|
||||
));
|
||||
} elseif (!$validator->isValid($address, new DNSCheckValidation())) {
|
||||
static::reportInvalidArgument(sprintf(
|
||||
'Email address `%s` has thrown an error when processing a DNS Check Validation',
|
||||
$address
|
||||
));
|
||||
} elseif (!$validator->isValid($address, new SpoofCheckValidation())) {
|
||||
static::reportInvalidArgument(sprintf(
|
||||
'Email address `%s` has thrown an error when processing a Spoof Check Validation',
|
||||
$address
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Overwrites the existing `reportInvalidArgument` method in order to thrown a Mailgun Exception.
|
||||
*
|
||||
* @param string $message
|
||||
*/
|
||||
protected static function reportInvalidArgument($message)
|
||||
{
|
||||
throw new InvalidArgumentException($message);
|
||||
|
Loading…
Reference in New Issue
Block a user