-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Michal Szczur
committed
Mar 17, 2016
1 parent
87ff316
commit 4a2dbe1
Showing
4 changed files
with
273 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
namespace Coduo\PHPMatcher\Matcher; | ||
|
||
final class OrMatcher extends Matcher | ||
{ | ||
const MATCH_PATTERN = "/\|\|/"; | ||
|
||
/** | ||
* @var ChainMatcher | ||
*/ | ||
private $chainMatcher; | ||
|
||
/** | ||
* @param ChainMatcher $chainMatcher | ||
*/ | ||
public function __construct(ChainMatcher $chainMatcher) | ||
{ | ||
$this->chainMatcher = $chainMatcher; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function match($value, $pattern) | ||
{ | ||
$patterns = explode('||', $pattern); | ||
foreach ($patterns as $childPattern) { | ||
if ($this->matchChild($value, $childPattern)){ | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* Matches single pattern | ||
* | ||
* @param $value | ||
* @param $pattern | ||
* @return bool | ||
*/ | ||
private function matchChild($value, $pattern) | ||
{ | ||
if (!$this->chainMatcher->canMatch($pattern)) { | ||
return false; | ||
} | ||
|
||
if ($this->chainMatcher->match($value, $pattern)) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function canMatch($pattern) | ||
{ | ||
return is_string($pattern) && 0 !== preg_match_all(self::MATCH_PATTERN, $pattern, $matches); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
<?php | ||
namespace Coduo\PHPMatcher\Tests\Matcher; | ||
|
||
use Coduo\PHPMatcher\Factory\SimpleFactory; | ||
use Coduo\PHPMatcher\Matcher; | ||
use Coduo\PHPMatcher\Parser; | ||
|
||
class OrMatcherTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @var Matcher\OrMatcher | ||
*/ | ||
private $matcher; | ||
|
||
public function setUp() | ||
{ | ||
$factory = new SimpleFactory(); | ||
$this->matcher = $factory->createMatcher(); | ||
} | ||
|
||
/** | ||
* @dataProvider positiveMatchData | ||
*/ | ||
public function test_positive_match_arrays($value, $pattern) | ||
{ | ||
$this->assertTrue( | ||
$this->matcher->match($value, $pattern), | ||
$this->matcher->getError() | ||
); | ||
} | ||
|
||
/** | ||
* @dataProvider negativeMatchData | ||
*/ | ||
public function test_negative_match_arrays($value, $pattern) | ||
{ | ||
$this->assertFalse( | ||
$this->matcher->match($value, $pattern), | ||
$this->matcher->getError() | ||
); | ||
} | ||
|
||
public static function positiveMatchData() | ||
{ | ||
$simpleArr = array( | ||
'users' => array( | ||
array( | ||
'firstName' => 'Norbert', | ||
'lastName' => 'Orzechowicz' | ||
), | ||
array( | ||
'firstName' => 1, | ||
'lastName' => 2 | ||
) | ||
), | ||
true, | ||
false, | ||
1, | ||
6.66 | ||
); | ||
|
||
$simpleArrPattern = array( | ||
'users' => array( | ||
array( | ||
'firstName' => '@string@', | ||
'lastName' => '@null@||@string@||@integer@' | ||
), | ||
'@...@' | ||
), | ||
true, | ||
false, | ||
1, | ||
6.66 | ||
); | ||
|
||
return array( | ||
array('test', '@string@'), | ||
array(null, '@array@||@string@||@null@'), | ||
array( | ||
array( | ||
'test' => 1 | ||
), | ||
array( | ||
'test' => '@integer@' | ||
) | ||
), | ||
array( | ||
array( | ||
'test' => null | ||
), | ||
array( | ||
'test' => '@integer@||@null@' | ||
) | ||
), | ||
array( | ||
array( | ||
'first_level' => array('second_level', array('third_level')) | ||
), | ||
'@array@||@null@||@*@' | ||
), | ||
array($simpleArr, $simpleArr), | ||
array($simpleArr, $simpleArrPattern), | ||
); | ||
} | ||
|
||
public static function negativeMatchData() | ||
{ | ||
$simpleArr = array( | ||
'users' => array( | ||
array( | ||
'firstName' => 'Norbert', | ||
'lastName' => 'Orzechowicz' | ||
), | ||
array( | ||
'firstName' => 'Michał', | ||
'lastName' => 'Dąbrowski' | ||
) | ||
), | ||
true, | ||
false, | ||
1, | ||
6.66 | ||
); | ||
|
||
$simpleDiff = array( | ||
'users' => array( | ||
array( | ||
'firstName' => 'Norbert', | ||
'lastName' => 'Orzechowicz' | ||
), | ||
array( | ||
'firstName' => 'Pablo', | ||
'lastName' => '@integer@||@null@||@double@' | ||
) | ||
), | ||
true, | ||
false, | ||
1, | ||
6.66 | ||
); | ||
|
||
return array( | ||
array($simpleArr, $simpleDiff), | ||
array(array("status" => "ok", "data" => array(array('foo'))), array("status" => "ok", "data" => array())), | ||
array(array(1), array()), | ||
array(array('key' => 'val'), array('key' => 'val2')), | ||
array(array(1), array(2)), | ||
array(array('foo', 1, 3), array('foo', 2, 3)), | ||
array(array(), array('foo' => 'bar')), | ||
array(10, '@null@||@[email protected](10)'), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
|
||
namespace Coduo\PHPMatcher\Tests; | ||
|
||
use Coduo\PHPMatcher\Lexer; | ||
use Coduo\PHPMatcher\Factory\SimpleFactory; | ||
use Coduo\PHPMatcher\Matcher; | ||
use Coduo\PHPMatcher\Parser; | ||
use Coduo\PHPMatcher\PHPMatcher; | ||
|
@@ -16,29 +16,8 @@ class MatcherTest extends \PHPUnit_Framework_TestCase | |
|
||
public function setUp() | ||
{ | ||
$parser = new Parser(new Lexer(), new Parser\ExpanderInitializer()); | ||
$scalarMatchers = new Matcher\ChainMatcher(array( | ||
new Matcher\CallbackMatcher(), | ||
new Matcher\ExpressionMatcher(), | ||
new Matcher\NullMatcher(), | ||
new Matcher\StringMatcher($parser), | ||
new Matcher\IntegerMatcher($parser), | ||
new Matcher\BooleanMatcher(), | ||
new Matcher\DoubleMatcher($parser), | ||
new Matcher\NumberMatcher(), | ||
new Matcher\ScalarMatcher(), | ||
new Matcher\WildcardMatcher(), | ||
)); | ||
|
||
$arrayMatcher = new Matcher\ArrayMatcher($scalarMatchers, $parser); | ||
|
||
$this->matcher = new Matcher(new Matcher\ChainMatcher(array( | ||
$scalarMatchers, | ||
$arrayMatcher, | ||
new Matcher\JsonMatcher($arrayMatcher), | ||
new Matcher\XmlMatcher($arrayMatcher), | ||
new Matcher\TextMatcher($scalarMatchers, $parser) | ||
))); | ||
$factory = new SimpleFactory(); | ||
$this->matcher = $factory->createMatcher(); | ||
} | ||
|
||
public function test_matcher_with_array_value() | ||
|
@@ -217,6 +196,15 @@ public function test_matcher_with_wildcard() | |
$this->assertTrue(PHPMatcher::match('test', '@wildcard@')); | ||
} | ||
|
||
/** | ||
* @dataProvider orExamples() | ||
*/ | ||
public function test_matcher_with_or($value, $pattern, $expectedResult) | ||
{ | ||
$this->assertSame($expectedResult, $this->matcher->match($value, $pattern)); | ||
$this->assertSame($expectedResult, PHPMatcher::match($value, $pattern)); | ||
} | ||
|
||
/** | ||
* @dataProvider expanderExamples() | ||
*/ | ||
|
@@ -235,7 +223,7 @@ public function scalarValueExamples() | |
array(array('foo'), '@array@') | ||
); | ||
} | ||
|
||
public static function expanderExamples() | ||
{ | ||
return array( | ||
|
@@ -262,4 +250,17 @@ public static function expanderExamples() | |
array("lorem ipsum", "@[email protected](\"/^foo/\")", false), | ||
); | ||
} | ||
|
||
public static function orExamples() | ||
{ | ||
return array( | ||
array("lorem ipsum", "@[email protected](\"lorem\")||@[email protected](\"lorem\")", true), | ||
array("[email protected]", "@[email protected]()||@null@", true), | ||
array(null, "@[email protected]()||@null@", true), | ||
array(null, "@[email protected]()||@null@", true), | ||
array("2014-08-19", "@[email protected]()||@integer@", true), | ||
array(null, "@integer@||@string@", false), | ||
array(1, "@[email protected](10)||@[email protected](\"10\")", false), | ||
); | ||
} | ||
} |