diff --git a/src/Coduo/PHPMatcher/Matcher/Pattern/Expander/IsDateTime.php b/src/Coduo/PHPMatcher/Matcher/Pattern/Expander/IsDateTime.php new file mode 100644 index 00000000..f2f8530c --- /dev/null +++ b/src/Coduo/PHPMatcher/Matcher/Pattern/Expander/IsDateTime.php @@ -0,0 +1,55 @@ +error = sprintf("IsDateTime expander require \"string\", got \"%s\".", new String($value)); + return false; + } + + if (false === $this->matchValue($value)) { + $this->error = sprintf("string \"%s\" is not a valid date.", $value); + return false; + } + + return true; + } + + /** + * @return string|null + */ + public function getError() + { + return $this->error; + } + + /** + * @param string $value + * @return bool + */ + protected function matchValue($value) + { + try { + new \DateTime($value); + return true; + } catch (\Exception $e) { + return false; + } + } +} diff --git a/src/Coduo/PHPMatcher/Matcher/Pattern/Expander/IsEmail.php b/src/Coduo/PHPMatcher/Matcher/Pattern/Expander/IsEmail.php new file mode 100644 index 00000000..0a9d5f5e --- /dev/null +++ b/src/Coduo/PHPMatcher/Matcher/Pattern/Expander/IsEmail.php @@ -0,0 +1,54 @@ +error = sprintf("IsEmail expander require \"string\", got \"%s\".", new String($value)); + return false; + } + + if (false === $this->matchValue($value)) { + $this->error = sprintf("string \"%s\" is not a valid e-mail address.", $value); + return false; + } + + return true; + } + + /** + * @return string|null + */ + public function getError() + { + return $this->error; + } + + /** + * @param string $value + * @return bool + */ + protected function matchValue($value) + { + try { + return false !== filter_var($value, FILTER_VALIDATE_EMAIL); + } catch (\Exception $e) { + return false; + } + } +} diff --git a/src/Coduo/PHPMatcher/Parser/ExpanderInitializer.php b/src/Coduo/PHPMatcher/Parser/ExpanderInitializer.php index c82f7219..cf541a21 100644 --- a/src/Coduo/PHPMatcher/Parser/ExpanderInitializer.php +++ b/src/Coduo/PHPMatcher/Parser/ExpanderInitializer.php @@ -18,6 +18,8 @@ class ExpanderInitializer "startsWith" => "Coduo\\PHPMatcher\\Matcher\\Pattern\\Expander\\StartsWith", "endsWith" => "Coduo\\PHPMatcher\\Matcher\\Pattern\\Expander\\EndsWith", "notEmpty" => "Coduo\\PHPMatcher\\Matcher\\Pattern\\Expander\\NotEmpty", + "isDateTime" => "Coduo\\PHPMatcher\\Matcher\\Pattern\\Expander\\IsDateTime", + "isEmail" => "Coduo\\PHPMatcher\\Matcher\\Pattern\\Expander\\IsEmail", "lowerThan" => "Coduo\\PHPMatcher\\Matcher\\Pattern\\Expander\\LowerThan", "greaterThan" => "Coduo\\PHPMatcher\\Matcher\\Pattern\\Expander\\GreaterThan", "inArray" => "Coduo\\PHPMatcher\\Matcher\\Pattern\\Expander\\InArray", diff --git a/tests/Coduo/PHPMatcher/Matcher/Pattern/Expander/IsDateTimeTest.php b/tests/Coduo/PHPMatcher/Matcher/Pattern/Expander/IsDateTimeTest.php new file mode 100644 index 00000000..151c05e7 --- /dev/null +++ b/tests/Coduo/PHPMatcher/Matcher/Pattern/Expander/IsDateTimeTest.php @@ -0,0 +1,28 @@ +assertEquals($expectedResult, $expander->match($date)); + } + + public static function examplesDatesProvider() + { + return array( + array("201-20-44", false), + array("2012-10-11", true), + array("invalid", false), + array("Monday, 15-Aug-2005 15:52:01 UTC", true) + ); + } +} diff --git a/tests/Coduo/PHPMatcher/Matcher/Pattern/Expander/IsEmailTest.php b/tests/Coduo/PHPMatcher/Matcher/Pattern/Expander/IsEmailTest.php new file mode 100644 index 00000000..1f554540 --- /dev/null +++ b/tests/Coduo/PHPMatcher/Matcher/Pattern/Expander/IsEmailTest.php @@ -0,0 +1,28 @@ +assertEquals($expectedResult, $expander->match($email)); + } + + public static function examplesEmailsProvider() + { + return array( + array("valid@email.com", true), + array("valid+12345@email.com", true), + array("...@domain.com", false), + array("2222----###@domain.co", true) + ); + } +}