feature #19153 [Validator] support egulias/email-validator 2.x (xabbuh)
This PR was merged into the 3.2-dev branch.
Discussion
----------
[Validator] support egulias/email-validator 2.x
| Q | A
| ------------- | ---
| Branch? | master
| Bug fix? | no
| New feature? | yes
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes
| Fixed tickets |
| License | MIT
| Doc PR |
Commits
-------
25d39aa
support egulias/email-validator 2.x
This commit is contained in:
commit
9baf51b00f
|
@ -85,7 +85,7 @@
|
|||
"monolog/monolog": "~1.11",
|
||||
"ocramius/proxy-manager": "~0.4|~1.0|~2.0",
|
||||
"predis/predis": "~1.0",
|
||||
"egulias/email-validator": "~1.2",
|
||||
"egulias/email-validator": "~1.2,>=1.2.8|~2.0",
|
||||
"symfony/polyfill-apcu": "~1.1",
|
||||
"symfony/security-acl": "~2.8|~3.0",
|
||||
"phpdocumentor/reflection-docblock": "^3.0"
|
||||
|
|
|
@ -11,6 +11,8 @@
|
|||
|
||||
namespace Symfony\Component\Validator\Constraints;
|
||||
|
||||
use Egulias\EmailValidator\Validation\EmailValidation;
|
||||
use Egulias\EmailValidator\Validation\RFCValidation;
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
use Symfony\Component\Validator\ConstraintValidator;
|
||||
use Symfony\Component\Validator\Exception\RuntimeException;
|
||||
|
@ -61,7 +63,14 @@ class EmailValidator extends ConstraintValidator
|
|||
|
||||
$strictValidator = new \Egulias\EmailValidator\EmailValidator();
|
||||
|
||||
if (!$strictValidator->isValid($value, false, true)) {
|
||||
if (interface_exists(EmailValidation::class) && !$strictValidator->isValid($value, new RFCValidation())) {
|
||||
$this->context->buildViolation($constraint->message)
|
||||
->setParameter('{{ value }}', $this->formatValue($value))
|
||||
->setCode(Email::INVALID_FORMAT_ERROR)
|
||||
->addViolation();
|
||||
|
||||
return;
|
||||
} 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)
|
||||
|
|
|
@ -103,6 +103,80 @@ class EmailValidatorTest extends ConstraintValidatorTestCase
|
|||
$this->assertNoViolation();
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider getInvalidEmailsForStrictChecks
|
||||
*/
|
||||
public function testStrictWithInvalidEmails($email)
|
||||
{
|
||||
$constraint = new Email(array(
|
||||
'message' => 'myMessage',
|
||||
'strict' => true,
|
||||
));
|
||||
|
||||
$this->validator->validate($email, $constraint);
|
||||
|
||||
$this
|
||||
->buildViolation('myMessage')
|
||||
->setParameter('{{ value }}', '"'.$email.'"')
|
||||
->setCode(Email::INVALID_FORMAT_ERROR)
|
||||
->assertRaised();
|
||||
}
|
||||
|
||||
/**
|
||||
* @link https://github.com/egulias/EmailValidator/blob/1.2.8/tests/egulias/Tests/EmailValidator/EmailValidatorTest.php
|
||||
*/
|
||||
public function getInvalidEmailsForStrictChecks()
|
||||
{
|
||||
return array(
|
||||
array('test@example.com test'),
|
||||
array('user name@example.com'),
|
||||
array('user name@example.com'),
|
||||
array('example.@example.co.uk'),
|
||||
array('example@example@example.co.uk'),
|
||||
array('(test_exampel@example.fr)'),
|
||||
array('example(example)example@example.co.uk'),
|
||||
array('.example@localhost'),
|
||||
array('ex\ample@localhost'),
|
||||
array('example@local\host'),
|
||||
array('example@localhost.'),
|
||||
array('user name@example.com'),
|
||||
array('username@ example . com'),
|
||||
array('example@(fake).com'),
|
||||
array('example@(fake.com'),
|
||||
array('username@example,com'),
|
||||
array('usern,ame@example.com'),
|
||||
array('user[na]me@example.com'),
|
||||
array('"""@iana.org'),
|
||||
array('"\"@iana.org'),
|
||||
array('"test"test@iana.org'),
|
||||
array('"test""test"@iana.org'),
|
||||
array('"test"."test"@iana.org'),
|
||||
array('"test".test@iana.org'),
|
||||
array('"test"'.chr(0).'@iana.org'),
|
||||
array('"test\"@iana.org'),
|
||||
array(chr(226).'@iana.org'),
|
||||
array('test@'.chr(226).'.org'),
|
||||
array('\r\ntest@iana.org'),
|
||||
array('\r\n test@iana.org'),
|
||||
array('\r\n \r\ntest@iana.org'),
|
||||
array('\r\n \r\ntest@iana.org'),
|
||||
array('\r\n \r\n test@iana.org'),
|
||||
array('test@iana.org \r\n'),
|
||||
array('test@iana.org \r\n '),
|
||||
array('test@iana.org \r\n \r\n'),
|
||||
array('test@iana.org \r\n\r\n'),
|
||||
array('test@iana.org \r\n\r\n '),
|
||||
array('test@iana/icann.org'),
|
||||
array('test@foo;bar.com'),
|
||||
array('test;123@foobar.com'),
|
||||
array('test@example..com'),
|
||||
array('email.email@email."'),
|
||||
array('test@email>'),
|
||||
array('test@email<'),
|
||||
array('test@email{'),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider getDnsChecks
|
||||
* @requires function Symfony\Bridge\PhpUnit\DnsMock::withMockedHosts
|
||||
|
|
|
@ -29,7 +29,7 @@
|
|||
"symfony/cache": "~3.1",
|
||||
"doctrine/annotations": "~1.0",
|
||||
"doctrine/cache": "~1.0",
|
||||
"egulias/email-validator": "~1.2,>=1.2.1"
|
||||
"egulias/email-validator": "~1.2,>=1.2.8|~2.0"
|
||||
},
|
||||
"suggest": {
|
||||
"psr/cache-implementation": "For using the metadata cache.",
|
||||
|
|
Reference in New Issue