Skip to content

Commit 82b039d

Browse files
committed
Initial draft of expander mechanism with StartsWith expander example
Replaced [] with array() in AST\Expander class for php 5.3 support Refactoring Added Boolean and Null argument types support Added negative numbers support Added argument to StartsWith expander Set Lexer as a dependency of Parser and Parser as a dependency of StringMatcher Added possibility to add new expander definitions Added notBlank expander Changed expanderName definition Added lowerThan expander Implement expanders in IntegerMatcher Added expanders to DoubleMatcher Added GreaterThan expander Added EndsWith expander Renamed NotBlank into NotEmpty Added array argument type support Added InArray expander Added Contains expander
1 parent 7809ef4 commit 82b039d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+2300
-80
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
"php": ">=5.3.0",
1818
"coduo/php-to-string": "~1.0",
1919
"symfony/property-access": "~2.3",
20-
"symfony/expression-language": "~2.4"
20+
"symfony/expression-language": "~2.4",
21+
"doctrine/lexer": "1.0.*"
2122
},
2223
"require-dev": {
2324
"phpunit/phpunit": "3.7.*"

src/Coduo/PHPMatcher/AST/Expander.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace Coduo\PHPMatcher\AST;
4+
5+
class Expander implements Node
6+
{
7+
/**
8+
* @var
9+
*/
10+
private $name;
11+
12+
/**
13+
* @var array
14+
*/
15+
private $arguments;
16+
17+
/**
18+
* @param $name
19+
*/
20+
public function __construct($name)
21+
{
22+
$this->name = $name;
23+
$this->arguments = array();
24+
}
25+
26+
/**
27+
* @return mixed
28+
*/
29+
public function getName()
30+
{
31+
return $this->name;
32+
}
33+
34+
/**
35+
* @param $argument
36+
*/
37+
public function addArgument($argument)
38+
{
39+
$this->arguments[] = $argument;
40+
}
41+
42+
/**
43+
* @return bool
44+
*/
45+
public function hasArguments()
46+
{
47+
return (boolean) count($this->arguments);
48+
}
49+
50+
/**
51+
* @return array
52+
*/
53+
public function getArguments()
54+
{
55+
return $this->arguments;
56+
}
57+
}

src/Coduo/PHPMatcher/AST/Node.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Coduo\PHPMatcher\AST;
4+
5+
interface Node
6+
{
7+
8+
}

src/Coduo/PHPMatcher/AST/Pattern.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace Coduo\PHPMatcher\AST;
4+
5+
class Pattern implements Node
6+
{
7+
/**
8+
* @var Type
9+
*/
10+
private $type;
11+
12+
/**
13+
* @var Expander[]|array
14+
*/
15+
private $expanders;
16+
17+
/**
18+
* @param Type $type
19+
*/
20+
public function __construct(Type $type)
21+
{
22+
$this->expanders = array();
23+
$this->type = $type;
24+
}
25+
26+
/**
27+
* @return mixed
28+
*/
29+
public function getType()
30+
{
31+
return $this->type;
32+
}
33+
34+
/**
35+
* @return bool
36+
*/
37+
public function hasExpanders()
38+
{
39+
return (boolean) count($this->expanders);
40+
}
41+
42+
/**
43+
* @return Expander[]|array
44+
*/
45+
public function getExpanders()
46+
{
47+
return $this->expanders;
48+
}
49+
50+
/**
51+
* @param Expander $expander
52+
*/
53+
public function addExpander(Expander $expander)
54+
{
55+
$this->expanders[] = $expander;
56+
}
57+
}

src/Coduo/PHPMatcher/AST/Type.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Coduo\PHPMatcher\AST;
4+
5+
class Type implements Node
6+
{
7+
/**
8+
* @var string
9+
*/
10+
private $type;
11+
12+
/**
13+
* @param string $type
14+
*/
15+
public function __construct($type)
16+
{
17+
$this->type = $type;
18+
}
19+
20+
public function __toString()
21+
{
22+
return $this->type;
23+
}
24+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Coduo\PHPMatcher\Exception;
4+
5+
class Exception extends \Exception
6+
{
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Coduo\PHPMatcher\Exception;
4+
5+
class InvalidExpanderTypeException extends Exception
6+
{
7+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Coduo\PHPMatcher\Exception;
4+
5+
class PatternException extends Exception
6+
{
7+
public static function syntaxError($message, $previous = null)
8+
{
9+
return new self('[Syntax Error] ' . $message, 0, $previous);
10+
}
11+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Coduo\PHPMatcher\Exception;
4+
5+
class UnknownExpanderClassException extends Exception
6+
{
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Coduo\PHPMatcher\Exception;
4+
5+
class UnknownExpanderException extends Exception
6+
{
7+
}

src/Coduo/PHPMatcher/Factory.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
namespace Coduo\PHPMatcher;
34

45
interface Factory

src/Coduo/PHPMatcher/Factory/SimpleFactory.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
namespace Coduo\PHPMatcher\Factory;
44

55
use Coduo\PHPMatcher\Factory;
6+
use Coduo\PHPMatcher\Lexer;
67
use Coduo\PHPMatcher\Matcher;
8+
use Coduo\PHPMatcher\Parser;
79

810
class SimpleFactory implements Factory
911
{
@@ -21,7 +23,7 @@ public function createMatcher()
2123
protected function buildMatchers()
2224
{
2325
$scalarMatchers = $this->buildScalarMatchers();
24-
$arrayMatcher = new Matcher\ArrayMatcher($scalarMatchers);
26+
$arrayMatcher = new Matcher\ArrayMatcher($scalarMatchers, $this->buildParser());
2527

2628
return new Matcher\ChainMatcher(array(
2729
$scalarMatchers,
@@ -35,17 +37,27 @@ protected function buildMatchers()
3537
*/
3638
protected function buildScalarMatchers()
3739
{
40+
$parser = $this->buildParser();
41+
3842
return new Matcher\ChainMatcher(array(
3943
new Matcher\CallbackMatcher(),
4044
new Matcher\ExpressionMatcher(),
4145
new Matcher\NullMatcher(),
42-
new Matcher\StringMatcher(),
43-
new Matcher\IntegerMatcher(),
46+
new Matcher\StringMatcher($parser),
47+
new Matcher\IntegerMatcher($parser),
4448
new Matcher\BooleanMatcher(),
45-
new Matcher\DoubleMatcher(),
49+
new Matcher\DoubleMatcher($parser),
4650
new Matcher\NumberMatcher(),
4751
new Matcher\ScalarMatcher(),
4852
new Matcher\WildcardMatcher()
4953
));
5054
}
55+
56+
/**
57+
* @return Parser
58+
*/
59+
protected function buildParser()
60+
{
61+
return new Parser(new Lexer());
62+
}
5163
}

0 commit comments

Comments
 (0)