4
4
5
5
use Coduo \PHPMatcher \AST ;
6
6
use Coduo \PHPMatcher \Exception \Exception ;
7
- use Coduo \PHPMatcher \Exception \InvalidExpanderTypeException ;
8
7
use Coduo \PHPMatcher \Exception \PatternException ;
9
- use Coduo \PHPMatcher \Exception \UnknownExpanderClassException ;
10
8
use Coduo \PHPMatcher \Exception \UnknownExpanderException ;
11
9
use Coduo \PHPMatcher \Matcher \Pattern ;
10
+ use Coduo \PHPMatcher \Parser \ExpanderInitializer ;
12
11
13
12
class Parser
14
13
{
@@ -20,24 +19,17 @@ class Parser
20
19
private $ lexer ;
21
20
22
21
/**
23
- * @var array
22
+ * @var ExpanderInitializer
24
23
*/
25
- private $ expanderDefinitions = array (
26
- "startsWith " => "Coduo \\PHPMatcher \\Matcher \\Pattern \\Expander \\StartsWith " ,
27
- "endsWith " => "Coduo \\PHPMatcher \\Matcher \\Pattern \\Expander \\EndsWith " ,
28
- "notEmpty " => "Coduo \\PHPMatcher \\Matcher \\Pattern \\Expander \\NotEmpty " ,
29
- "lowerThan " => "Coduo \\PHPMatcher \\Matcher \\Pattern \\Expander \\LowerThan " ,
30
- "greaterThan " => "Coduo \\PHPMatcher \\Matcher \\Pattern \\Expander \\GreaterThan " ,
31
- "inArray " => "Coduo \\PHPMatcher \\Matcher \\Pattern \\Expander \\InArray " ,
32
- "contains " => "Coduo \\PHPMatcher \\Matcher \\Pattern \\Expander \\Contains "
33
- );
24
+ private $ expanderInitializer ;
34
25
35
26
/**
36
27
* @param Lexer $lexer
37
28
*/
38
- public function __construct (Lexer $ lexer )
29
+ public function __construct (Lexer $ lexer, ExpanderInitializer $ expanderInitializer )
39
30
{
40
31
$ this ->lexer = $ lexer ;
32
+ $ this ->expanderInitializer = $ expanderInitializer ;
41
33
}
42
34
43
35
/**
@@ -64,11 +56,7 @@ public function parse($pattern)
64
56
$ AST = $ this ->getAST ($ pattern );
65
57
$ pattern = new Pattern \TypePattern ((string ) $ AST ->getType ());
66
58
foreach ($ AST ->getExpanders () as $ expander ) {
67
- if (!array_key_exists ($ expander ->getName (), $ this ->expanderDefinitions )) {
68
- throw new UnknownExpanderException (sprintf ("Unknown expander \"%s \"" , $ expander ->getName ()));
69
- }
70
-
71
- $ pattern ->addExpander ($ this ->initializeExpander ($ expander ));
59
+ $ pattern ->addExpander ($ this ->expanderInitializer ->initialize ($ expander ));
72
60
}
73
61
74
62
return $ pattern ;
@@ -84,20 +72,6 @@ public function getAST($pattern)
84
72
return $ this ->getPattern ();
85
73
}
86
74
87
- /**
88
- * @param $expanderName
89
- * @param $expanderFQCN Fully-Qualified Class Name that implements PatternExpander interface
90
- * @throws UnknownExpanderClassException
91
- */
92
- public function addExpanderDefinition ($ expanderName , $ expanderFQCN )
93
- {
94
- if (!class_exists ($ expanderFQCN )) {
95
- throw new UnknownExpanderClassException (sprintf ("Class \"%s \" does not exists. " , $ expanderFQCN ));
96
- }
97
-
98
- $ this ->expanderDefinitions [$ expanderName ] = $ expanderFQCN ;
99
- }
100
-
101
75
/**
102
76
* Create AST root
103
77
*
@@ -222,6 +196,10 @@ private function getNextArgumentValue()
222
196
return $ this ->getArrayArgument ();
223
197
}
224
198
199
+ if ($ this ->lexer ->isNextToken (Lexer::T_EXPANDER_NAME )) {
200
+ return $ this ->getNextExpanderNode ();
201
+ }
202
+
225
203
if (!$ this ->lexer ->isNextTokenAny ($ validArgumentTypes )) {
226
204
$ this ->unexpectedSyntaxError ($ this ->lexer ->lookahead , "string, number, boolean or null argument " );
227
205
}
@@ -341,23 +319,4 @@ private function endOfPattern()
341
319
{
342
320
return is_null ($ this ->lexer ->lookahead );
343
321
}
344
-
345
- /**
346
- * @param AST\Expander $expander
347
- * @throws InvalidExpanderTypeException
348
- * @return Pattern\PatternExpander
349
- */
350
- private function initializeExpander (AST \Expander $ expander )
351
- {
352
- $ reflection = new \ReflectionClass ($ this ->expanderDefinitions [$ expander ->getName ()]);
353
- $ expander = !$ expander ->hasArguments ()
354
- ? $ reflection ->newInstance ()
355
- : $ reflection ->newInstanceArgs ($ expander ->getArguments ());
356
-
357
- if (!$ expander instanceof Pattern \PatternExpander) {
358
- throw new InvalidExpanderTypeException ();
359
- }
360
-
361
- return $ expander ;
362
- }
363
322
}
0 commit comments