Skip to content

Matcher factory introduction #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 19, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
}
}