Skip to content
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
60 changes: 30 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ if (!PHPMatcher::match("lorem ipsum dolor", "@string@", $error)) {
```php
<?php

use Coduo\PHPMatcher\Factory\SimpleFactory;
use Coduo\PHPMatcher\Factory\MatcherFactory;

$factory = new SimpleFactory();
$factory = new MatcherFactory();
$matcher = $factory->createMatcher();

$match = $matcher->match("lorem ipsum dolor", "@string@");
Expand Down Expand Up @@ -98,9 +98,9 @@ $matcher->getError(); // returns null or error message
```php
<?php

use Coduo\PHPMatcher\Factory\SimpleFactory;
use Coduo\PHPMatcher\Factory\MatcherFactory;

$factory = new SimpleFactory();
$factory = new MatcherFactory();
$matcher = $factory->createMatcher();

$matcher->match(1, 1);
Expand All @@ -112,9 +112,9 @@ $matcher->match('string', 'string');
```php
<?php

use Coduo\PHPMatcher\Factory\SimpleFactory;
use Coduo\PHPMatcher\Factory\MatcherFactory;

$factory = new SimpleFactory();
$factory = new MatcherFactory();
$matcher = $factory->createMatcher();

$matcher->match('Norbert', '@string@');
Expand All @@ -127,9 +127,9 @@ $matcher->match("lorem ipsum dolor", "@string@.startsWith('lorem').contains('ips
```php
<?php

use Coduo\PHPMatcher\Factory\SimpleFactory;
use Coduo\PHPMatcher\Factory\MatcherFactory;

$factory = new SimpleFactory();
$factory = new MatcherFactory();
$matcher = $factory->createMatcher();

$matcher->match('2014-08-19', '@string@.isDateTime()');
Expand All @@ -143,9 +143,9 @@ $matcher->match('2014-08-19', '@string@.isDateTime().before("today").after("+ 10
```php
<?php

use Coduo\PHPMatcher\Factory\SimpleFactory;
use Coduo\PHPMatcher\Factory\MatcherFactory;

$factory = new SimpleFactory();
$factory = new MatcherFactory();
$matcher = $factory->createMatcher();

$matcher->match(100, '@integer@');
Expand All @@ -158,9 +158,9 @@ $matcher->match(100, '@integer@.lowerThan(200).greaterThan(10)');
```php
<?php

use Coduo\PHPMatcher\Factory\SimpleFactory;
use Coduo\PHPMatcher\Factory\MatcherFactory;

$factory = new SimpleFactory();
$factory = new MatcherFactory();
$matcher = $factory->createMatcher();

$matcher->match(100, '@number@');
Expand All @@ -175,9 +175,9 @@ $matcher->match(0b10100111001, '@number@');
```php
<?php

use Coduo\PHPMatcher\Factory\SimpleFactory;
use Coduo\PHPMatcher\Factory\MatcherFactory;

$factory = new SimpleFactory();
$factory = new MatcherFactory();
$matcher = $factory->createMatcher();

$matcher->match(10.1, "@double@");
Expand All @@ -189,9 +189,9 @@ $matcher->match(10.1, "@double@.lowerThan(50.12).greaterThan(10)");
```php
<?php

use Coduo\PHPMatcher\Factory\SimpleFactory;
use Coduo\PHPMatcher\Factory\MatcherFactory;

$factory = new SimpleFactory();
$factory = new MatcherFactory();
$matcher = $factory->createMatcher();

$matcher->match(true, "@boolean@");
Expand All @@ -203,9 +203,9 @@ $matcher->match(false, "@boolean@");
```php
<?php

use Coduo\PHPMatcher\Factory\SimpleFactory;
use Coduo\PHPMatcher\Factory\MatcherFactory;

$factory = new SimpleFactory();
$factory = new MatcherFactory();
$matcher = $factory->createMatcher();

$matcher->match("@integer@", "@*@");
Expand All @@ -221,9 +221,9 @@ $matcher->match(new \stdClass, "@wildcard@");
```php
<?php

use Coduo\PHPMatcher\Factory\SimpleFactory;
use Coduo\PHPMatcher\Factory\MatcherFactory;

$factory = new SimpleFactory();
$factory = new MatcherFactory();
$matcher = $factory->createMatcher();

$matcher->match(new \DateTime('2014-04-01'), "expr(value.format('Y-m-d') == '2014-04-01'");
Expand All @@ -235,9 +235,9 @@ $matcher->match("Norbert", "expr(value === 'Norbert')");
```php
<?php

use Coduo\PHPMatcher\Factory\SimpleFactory;
use Coduo\PHPMatcher\Factory\MatcherFactory;

$factory = new SimpleFactory();
$factory = new MatcherFactory();
$matcher = $factory->createMatcher();

$matcher->match('9f4db639-0e87-4367-9beb-d64e3f42ae18', '@uuid@');
Expand All @@ -248,9 +248,9 @@ $matcher->match('9f4db639-0e87-4367-9beb-d64e3f42ae18', '@uuid@');
```php
<?php

use Coduo\PHPMatcher\Factory\SimpleFactory;
use Coduo\PHPMatcher\Factory\MatcherFactory;

$factory = new SimpleFactory();
$factory = new MatcherFactory();
$matcher = $factory->createMatcher();

$matcher->match(
Expand Down Expand Up @@ -307,9 +307,9 @@ $matcher->match(
```php
<?php

use Coduo\PHPMatcher\Factory\SimpleFactory;
use Coduo\PHPMatcher\Factory\MatcherFactory;

$factory = new SimpleFactory();
$factory = new MatcherFactory();
$matcher = $factory->createMatcher();

$matcher->match(
Expand Down Expand Up @@ -341,9 +341,9 @@ $matcher->match(
```php
<?php

use Coduo\PHPMatcher\Factory\SimpleFactory;
use Coduo\PHPMatcher\Factory\MatcherFactory;

$factory = new SimpleFactory();
$factory = new MatcherFactory();
$matcher = $factory->createMatcher();

$matcher->match(
Expand Down Expand Up @@ -399,9 +399,9 @@ $matcher->match(
```php
<?php

use Coduo\PHPMatcher\Factory\SimpleFactory;
use Coduo\PHPMatcher\Factory\MatcherFactory;

$factory = new SimpleFactory();
$factory = new MatcherFactory();
$matcher = $factory->createMatcher();

$matcher->match(<<<XML
Expand Down
73 changes: 73 additions & 0 deletions src/Factory/MatcherFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

declare(strict_types=1);

namespace Coduo\PHPMatcher\Factory;

use Coduo\PHPMatcher\Factory;
use Coduo\PHPMatcher\Lexer;
use Coduo\PHPMatcher\Matcher;
use Coduo\PHPMatcher\Parser;

final class MatcherFactory implements Factory
{
public function createMatcher() : Matcher
{
return new Matcher($this->buildMatchers($this->buildParser()));
}

protected function buildMatchers(Parser $parser) : Matcher\ChainMatcher
{
$scalarMatchers = $this->buildScalarMatchers($parser);
$arrayMatcher = $this->buildArrayMatcher($scalarMatchers, $parser);

$chainMatcher = new Matcher\ChainMatcher([
$scalarMatchers,
$arrayMatcher,
new Matcher\OrMatcher($scalarMatchers),
new Matcher\JsonMatcher($arrayMatcher),
new Matcher\XmlMatcher($arrayMatcher),
new Matcher\TextMatcher($scalarMatchers, $parser)
]);

return $chainMatcher;
}

protected function buildArrayMatcher(Matcher\ChainMatcher $scalarMatchers, Parser $parser) : Matcher\ArrayMatcher
{
$orMatcher = new Matcher\OrMatcher($scalarMatchers);
$arrayMatcher = new Matcher\ArrayMatcher(
new Matcher\ChainMatcher([
$orMatcher,
$scalarMatchers,
new Matcher\TextMatcher($scalarMatchers, $parser)
]),
$parser
);

return $arrayMatcher;
}

protected function buildScalarMatchers(Parser $parser) : Matcher\ChainMatcher
{
return new Matcher\ChainMatcher([
new Matcher\CallbackMatcher(),
new Matcher\ExpressionMatcher(),
new Matcher\NullMatcher(),
new Matcher\StringMatcher($parser),
new Matcher\IntegerMatcher($parser),
new Matcher\BooleanMatcher($parser),
new Matcher\DoubleMatcher($parser),
new Matcher\NumberMatcher($parser),
new Matcher\ScalarMatcher(),
new Matcher\WildcardMatcher(),
new Matcher\UuidMatcher($parser),
new Matcher\JsonObjectMatcher($parser)
]);
}

protected function buildParser() : Parser
{
return new Parser(new Lexer(), new Parser\ExpanderInitializer());
}
}
80 changes: 4 additions & 76 deletions src/Factory/SimpleFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,87 +5,15 @@
namespace Coduo\PHPMatcher\Factory;

use Coduo\PHPMatcher\Factory;
use Coduo\PHPMatcher\Lexer;
use Coduo\PHPMatcher\Matcher;
use Coduo\PHPMatcher\Parser;

/**
* @deprecated Please use \Coduo\PHPMatcher\Factory\MatcherFactory instead
*/
class SimpleFactory implements Factory
{
private $parser;

public function createMatcher() : Matcher
{
return new Matcher($this->buildMatchers());
}

protected function buildMatchers() : Matcher\ChainMatcher
{
$scalarMatchers = $this->buildScalarMatchers();
$orMatcher = $this->buildOrMatcher();

$chainMatcher = new Matcher\ChainMatcher([
$scalarMatchers,
$orMatcher,
new Matcher\JsonMatcher($orMatcher, $this->buildParser()),
new Matcher\XmlMatcher($orMatcher),
new Matcher\TextMatcher($scalarMatchers, $this->buildParser())
]);

return $chainMatcher;
}

protected function buildOrMatcher() : Matcher\ChainMatcher
{
$scalarMatchers = $this->buildScalarMatchers();
$orMatcher = new Matcher\OrMatcher($scalarMatchers);
$arrayMatcher = new Matcher\ArrayMatcher(
new Matcher\ChainMatcher([
$orMatcher,
$scalarMatchers,
new Matcher\TextMatcher($scalarMatchers, $this->buildParser())
]),
$this->buildParser()
);

$chainMatcher = new Matcher\ChainMatcher([
$orMatcher,
$arrayMatcher,
]);

return $chainMatcher;
}

/**
* @return Matcher\ChainMatcher
*/
protected function buildScalarMatchers() : Matcher\ChainMatcher
{
$parser = $this->buildParser();

return new Matcher\ChainMatcher([
new Matcher\CallbackMatcher(),
new Matcher\ExpressionMatcher(),
new Matcher\NullMatcher(),
new Matcher\StringMatcher($parser),
new Matcher\IntegerMatcher($parser),
new Matcher\BooleanMatcher($parser),
new Matcher\DoubleMatcher($parser),
new Matcher\NumberMatcher($parser),
new Matcher\ScalarMatcher(),
new Matcher\WildcardMatcher(),
new Matcher\UuidMatcher($parser),
new Matcher\JsonObjectMatcher($parser)
]);
}

protected function buildParser() : Parser
{
if ($this->parser) {
return $this->parser;
}

$this->parser = new Parser(new Lexer(), new Parser\ExpanderInitializer());

return $this->parser;
return (new MatcherFactory())->createMatcher();
}
}
Loading