Skip to content

Commit 443ede1

Browse files
author
Norbert Orzechowicz
committed
Merge pull request #60 from blazarecki/is-empty-expander
Add IsEmpty expander
2 parents c4917b1 + 9442d8e commit 443ede1

File tree

5 files changed

+74
-0
lines changed

5 files changed

+74
-0
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ $match = $matcher->match("lorem ipsum dolor", "@string@")
5353
* ``isEmail()``
5454
* ``isUrl()``
5555
* ``notEmpty()``
56+
* ``isEmpty()``
5657
* ``lowerThan($boundry)``
5758
* ``greaterThan($boundry)``
5859
* ``inArray($value)``
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Coduo\PHPMatcher\Matcher\Pattern\Expander;
4+
5+
use Coduo\PHPMatcher\Matcher\Pattern\PatternExpander;
6+
use Coduo\ToString\StringConverter;
7+
8+
/**
9+
* @author Benjamin Lazarecki <benjamin.lazarecki@gmail.com>
10+
*/
11+
class isEmpty implements PatternExpander
12+
{
13+
private $error;
14+
15+
/**
16+
* @param mixed $value
17+
*
18+
* @return boolean
19+
*/
20+
public function match($value)
21+
{
22+
if (!empty($value)) {
23+
$this->error = sprintf("Value %s is not empty.", new StringConverter($value));
24+
25+
return false;
26+
}
27+
28+
return true;
29+
}
30+
31+
/**
32+
* @return string|null
33+
*/
34+
public function getError()
35+
{
36+
return $this->error;
37+
}
38+
}

Diff for: src/Coduo/PHPMatcher/Parser/ExpanderInitializer.php

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class ExpanderInitializer
1818
"startsWith" => "Coduo\\PHPMatcher\\Matcher\\Pattern\\Expander\\StartsWith",
1919
"endsWith" => "Coduo\\PHPMatcher\\Matcher\\Pattern\\Expander\\EndsWith",
2020
"notEmpty" => "Coduo\\PHPMatcher\\Matcher\\Pattern\\Expander\\NotEmpty",
21+
"isEmpty" => "Coduo\\PHPMatcher\\Matcher\\Pattern\\Expander\\IsEmpty",
2122
"isDateTime" => "Coduo\\PHPMatcher\\Matcher\\Pattern\\Expander\\IsDateTime",
2223
"isEmail" => "Coduo\\PHPMatcher\\Matcher\\Pattern\\Expander\\IsEmail",
2324
"isUrl" => "Coduo\\PHPMatcher\\Matcher\\Pattern\\Expander\\IsUrl",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Coduo\PHPMatcher\Tests\Matcher\Pattern\Expander;
4+
5+
use Coduo\PHPMatcher\Matcher\Pattern\Expander\isEmpty;
6+
7+
/**
8+
* @author Benjamin Lazarecki <benjamin.lazarecki@gmail.com>
9+
*/
10+
class isEmptyTest extends \PHPUnit_Framework_TestCase
11+
{
12+
/**
13+
* @dataProvider examplesProvider
14+
*/
15+
public function test_is_empty_expander_match($value, $expectedResult)
16+
{
17+
$expander = new IsEmpty();
18+
$this->assertEquals($expectedResult, $expander->match($value));
19+
}
20+
21+
public static function examplesProvider()
22+
{
23+
return array(
24+
array(array(), true),
25+
array(array('data'), false),
26+
array('', true),
27+
array(0, true),
28+
array(null, true),
29+
);
30+
}
31+
}

Diff for: tests/Coduo/PHPMatcher/MatcherTest.php

+3
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,10 @@ public static function expanderExamples()
253253
array(100, "@integer@.lowerThan(101).greaterThan(10)", true),
254254
array("", "@string@.notEmpty()", false),
255255
array("lorem ipsum", "@string@.notEmpty()", true),
256+
array("", "@string@.isEmpty()", true),
256257
array(array("foo", "bar"), "@array@.inArray(\"bar\")", true),
258+
array(array(), "@array@.isEmpty()", true),
259+
array(array('foo'), "@array@.isEmpty()", false),
257260
array("lorem ipsum", "@string@.oneOf(contains(\"lorem\"), contains(\"test\"))", true),
258261
array("lorem ipsum", "@string@.oneOf(contains(\"lorem\"), contains(\"test\")).endsWith(\"ipsum\")", true),
259262
);

0 commit comments

Comments
 (0)