Skip to content

Commit 55456e2

Browse files
authored
Added count pattern expander (#86)
1 parent d57de79 commit 55456e2

File tree

4 files changed

+101
-0
lines changed

4 files changed

+101
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
final class Count implements PatternExpander
9+
{
10+
/**
11+
* @var null|string
12+
*/
13+
private $error;
14+
15+
/**
16+
* @var
17+
*/
18+
private $value;
19+
20+
/**
21+
* @param $value
22+
*/
23+
public function __construct($value)
24+
{
25+
$this->value = $value;
26+
}
27+
28+
/**
29+
* @param $value
30+
* @return boolean
31+
*/
32+
public function match($value)
33+
{
34+
if (!is_array($value)) {
35+
$this->error = sprintf("Count expander require \"array\", got \"%s\".", new StringConverter($value));
36+
return false;
37+
}
38+
39+
if (count($value) !== $this->value) {
40+
$this->error = sprintf("Expected count of %s is %s.", new StringConverter($value), new StringConverter($this->value));
41+
return false;
42+
}
43+
44+
return true;
45+
}
46+
47+
/**
48+
* @return string|null
49+
*/
50+
public function getError()
51+
{
52+
return $this->error;
53+
}
54+
}

src/Parser/ExpanderInitializer.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ final class ExpanderInitializer
2525
"lowerThan" => "Coduo\\PHPMatcher\\Matcher\\Pattern\\Expander\\LowerThan",
2626
"greaterThan" => "Coduo\\PHPMatcher\\Matcher\\Pattern\\Expander\\GreaterThan",
2727
"inArray" => "Coduo\\PHPMatcher\\Matcher\\Pattern\\Expander\\InArray",
28+
"count" => "Coduo\\PHPMatcher\\Matcher\\Pattern\\Expander\\Count",
2829
"contains" => "Coduo\\PHPMatcher\\Matcher\\Pattern\\Expander\\Contains",
2930
"matchRegex" => "Coduo\\PHPMatcher\\Matcher\\Pattern\\Expander\\MatchRegex",
3031

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace Coduo\PHPMatcher\Tests\Matcher\Pattern\Expander;
4+
5+
use Coduo\PHPMatcher\Matcher;
6+
use Coduo\PHPMatcher\Matcher\Pattern\Expander\Count;
7+
8+
class CountTest extends \PHPUnit_Framework_TestCase
9+
{
10+
/**
11+
* @dataProvider examplesProvider
12+
*/
13+
public function test_matching_values($needle, $haystack, $expectedResult)
14+
{
15+
$expander = new Count($needle);
16+
$this->assertEquals($expectedResult, $expander->match($haystack));
17+
}
18+
19+
public static function examplesProvider()
20+
{
21+
return array(
22+
array(1, array("ipsum"), true),
23+
array(2, array("foo", 1), true),
24+
);
25+
}
26+
27+
/**
28+
* @dataProvider invalidCasesProvider
29+
*/
30+
public function test_error_when_matching_fail($boundary, $value, $errorMessage)
31+
{
32+
$expander = new Count($boundary);
33+
$this->assertFalse($expander->match($value));
34+
$this->assertEquals($errorMessage, $expander->getError());
35+
}
36+
37+
public static function invalidCasesProvider()
38+
{
39+
return array(
40+
array(2, array(1, 2, 3), "Expected count of Array(3) is 2."),
41+
array(2, new \DateTime(), "Count expander require \"array\", got \"\\DateTime\"."),
42+
);
43+
}
44+
}

tests/MatcherTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,8 @@ public static function expanderExamples()
244244
array(array("foo", "bar"), "@[email protected](\"bar\")", true),
245245
array(array(), "@[email protected]()", true),
246246
array(array('foo'), "@[email protected]()", false),
247+
array(array(1, 2, 3), "@[email protected](3)", true),
248+
array(array(1, 2, 3), "@[email protected](4)", false),
247249
array("lorem ipsum", "@[email protected](contains(\"lorem\"), contains(\"test\"))", true),
248250
array("lorem ipsum", "@[email protected](contains(\"lorem\"), contains(\"test\")).endsWith(\"ipsum\")", true),
249251
array("lorem ipsum", "@[email protected](\"/^lorem \\w+$/\")", true),

0 commit comments

Comments
 (0)