diff --git a/README.md b/README.md index 0f0a4056..427079ba 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,7 @@ $match = $matcher->match("lorem ipsum dolor", "@string@") * ``isEmail()`` * ``isUrl()`` * ``notEmpty()`` +* ``isEmpty()`` * ``lowerThan($boundry)`` * ``greaterThan($boundry)`` * ``inArray($value)`` diff --git a/src/Coduo/PHPMatcher/Matcher/Pattern/Expander/isEmpty.php b/src/Coduo/PHPMatcher/Matcher/Pattern/Expander/isEmpty.php new file mode 100644 index 00000000..c06599ce --- /dev/null +++ b/src/Coduo/PHPMatcher/Matcher/Pattern/Expander/isEmpty.php @@ -0,0 +1,38 @@ + + */ +class isEmpty implements PatternExpander +{ + private $error; + + /** + * @param mixed $value + * + * @return boolean + */ + public function match($value) + { + if (!empty($value)) { + $this->error = sprintf("Value %s is not empty.", new StringConverter($value)); + + return false; + } + + return true; + } + + /** + * @return string|null + */ + public function getError() + { + return $this->error; + } +} diff --git a/src/Coduo/PHPMatcher/Parser/ExpanderInitializer.php b/src/Coduo/PHPMatcher/Parser/ExpanderInitializer.php index d9d58795..ce86638f 100644 --- a/src/Coduo/PHPMatcher/Parser/ExpanderInitializer.php +++ b/src/Coduo/PHPMatcher/Parser/ExpanderInitializer.php @@ -18,6 +18,7 @@ class ExpanderInitializer "startsWith" => "Coduo\\PHPMatcher\\Matcher\\Pattern\\Expander\\StartsWith", "endsWith" => "Coduo\\PHPMatcher\\Matcher\\Pattern\\Expander\\EndsWith", "notEmpty" => "Coduo\\PHPMatcher\\Matcher\\Pattern\\Expander\\NotEmpty", + "isEmpty" => "Coduo\\PHPMatcher\\Matcher\\Pattern\\Expander\\IsEmpty", "isDateTime" => "Coduo\\PHPMatcher\\Matcher\\Pattern\\Expander\\IsDateTime", "isEmail" => "Coduo\\PHPMatcher\\Matcher\\Pattern\\Expander\\IsEmail", "isUrl" => "Coduo\\PHPMatcher\\Matcher\\Pattern\\Expander\\IsUrl", diff --git a/tests/Coduo/PHPMatcher/Matcher/Pattern/Expander/isEmptyTest.php b/tests/Coduo/PHPMatcher/Matcher/Pattern/Expander/isEmptyTest.php new file mode 100644 index 00000000..bef5c662 --- /dev/null +++ b/tests/Coduo/PHPMatcher/Matcher/Pattern/Expander/isEmptyTest.php @@ -0,0 +1,31 @@ + + */ +class isEmptyTest extends \PHPUnit_Framework_TestCase +{ + /** + * @dataProvider examplesProvider + */ + public function test_is_empty_expander_match($value, $expectedResult) + { + $expander = new IsEmpty(); + $this->assertEquals($expectedResult, $expander->match($value)); + } + + public static function examplesProvider() + { + return array( + array(array(), true), + array(array('data'), false), + array('', true), + array(0, true), + array(null, true), + ); + } +} diff --git a/tests/Coduo/PHPMatcher/MatcherTest.php b/tests/Coduo/PHPMatcher/MatcherTest.php index fa24516c..15fecabe 100644 --- a/tests/Coduo/PHPMatcher/MatcherTest.php +++ b/tests/Coduo/PHPMatcher/MatcherTest.php @@ -253,7 +253,10 @@ public static function expanderExamples() array(100, "@integer@.lowerThan(101).greaterThan(10)", true), array("", "@string@.notEmpty()", false), array("lorem ipsum", "@string@.notEmpty()", true), + array("", "@string@.isEmpty()", true), array(array("foo", "bar"), "@array@.inArray(\"bar\")", true), + array(array(), "@array@.isEmpty()", true), + array(array('foo'), "@array@.isEmpty()", false), array("lorem ipsum", "@string@.oneOf(contains(\"lorem\"), contains(\"test\"))", true), array("lorem ipsum", "@string@.oneOf(contains(\"lorem\"), contains(\"test\")).endsWith(\"ipsum\")", true), );