Skip to content

Commit b4a3a16

Browse files
committed
Added Contains expander
1 parent b586f9c commit b4a3a16

File tree

4 files changed

+133
-1
lines changed

4 files changed

+133
-1
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace Coduo\PHPMatcher\Matcher\Pattern\Expander;
4+
5+
use Coduo\PHPMatcher\Matcher\Pattern\PatternExpander;
6+
use Coduo\ToString\String;
7+
8+
class Contains implements PatternExpander
9+
{
10+
/**
11+
* @var null|string
12+
*/
13+
private $error;
14+
15+
/**
16+
* @var
17+
*/
18+
private $string;
19+
20+
/**
21+
* @var bool
22+
*/
23+
private $ignoreCase;
24+
25+
/**
26+
* @param $string
27+
* @param bool $ignoreCase
28+
*/
29+
public function __construct($string, $ignoreCase = false)
30+
{
31+
$this->string = $string;
32+
$this->ignoreCase = $ignoreCase;
33+
}
34+
35+
/**
36+
* @param $value
37+
* @return boolean
38+
*/
39+
public function match($value)
40+
{
41+
if (!is_string($value)) {
42+
$this->error = sprintf("Contains expander require \"string\", got \"%s\".", new String($value));
43+
return false;
44+
}
45+
46+
$contains = $this->ignoreCase
47+
? mb_strpos(mb_strtolower($value), mb_strtolower($this->string))
48+
: mb_strpos($value, $this->string);
49+
50+
if ($contains === false) {
51+
$this->error = sprintf("String \"%s\" doesn't contains \"%s\".", $value, $this->string);
52+
return false;
53+
}
54+
55+
return true;
56+
}
57+
58+
/**
59+
* @return string|null
60+
*/
61+
public function getError()
62+
{
63+
return $this->error;
64+
}
65+
}

src/Coduo/PHPMatcher/Parser.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ class Parser
2828
"notEmpty" => "Coduo\\PHPMatcher\\Matcher\\Pattern\\Expander\\NotEmpty",
2929
"lowerThan" => "Coduo\\PHPMatcher\\Matcher\\Pattern\\Expander\\LowerThan",
3030
"greaterThan" => "Coduo\\PHPMatcher\\Matcher\\Pattern\\Expander\\GreaterThan",
31-
"inArray" => "Coduo\\PHPMatcher\\Matcher\\Pattern\\Expander\\InArray"
31+
"inArray" => "Coduo\\PHPMatcher\\Matcher\\Pattern\\Expander\\InArray",
32+
"contains" => "Coduo\\PHPMatcher\\Matcher\\Pattern\\Expander\\Contains"
3233
);
3334

3435
/**
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace Coduo\PHPMatcher\Tests\Matcher\Pattern\Expander;
4+
5+
use Coduo\PHPMatcher\Matcher;
6+
use Coduo\PHPMatcher\Matcher\Pattern\Expander\Contains;
7+
8+
class ContainsTest extends \PHPUnit_Framework_TestCase
9+
{
10+
/**
11+
* @dataProvider examplesIgnoreCaseProvider
12+
*/
13+
public function test_matching_values_ignore_case($needle, $haystack, $expectedResult)
14+
{
15+
$expander = new Contains($needle);
16+
$this->assertEquals($expectedResult, $expander->match($haystack));
17+
}
18+
19+
public static function examplesIgnoreCaseProvider()
20+
{
21+
return array(
22+
array("ipsum", "lorem ipsum", true),
23+
array("wor", "this is my hello world string", true),
24+
array("lol", "lorem ipsum", false),
25+
array("NO", "norbert", false)
26+
);
27+
}
28+
29+
/**
30+
* @dataProvider examplesProvider
31+
*/
32+
public function test_matching_values($needle, $haystack, $expectedResult)
33+
{
34+
$expander = new Contains($needle, true);
35+
$this->assertEquals($expectedResult, $expander->match($haystack));
36+
}
37+
38+
public static function examplesProvider()
39+
{
40+
return array(
41+
array("IpSum", "lorem ipsum", true),
42+
array("wor", "this is my hello WORLD string", true),
43+
array("lol", "LOREM ipsum", false),
44+
array("NO", "NORBERT", true)
45+
);
46+
}
47+
48+
/**
49+
* @dataProvider invalidCasesProvider
50+
*/
51+
public function test_error_when_matching_fail($string, $value, $errorMessage)
52+
{
53+
$expander = new Contains($string);
54+
$this->assertFalse($expander->match($value));
55+
$this->assertEquals($errorMessage, $expander->getError());
56+
}
57+
58+
public static function invalidCasesProvider()
59+
{
60+
return array(
61+
array("ipsum", "hello world", "String \"hello world\" doesn't contains \"ipsum\"."),
62+
array("lorem", new \DateTime(), "Contains expander require \"string\", got \"\\DateTime\"."),
63+
);
64+
}
65+
}

tests/Coduo/PHPMatcher/Matcher/StringMatcherTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ public static function positiveMatchData()
7373
array("lorem ipsum", "@[email protected]()"),
7474
array("lorem ipsum", "@[email protected]('lorem')"),
7575
array("lorem ipsum", "@[email protected]('ipsum')"),
76+
array("lorem ipsum dolor", "@[email protected]('lorem').contains('ipsum').endsWith('dolor')"),
7677
);
7778
}
7879

0 commit comments

Comments
 (0)