@@ 8-55 (lines=48) @@ | ||
5 | use Coduo\PHPMatcher\Matcher\Pattern\PatternExpander; |
|
6 | use Coduo\ToString\StringConverter; |
|
7 | ||
8 | final class IsDateTime 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("IsDateTime expander require \"string\", got \"%s\".", new StringConverter($value)); |
|
23 | return false; |
|
24 | } |
|
25 | ||
26 | if (false === $this->matchValue($value)) { |
|
27 | $this->error = sprintf("string \"%s\" is not a valid date.", $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 | new \DateTime($value); |
|
50 | return true; |
|
51 | } catch (\Exception $e) { |
|
52 | return false; |
|
53 | } |
|
54 | } |
|
55 | } |
|
56 |
@@ 8-54 (lines=47) @@ | ||
5 | use Coduo\PHPMatcher\Matcher\Pattern\PatternExpander; |
|
6 | use Coduo\ToString\StringConverter; |
|
7 | ||
8 | final 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 StringConverter($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 | } |
|
55 |
@@ 8-54 (lines=47) @@ | ||
5 | use Coduo\PHPMatcher\Matcher\Pattern\PatternExpander; |
|
6 | use Coduo\ToString\StringConverter; |
|
7 | ||
8 | final class IsUrl 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("IsUrl expander require \"string\", got \"%s\".", new StringConverter($value)); |
|
23 | return false; |
|
24 | } |
|
25 | ||
26 | if (false === $this->matchValue($value)) { |
|
27 | $this->error = sprintf("string \"%s\" is not a valid URL.", $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_URL); |
|
50 | } catch (\Exception $e) { |
|
51 | return false; |
|
52 | } |
|
53 | } |
|
54 | } |
|
55 |