Skip to content

Commit

Permalink
Merge pull request #18 from norzechowicz/factory
Browse files Browse the repository at this point in the history
Matcher factory introduction
  • Loading branch information
defrag committed Jun 19, 2014
2 parents 70f00bc + 6bf2d8e commit 4f9bf64
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 24 deletions.
27 changes: 3 additions & 24 deletions match.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
<?php

use Coduo\PHPMatcher\Matcher\ArrayMatcher;
use Coduo\PHPMatcher\Matcher\CallbackMatcher;
use Coduo\PHPMatcher\Matcher\ChainMatcher;
use Coduo\PHPMatcher\Matcher\ExpressionMatcher;
use Coduo\PHPMatcher\Matcher\JsonMatcher;
use Coduo\PHPMatcher\Matcher\NullMatcher;
use Coduo\PHPMatcher\Matcher\ScalarMatcher;
use Coduo\PHPMatcher\Matcher\TypeMatcher;
use Coduo\PHPMatcher\Matcher\WildcardMatcher;
use Coduo\PHPMatcher\Matcher;
use Coduo\PHPMatcher\Factory\SimpleFactory;

if (is_dir($vendor = __DIR__ . '/../vendor')) {
require_once($vendor . '/autoload.php');
Expand All @@ -33,20 +24,8 @@
*/
function match($value, $pattern)
{
$scalarMatchers = new ChainMatcher(array(
new CallbackMatcher(),
new ExpressionMatcher(),
new TypeMatcher(),
new NullMatcher(),
new ScalarMatcher(),
new WildcardMatcher()
));
$arrayMatcher = new ArrayMatcher($scalarMatchers);
$matcher = new Matcher(new ChainMatcher(array(
$scalarMatchers,
$arrayMatcher,
new JsonMatcher($arrayMatcher)
)));
$factory = new SimpleFactory();
$matcher = $factory->createMatcher();

return $matcher->match($value, $pattern);
}
Expand Down
10 changes: 10 additions & 0 deletions src/Coduo/PHPMatcher/Factory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
namespace Coduo\PHPMatcher;

interface Factory
{
/**
* @return Matcher
*/
public function createMatcher();
}
47 changes: 47 additions & 0 deletions src/Coduo/PHPMatcher/Factory/SimpleFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Coduo\PHPMatcher\Factory;

use Coduo\PHPMatcher\Factory;
use Coduo\PHPMatcher\Matcher;

class SimpleFactory implements Factory
{
/**
* @return Matcher
*/
public function createMatcher()
{
return new Matcher($this->buildMatchers());
}

/**
* @return Matcher\ChainMatcher
*/
protected function buildMatchers()
{
$scalarMatchers = $this->buildScalarMatchers();
$arrayMatcher = new Matcher\ArrayMatcher($scalarMatchers);

return new Matcher\ChainMatcher(array(
$scalarMatchers,
$arrayMatcher,
new Matcher\JsonMatcher($arrayMatcher)
));
}

/**
* @return Matcher\ChainMatcher
*/
protected function buildScalarMatchers()
{
return new Matcher\ChainMatcher(array(
new Matcher\CallbackMatcher(),
new Matcher\ExpressionMatcher(),
new Matcher\TypeMatcher(),
new Matcher\NullMatcher(),
new Matcher\ScalarMatcher(),
new Matcher\WildcardMatcher()
));
}
}
14 changes: 14 additions & 0 deletions tests/Coduo/PHPMatcher/Factory/SimpleFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
namespace Coduo\PHPMatcher\Tests;

use Coduo\PHPMatcher\Matcher;
use Coduo\PHPMatcher\Factory\SimpleFactory;

class SimpleFactoryTest extends \PHPUnit_Framework_TestCase
{
public function test_creating_matcher()
{
$factory = new SimpleFactory();
$this->assertInstanceOf('Coduo\PHPMatcher\Matcher', $factory->createMatcher());
}
}

0 comments on commit 4f9bf64

Please sign in to comment.