|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Coduo\PHPMatcher\Parser; |
|
4
|
|
|
|
|
5
|
|
|
use Coduo\PHPMatcher\AST\Expander; |
|
6
|
|
|
use Coduo\PHPMatcher\Exception\InvalidArgumentException; |
|
7
|
|
|
use Coduo\PHPMatcher\Exception\InvalidExpanderTypeException; |
|
8
|
|
|
use Coduo\PHPMatcher\Exception\UnknownExpanderClassException; |
|
9
|
|
|
use Coduo\PHPMatcher\Exception\UnknownExpanderException; |
|
10
|
|
|
use Coduo\PHPMatcher\Matcher\Pattern\PatternExpander; |
|
11
|
|
|
|
|
12
|
|
|
final class ExpanderInitializer |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var array |
|
16
|
|
|
*/ |
|
17
|
|
|
private $expanderDefinitions = array( |
|
18
|
|
|
"startsWith" => "Coduo\\PHPMatcher\\Matcher\\Pattern\\Expander\\StartsWith", |
|
19
|
|
|
"endsWith" => "Coduo\\PHPMatcher\\Matcher\\Pattern\\Expander\\EndsWith", |
|
20
|
|
|
"isNotEmpty" => "Coduo\\PHPMatcher\\Matcher\\Pattern\\Expander\\IsNotEmpty", |
|
21
|
|
|
"isEmpty" => "Coduo\\PHPMatcher\\Matcher\\Pattern\\Expander\\IsEmpty", |
|
22
|
|
|
"isDateTime" => "Coduo\\PHPMatcher\\Matcher\\Pattern\\Expander\\IsDateTime", |
|
23
|
|
|
"isEmail" => "Coduo\\PHPMatcher\\Matcher\\Pattern\\Expander\\IsEmail", |
|
24
|
|
|
"isUrl" => "Coduo\\PHPMatcher\\Matcher\\Pattern\\Expander\\IsUrl", |
|
25
|
|
|
"lowerThan" => "Coduo\\PHPMatcher\\Matcher\\Pattern\\Expander\\LowerThan", |
|
26
|
|
|
"greaterThan" => "Coduo\\PHPMatcher\\Matcher\\Pattern\\Expander\\GreaterThan", |
|
27
|
|
|
"inArray" => "Coduo\\PHPMatcher\\Matcher\\Pattern\\Expander\\InArray", |
|
28
|
|
|
"count" => "Coduo\\PHPMatcher\\Matcher\\Pattern\\Expander\\Count", |
|
29
|
|
|
"contains" => "Coduo\\PHPMatcher\\Matcher\\Pattern\\Expander\\Contains", |
|
30
|
|
|
"matchRegex" => "Coduo\\PHPMatcher\\Matcher\\Pattern\\Expander\\MatchRegex", |
|
31
|
|
|
|
|
32
|
|
|
"oneOf" => "Coduo\\PHPMatcher\\Matcher\\Pattern\\Expander\\OneOf" |
|
33
|
|
|
); |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param string $expanderName |
|
37
|
|
|
* @param string $expanderFQCN Fully-Qualified Class Name that implements PatternExpander interface |
|
38
|
|
|
* @throws UnknownExpanderClassException |
|
39
|
|
|
*/ |
|
40
|
|
|
public function setExpanderDefinition($expanderName, $expanderFQCN) |
|
41
|
|
|
{ |
|
42
|
|
|
if (!class_exists($expanderFQCN)) { |
|
43
|
|
|
throw new UnknownExpanderClassException(sprintf("Class \"%s\" does not exists.", $expanderFQCN)); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
$this->expanderDefinitions[$expanderName] = $expanderFQCN; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param $expanderName |
|
51
|
|
|
* @return bool |
|
52
|
|
|
*/ |
|
53
|
|
|
public function hasExpanderDefinition($expanderName) |
|
54
|
|
|
{ |
|
55
|
|
|
return array_key_exists($expanderName, $this->expanderDefinitions); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param $expanderName |
|
60
|
|
|
* @return string |
|
61
|
|
|
* @throws InvalidArgumentException |
|
62
|
|
|
*/ |
|
63
|
|
|
public function getExpanderDefinition($expanderName) |
|
64
|
|
|
{ |
|
65
|
|
|
if (!$this->hasExpanderDefinition($expanderName)) { |
|
66
|
|
|
throw new InvalidArgumentException(sprintf("Definition for \"%s\" expander does not exists.", $expanderName)); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
return $this->expanderDefinitions[$expanderName]; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param Expander $expanderNode |
|
74
|
|
|
* @throws InvalidExpanderTypeException |
|
75
|
|
|
* @throws UnknownExpanderException |
|
76
|
|
|
* @return PatternExpander |
|
77
|
|
|
*/ |
|
78
|
|
|
public function initialize(Expander $expanderNode) |
|
79
|
|
|
{ |
|
80
|
|
|
if (!array_key_exists($expanderNode->getName(), $this->expanderDefinitions)) { |
|
81
|
|
|
throw new UnknownExpanderException(sprintf("Unknown expander \"%s\"", $expanderNode->getName())); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
$reflection = new \ReflectionClass($this->expanderDefinitions[$expanderNode->getName()]); |
|
85
|
|
|
|
|
86
|
|
|
if ($expanderNode->hasArguments()) { |
|
87
|
|
|
$arguments = array(); |
|
88
|
|
|
foreach ($expanderNode->getArguments() as $argument) { |
|
89
|
|
|
$arguments[] = ($argument instanceof Expander) |
|
90
|
|
|
? $this->initialize($argument) |
|
91
|
|
|
: $argument; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
$expander = $reflection->newInstanceArgs($arguments); |
|
95
|
|
|
} else { |
|
96
|
|
|
$expander = $reflection->newInstance(); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
if (!$expander instanceof PatternExpander) { |
|
100
|
|
|
throw new InvalidExpanderTypeException(); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
return $expander; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|