File tree Expand file tree Collapse file tree 2 files changed +55
-0
lines changed Expand file tree Collapse file tree 2 files changed +55
-0
lines changed Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace Coduo \PHPMatcher \Matcher \Pattern \Expander ;
4
+
5
+ use Coduo \PHPMatcher \Matcher \Pattern \PatternExpander ;
6
+ use Coduo \ToString \String ;
7
+
8
+ class IsEmail implements PatternExpander
9
+ {
10
+ /**
11
+ * @var null|string
12
+ */
13
+ private $ error ;
14
+
15
+ /**
16
+ * @param string $value
17
+ * @return boolean
18
+ */
19
+ public function match ($ value )
20
+ {
21
+ if (false === is_string ($ value )) {
22
+ $ this ->error = sprintf ("IsEmail expander require \"string \", got \"%s \". " , new String ($ value ));
23
+ return false ;
24
+ }
25
+
26
+ if (false === $ this ->matchValue ($ value )) {
27
+ $ this ->error = sprintf ("string \"%s \" is not a valid e-mail address. " , $ value );
28
+ return false ;
29
+ }
30
+
31
+ return true ;
32
+ }
33
+
34
+ /**
35
+ * @return string|null
36
+ */
37
+ public function getError ()
38
+ {
39
+ return $ this ->error ;
40
+ }
41
+
42
+ /**
43
+ * @param string $value
44
+ * @return bool
45
+ */
46
+ protected function matchValue ($ value )
47
+ {
48
+ try {
49
+ return false !== filter_var ($ value , FILTER_VALIDATE_EMAIL );
50
+ } catch (\Exception $ e ) {
51
+ return false ;
52
+ }
53
+ }
54
+ }
Original file line number Diff line number Diff line change @@ -19,6 +19,7 @@ class ExpanderInitializer
19
19
"endsWith " => "Coduo \\PHPMatcher \\Matcher \\Pattern \\Expander \\EndsWith " ,
20
20
"notEmpty " => "Coduo \\PHPMatcher \\Matcher \\Pattern \\Expander \\NotEmpty " ,
21
21
"isDateTime " => "Coduo \\PHPMatcher \\Matcher \\Pattern \\Expander \\IsDateTime " ,
22
+ "isEmail " => "Coduo \\PHPMatcher \\Matcher \\Pattern \\Expander \\IsEmail " ,
22
23
"lowerThan " => "Coduo \\PHPMatcher \\Matcher \\Pattern \\Expander \\LowerThan " ,
23
24
"greaterThan " => "Coduo \\PHPMatcher \\Matcher \\Pattern \\Expander \\GreaterThan " ,
24
25
"inArray " => "Coduo \\PHPMatcher \\Matcher \\Pattern \\Expander \\InArray " ,
You can’t perform that action at this time.
0 commit comments