-
Notifications
You must be signed in to change notification settings - Fork 86
/
MatcherFactory.php
113 lines (98 loc) · 3.8 KB
/
MatcherFactory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
declare(strict_types=1);
namespace Coduo\PHPMatcher\Factory;
use Coduo\PHPMatcher\Backtrace;
use Coduo\PHPMatcher\Factory;
use Coduo\PHPMatcher\Lexer;
use Coduo\PHPMatcher\Matcher;
use Coduo\PHPMatcher\Parser;
final class MatcherFactory implements Factory
{
public function createMatcher(Backtrace $backtrace) : Matcher
{
return new Matcher($this->buildMatchers($this->buildParser($backtrace), $backtrace), $backtrace);
}
private function buildMatchers(Parser $parser, Backtrace $backtrace) : Matcher\ChainMatcher
{
$scalarMatchers = $this->buildScalarMatchers($parser, $backtrace);
$arrayMatcher = $this->buildArrayMatcher($scalarMatchers, $parser, $backtrace);
// Matchers are registered in order of matching
// 1) all scalars
// 2) json/xml
// 3) array
// 4) or "||"
// 5) full text
$matchers = [$scalarMatchers];
$matchers[] = new Matcher\JsonMatcher($arrayMatcher, $backtrace);
if (\class_exists(\LSS\XML2Array::class)) {
$matchers[] = new Matcher\XmlMatcher($arrayMatcher, $backtrace);
}
$matchers[] = $arrayMatcher;
$matchers[] = $this->buildOrMatcher($backtrace, $matchers);
$matchers[] = new Matcher\TextMatcher($backtrace, $parser);
return new Matcher\ChainMatcher(
'all',
$backtrace,
$matchers
);
}
private function buildArrayMatcher(Matcher\ChainMatcher $scalarMatchers, Parser $parser, Backtrace $backtrace) : Matcher\ArrayMatcher
{
$arrayMatcher = new Matcher\ArrayMatcher(
new Matcher\ChainMatcher(
'array',
$backtrace,
[
new Matcher\OrMatcher($backtrace, $orMatchers = clone $scalarMatchers),
$scalarMatchers,
new Matcher\TextMatcher($backtrace, $parser),
]
),
$backtrace,
$parser
);
$orMatchers->registerMatcher($arrayMatcher);
return $arrayMatcher;
}
private function buildScalarMatchers(Parser $parser, Backtrace $backtrace) : Matcher\ChainMatcher
{
return new Matcher\ChainMatcher(
'scalars',
$backtrace,
[
new Matcher\CallbackMatcher($backtrace),
new Matcher\ExpressionMatcher($backtrace),
new Matcher\NullMatcher($backtrace),
new Matcher\StringMatcher($backtrace, $parser),
new Matcher\IntegerMatcher($backtrace, $parser),
new Matcher\BooleanMatcher($backtrace, $parser),
new Matcher\DoubleMatcher($backtrace, $parser),
new Matcher\NumberMatcher($backtrace, $parser),
new Matcher\TimeMatcher($backtrace, $parser),
new Matcher\DateMatcher($backtrace, $parser),
new Matcher\DateTimeMatcher($backtrace, $parser),
new Matcher\TimeZoneMatcher($backtrace, $parser),
new Matcher\ScalarMatcher($backtrace),
new Matcher\WildcardMatcher($backtrace),
new Matcher\UuidMatcher($backtrace, $parser),
new Matcher\UlidMatcher($backtrace, $parser),
new Matcher\JsonObjectMatcher($backtrace, $parser),
]
);
}
private function buildOrMatcher(Backtrace $backtrace, array $orMatchers) : Matcher\OrMatcher
{
return new Matcher\OrMatcher(
$backtrace,
new Matcher\ChainMatcher(
'or',
$backtrace,
$orMatchers
)
);
}
private function buildParser(Backtrace $backtrace) : Parser
{
return new Parser(new Lexer(), new Parser\ExpanderInitializer($backtrace));
}
}