24
24
class EmailValidator extends ConstraintValidator
25
25
{
26
26
/**
27
- * @var bool
27
+ * @var string
28
28
*/
29
- private $ isStrict ;
29
+ private $ mode ;
30
30
31
- public function __construct ($ strict = false )
31
+ public function __construct ($ mode = ' html5 ' )
32
32
{
33
- $ this ->isStrict = $ strict ;
33
+ $ this ->mode = $ mode ;
34
34
}
35
35
36
36
/**
@@ -52,33 +52,45 @@ public function validate($value, Constraint $constraint)
52
52
53
53
$ value = (string ) $ value ;
54
54
55
- if (null === $ constraint ->strict ) {
56
- $ constraint ->strict = $ this ->isStrict ;
55
+ if (null === $ constraint ->mode ) {
56
+ $ constraint ->mode = $ this ->mode ;
57
57
}
58
58
59
- if ($ constraint ->strict ) {
59
+ var_dump ($ constraint ->mode );
60
+
61
+ if ($ constraint ->mode === Email::VALIDATION_MODE_RFC ) {
60
62
if (!class_exists ('\Egulias\EmailValidator\EmailValidator ' )) {
61
- throw new RuntimeException ('Strict email validation requires egulias/email-validator ~1.2|~2.0 ' );
63
+ throw new RuntimeException ('RFC email validation requires egulias/email-validator ~1.2|~2.0 ' );
62
64
}
63
65
64
- $ strictValidator = new \Egulias \EmailValidator \EmailValidator ();
66
+ $ rfcValidator = new \Egulias \EmailValidator \EmailValidator ();
65
67
66
- if (interface_exists (EmailValidation::class) && !$ strictValidator ->isValid ($ value , new NoRFCWarningsValidation ())) {
68
+ if (interface_exists (EmailValidation::class) && !$ rfcValidator ->isValid ($ value , new NoRFCWarningsValidation ())) {
67
69
$ this ->context ->buildViolation ($ constraint ->message )
68
- ->setParameter ('{{ value }} ' , $ this ->formatValue ($ value ))
69
- ->setCode (Email::INVALID_FORMAT_ERROR )
70
- ->addViolation ();
70
+ ->setParameter ('{{ value }} ' , $ this ->formatValue ($ value ))
71
+ ->setCode (Email::INVALID_FORMAT_ERROR )
72
+ ->addViolation ();
71
73
72
74
return ;
73
- } elseif (!interface_exists (EmailValidation::class) && !$ strictValidator ->isValid ($ value , false , true )) {
75
+ } elseif (!interface_exists (EmailValidation::class) && !$ rfcValidator ->isValid ($ value , false , true )) {
74
76
$ this ->context ->buildViolation ($ constraint ->message )
75
- ->setParameter ('{{ value }} ' , $ this ->formatValue ($ value ))
76
- ->setCode (Email::INVALID_FORMAT_ERROR )
77
- ->addViolation ();
77
+ ->setParameter ('{{ value }} ' , $ this ->formatValue ($ value ))
78
+ ->setCode (Email::INVALID_FORMAT_ERROR )
79
+ ->addViolation ();
78
80
79
81
return ;
80
82
}
81
- } elseif (!preg_match ('/^.+\@\S+\.\S+$/ ' , $ value )) {
83
+ } elseif ($ constraint ->mode === Email::VALIDATION_MODE_LOOSE && preg_match ('/^.+\@\S+\.\S+$/ ' , $ value )) {
84
+ $ this ->context ->buildViolation ($ constraint ->message )
85
+ ->setParameter ('{{ value }} ' , $ this ->formatValue ($ value ))
86
+ ->setCode (Email::INVALID_FORMAT_ERROR )
87
+ ->addViolation ();
88
+
89
+ return ;
90
+ } elseif ($ constraint ->mode === Email::VALIDATION_MODE_HTML5 && preg_match (
91
+ '/^[a-zA-Z0-9.!#$%& \'*+ \\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)+$/ ' ,
92
+ $ value
93
+ )) {
82
94
$ this ->context ->buildViolation ($ constraint ->message )
83
95
->setParameter ('{{ value }} ' , $ this ->formatValue ($ value ))
84
96
->setCode (Email::INVALID_FORMAT_ERROR )
0 commit comments