Skip to content

Commit ae724fd

Browse files
author
Norbert Orzechowicz
committed
Merge pull request #61 from blazarecki/match-expander
Add Match expander
2 parents ffad87c + 66c5d82 commit ae724fd

File tree

5 files changed

+120
-0
lines changed

5 files changed

+120
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ $match = $matcher->match("lorem ipsum dolor", "@string@")
5858
* ``greaterThan($boundry)``
5959
* ``inArray($value)``
6060
* ``oneOf(...$expanders)`` - example usage ``"@[email protected](contains('foo'), contains('bar'), contains('baz'))"``
61+
* ``matchRegex($regex)`` - example usage ``"@[email protected]('/^lorem.+/')"``
6162

6263
##Example usage
6364

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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 <[email protected]>
10+
*/
11+
class MatchRegex implements PatternExpander
12+
{
13+
/**
14+
* @var null|string
15+
*/
16+
private $error;
17+
18+
/**
19+
* @var string
20+
*/
21+
private $pattern;
22+
23+
/**
24+
* @param string $pattern
25+
*/
26+
public function __construct($pattern)
27+
{
28+
if (!is_string($pattern)) {
29+
throw new \InvalidArgumentException("Regex pattern must be a string.");
30+
}
31+
32+
if (!is_string($pattern) || @preg_match($pattern, '') === false) {
33+
throw new \InvalidArgumentException("Regex pattern must be a valid one.");
34+
}
35+
36+
$this->pattern = $pattern;
37+
}
38+
39+
/**
40+
* @param string $value
41+
*
42+
* @return boolean
43+
*/
44+
public function match($value)
45+
{
46+
if (false === is_string($value)) {
47+
$this->error = sprintf("Match expander require \"string\", got \"%s\".", new StringConverter($value));
48+
49+
return false;
50+
}
51+
52+
if (1 !== preg_match($this->pattern, $value)) {
53+
$this->error = sprintf("string \"%s\" don't match pattern %s.", $value, $this->pattern);
54+
55+
return false;
56+
}
57+
58+
return true;
59+
}
60+
61+
/**
62+
* @return string|null
63+
*/
64+
public function getError()
65+
{
66+
return $this->error;
67+
}
68+
}

src/Coduo/PHPMatcher/Parser/ExpanderInitializer.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class ExpanderInitializer
2626
"greaterThan" => "Coduo\\PHPMatcher\\Matcher\\Pattern\\Expander\\GreaterThan",
2727
"inArray" => "Coduo\\PHPMatcher\\Matcher\\Pattern\\Expander\\InArray",
2828
"contains" => "Coduo\\PHPMatcher\\Matcher\\Pattern\\Expander\\Contains",
29+
"match" => "Coduo\\PHPMatcher\\Matcher\\Pattern\\Expander\\MatchRegex",
2930

3031
"oneOf" => "Coduo\\PHPMatcher\\Matcher\\Pattern\\Expander\\OneOf"
3132
);
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace Coduo\PHPMatcher\Tests\Matcher\Pattern\Expander;
4+
5+
use Coduo\PHPMatcher\Matcher\Pattern\Expander\MatchRegex;
6+
7+
/**
8+
* @author Benjamin Lazarecki <[email protected]>
9+
*/
10+
class MatchRegexTest extends \PHPUnit_Framework_TestCase
11+
{
12+
/**
13+
* @dataProvider examplesProvider
14+
*/
15+
public function test_match_expander($expectedResult, $expectedError, $pattern, $value)
16+
{
17+
$expander = new MatchRegex($pattern);
18+
$this->assertEquals($expectedResult, $expander->match($value));
19+
$this->assertSame($expectedError, $expander->getError());
20+
}
21+
22+
public static function examplesProvider()
23+
{
24+
return array(
25+
array(true, null, '/^\w$/', 'a'),
26+
array(false, 'string "aa" don\'t match pattern /^\w$/.', '/^\w$/', 'aa'),
27+
array(false, 'Match expander require "string", got "Array(0)".', '/^\w$/', array()),
28+
);
29+
}
30+
31+
/**
32+
* @expectedException \InvalidArgumentException
33+
* @expectedExceptionMessage Regex pattern must be a string.
34+
*/
35+
public function test_that_it_only_work_with_string()
36+
{
37+
new MatchRegex(null);
38+
}
39+
40+
/**
41+
* @expectedException \InvalidArgumentException
42+
* @expectedExceptionMessage Regex pattern must be a valid one.
43+
*/
44+
public function test_that_it_only_work_with_valid_pattern()
45+
{
46+
new MatchRegex('///');
47+
}
48+
}

tests/Coduo/PHPMatcher/MatcherTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,8 @@ public static function expanderExamples()
259259
array(array('foo'), "@[email protected]()", false),
260260
array("lorem ipsum", "@[email protected](contains(\"lorem\"), contains(\"test\"))", true),
261261
array("lorem ipsum", "@[email protected](contains(\"lorem\"), contains(\"test\")).endsWith(\"ipsum\")", true),
262+
array("lorem ipsum", "@[email protected](\"/^lorem \\w+$/\")", true),
263+
array("lorem ipsum", "@[email protected](\"/^foo/\")", false),
262264
);
263265
}
264266
}

0 commit comments

Comments
 (0)