44
55use Coduo \PHPMatcher \AST ;
66use Coduo \PHPMatcher \Exception \Exception ;
7- use Coduo \PHPMatcher \Exception \InvalidExpanderTypeException ;
87use Coduo \PHPMatcher \Exception \PatternException ;
9- use Coduo \PHPMatcher \Exception \UnknownExpanderClassException ;
108use Coduo \PHPMatcher \Exception \UnknownExpanderException ;
119use Coduo \PHPMatcher \Matcher \Pattern ;
10+ use Coduo \PHPMatcher \Parser \ExpanderInitializer ;
1211
1312class Parser
1413{
@@ -20,24 +19,17 @@ class Parser
2019 private $ lexer ;
2120
2221 /**
23- * @var array
22+ * @var ExpanderInitializer
2423 */
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 ;
3425
3526 /**
3627 * @param Lexer $lexer
3728 */
38- public function __construct (Lexer $ lexer )
29+ public function __construct (Lexer $ lexer, ExpanderInitializer $ expanderInitializer )
3930 {
4031 $ this ->lexer = $ lexer ;
32+ $ this ->expanderInitializer = $ expanderInitializer ;
4133 }
4234
4335 /**
@@ -64,11 +56,7 @@ public function parse($pattern)
6456 $ AST = $ this ->getAST ($ pattern );
6557 $ pattern = new Pattern \TypePattern ((string ) $ AST ->getType ());
6658 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 ));
7260 }
7361
7462 return $ pattern ;
@@ -84,20 +72,6 @@ public function getAST($pattern)
8472 return $ this ->getPattern ();
8573 }
8674
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-
10175 /**
10276 * Create AST root
10377 *
@@ -222,6 +196,10 @@ private function getNextArgumentValue()
222196 return $ this ->getArrayArgument ();
223197 }
224198
199+ if ($ this ->lexer ->isNextToken (Lexer::T_EXPANDER_NAME )) {
200+ return $ this ->getNextExpanderNode ();
201+ }
202+
225203 if (!$ this ->lexer ->isNextTokenAny ($ validArgumentTypes )) {
226204 $ this ->unexpectedSyntaxError ($ this ->lexer ->lookahead , "string, number, boolean or null argument " );
227205 }
@@ -341,23 +319,4 @@ private function endOfPattern()
341319 {
342320 return is_null ($ this ->lexer ->lookahead );
343321 }
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- }
363322}
0 commit comments