Skip to content

Commit 6bf8a60

Browse files
committed
Added argument to StartsWith expander
1 parent c543c98 commit 6bf8a60

File tree

2 files changed

+49
-8
lines changed

2 files changed

+49
-8
lines changed

src/Coduo/PHPMatcher/Matcher/Pattern/Expander/StartsWith.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,19 @@ class StartsWith implements PatternExpander
1616
* @var null|string
1717
*/
1818
private $error;
19+
/**
20+
* @var bool
21+
*/
22+
private $ignoreCase;
1923

2024
/**
2125
* @param string $stringBeginning
26+
* @param bool $ignoreCase
2227
*/
23-
public function __construct($stringBeginning)
28+
public function __construct($stringBeginning, $ignoreCase = false)
2429
{
2530
$this->stringBeginning = $stringBeginning;
31+
$this->ignoreCase = $ignoreCase;
2632
}
2733

2834
/**
@@ -36,7 +42,11 @@ public function match($value)
3642
return false;
3743
}
3844

39-
if (!empty($this->stringBeginning) && strpos($value, $this->stringBeginning) !== 0) {
45+
if (empty($this->stringBeginning)) {
46+
return true;
47+
}
48+
49+
if ($this->matchValue($value)) {
4050
$this->error = sprintf("string \"%s\" doesn't starts with string \"%s\".", $value, $this->stringBeginning);
4151
return false;
4252
}
@@ -51,4 +61,15 @@ public function getError()
5161
{
5262
return $this->error;
5363
}
64+
65+
/**
66+
* @param $value
67+
* @return bool
68+
*/
69+
protected function matchValue($value)
70+
{
71+
return $this->ignoreCase
72+
? strpos(strtolower($value), strtolower($this->stringBeginning)) !== 0
73+
: strpos($value, $this->stringBeginning) !== 0;
74+
}
5475
}

tests/Coduo/PHPMatcher/Matcher/Pattern/Expander/StartsWithTest.php

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,40 @@
88
class StartsWithTest extends \PHPUnit_Framework_TestCase
99
{
1010
/**
11-
* @dataProvider validCasesProvider
11+
* @dataProvider notIgnoringCaseExamplesProvider
1212
*/
13-
public function test_valid_cases($stringBeginning, $value)
13+
public function test_examples_not_ignoring_case($stringBeginning, $value, $expectedResult)
1414
{
1515
$expander = new StartsWith($stringBeginning);
16+
$this->assertEquals($expectedResult, $expander->match($value));
17+
}
18+
19+
public static function notIgnoringCaseExamplesProvider()
20+
{
21+
return array(
22+
array("lorem", "lorem ipsum", true),
23+
array("lorem", "Lorem ipsum", false),
24+
array("", "lorem ipsum", true),
25+
array("lorem", "lorem ipsum", true),
26+
array("ipsum", "lorem ipsum", false)
27+
);
28+
}
29+
30+
/**
31+
* @dataProvider ignoringCaseExamplesProvider
32+
*/
33+
public function test_examples_ignoring_case($stringBeginning, $value, $expectedResult)
34+
{
35+
$expander = new StartsWith($stringBeginning, true);
1636
$this->assertTrue($expander->match($value));
1737
}
1838

19-
public static function validCasesProvider()
39+
public static function ignoringCaseExamplesProvider()
2040
{
2141
return array(
22-
array("lorem", "lorem ipsum"),
23-
array("", "lorem ipsum"),
24-
array("lorem", "lorem ipsum")
42+
array("lorem", "Lorem ipsum", true),
43+
array("Lorem", "lorem ipsum", true),
44+
array("LOREM", "LoReM ipsum", true),
2545
);
2646
}
2747

0 commit comments

Comments
 (0)