Skip to content

Add IsEmpty expander #60

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 1 commit into from
Jan 16, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ $match = $matcher->match("lorem ipsum dolor", "@string@")
* ``isEmail()``
* ``isUrl()``
* ``notEmpty()``
* ``isEmpty()``
* ``lowerThan($boundry)``
* ``greaterThan($boundry)``
* ``inArray($value)``
Expand Down
38 changes: 38 additions & 0 deletions src/Coduo/PHPMatcher/Matcher/Pattern/Expander/isEmpty.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Coduo\PHPMatcher\Matcher\Pattern\Expander;

use Coduo\PHPMatcher\Matcher\Pattern\PatternExpander;
use Coduo\ToString\StringConverter;

/**
* @author Benjamin Lazarecki <benjamin.lazarecki@gmail.com>
*/
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;
}
}
1 change: 1 addition & 0 deletions src/Coduo/PHPMatcher/Parser/ExpanderInitializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
31 changes: 31 additions & 0 deletions tests/Coduo/PHPMatcher/Matcher/Pattern/Expander/isEmptyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Coduo\PHPMatcher\Tests\Matcher\Pattern\Expander;

use Coduo\PHPMatcher\Matcher\Pattern\Expander\isEmpty;

/**
* @author Benjamin Lazarecki <benjamin.lazarecki@gmail.com>
*/
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),
);
}
}
3 changes: 3 additions & 0 deletions tests/Coduo/PHPMatcher/MatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you please also add test that proves that for non empty valute this expander returns false?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, it's done.

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),
);
Expand Down