Skip to content

Commit 3d8ad88

Browse files
committed
Matcher factory introduction
1 parent 70f00bc commit 3d8ad88

File tree

4 files changed

+73
-24
lines changed

4 files changed

+73
-24
lines changed

match.php

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,6 @@
11
<?php
22

3-
use Coduo\PHPMatcher\Matcher\ArrayMatcher;
4-
use Coduo\PHPMatcher\Matcher\CallbackMatcher;
5-
use Coduo\PHPMatcher\Matcher\ChainMatcher;
6-
use Coduo\PHPMatcher\Matcher\ExpressionMatcher;
7-
use Coduo\PHPMatcher\Matcher\JsonMatcher;
8-
use Coduo\PHPMatcher\Matcher\NullMatcher;
9-
use Coduo\PHPMatcher\Matcher\ScalarMatcher;
10-
use Coduo\PHPMatcher\Matcher\TypeMatcher;
11-
use Coduo\PHPMatcher\Matcher\WildcardMatcher;
12-
use Coduo\PHPMatcher\Matcher;
3+
use Coduo\PHPMatcher\Factory\SimpleFactory;
134

145
if (is_dir($vendor = __DIR__ . '/../vendor')) {
156
require_once($vendor . '/autoload.php');
@@ -33,20 +24,8 @@
3324
*/
3425
function match($value, $pattern)
3526
{
36-
$scalarMatchers = new ChainMatcher(array(
37-
new CallbackMatcher(),
38-
new ExpressionMatcher(),
39-
new TypeMatcher(),
40-
new NullMatcher(),
41-
new ScalarMatcher(),
42-
new WildcardMatcher()
43-
));
44-
$arrayMatcher = new ArrayMatcher($scalarMatchers);
45-
$matcher = new Matcher(new ChainMatcher(array(
46-
$scalarMatchers,
47-
$arrayMatcher,
48-
new JsonMatcher($arrayMatcher)
49-
)));
27+
$factory = new SimpleFactory();
28+
$matcher = $factory->createMatcher();
5029

5130
return $matcher->match($value, $pattern);
5231
}

src/Coduo/PHPMatcher/Factory.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
namespace Coduo\PHPMatcher;
3+
4+
interface Factory
5+
{
6+
/**
7+
* @return Matcher
8+
*/
9+
public function createMatcher();
10+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace Coduo\PHPMatcher\Factory;
4+
5+
use Coduo\PHPMatcher\Factory;
6+
use Coduo\PHPMatcher\Matcher;
7+
8+
class SimpleFactory implements Factory
9+
{
10+
/**
11+
* @return Matcher
12+
*/
13+
public function createMatcher()
14+
{
15+
return new Matcher($this->buildMatchers());
16+
}
17+
18+
/**
19+
* @return Matcher\ChainMatcher
20+
*/
21+
protected function buildMatchers()
22+
{
23+
$scalarMatchers = $this->buildScalarMatchers();
24+
$arrayMatcher = new Matcher\ArrayMatcher($scalarMatchers);
25+
26+
return new Matcher\ChainMatcher(array(
27+
$scalarMatchers,
28+
$arrayMatcher,
29+
new Matcher\JsonMatcher($arrayMatcher)
30+
));
31+
}
32+
33+
/**
34+
* @return Matcher\ChainMatcher
35+
*/
36+
protected function buildScalarMatchers()
37+
{
38+
return new Matcher\ChainMatcher(array(
39+
new Matcher\CallbackMatcher(),
40+
new Matcher\ExpressionMatcher(),
41+
new Matcher\TypeMatcher(),
42+
new Matcher\ScalarMatcher(),
43+
new Matcher\WildcardMatcher()
44+
));
45+
}
46+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
namespace Coduo\PHPMatcher\Tests;
3+
4+
use Coduo\PHPMatcher\Matcher;
5+
use Coduo\PHPMatcher\Factory\SimpleFactory;
6+
7+
class SimpleFactoryTest extends \PHPUnit_Framework_TestCase
8+
{
9+
public function test_creating_matcher()
10+
{
11+
$factory = new SimpleFactory();
12+
$this->assertInstanceOf('Coduo\PHPMatcher\Matcher', $factory->createMatcher());
13+
}
14+
}

0 commit comments

Comments
 (0)