-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathRuleEngineBuilder.php
More file actions
129 lines (112 loc) · 3.27 KB
/
Copy pathRuleEngineBuilder.php
File metadata and controls
129 lines (112 loc) · 3.27 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
/**
* @license http://opensource.org/licenses/mit-license.php MIT
* @link https://github.com/nicoSWD
* @author Nicolas Oelgart <hello@nico.es>
*/
declare(strict_types=1);
namespace nicoSWD\Rule;
use nicoSWD\Rule\AST\AstEvaluator;
use nicoSWD\Rule\Grammar\Grammar;
use nicoSWD\Rule\Grammar\JavaScript\JavaScript;
use nicoSWD\Rule\Lexer\DefaultLexer;
use nicoSWD\Rule\Lexer\Lexer;
use nicoSWD\Rule\Parser\Parser;
use nicoSWD\Rule\TokenStream\FunctionRegistry;
use nicoSWD\Rule\TokenStream\MethodRegistry;
use nicoSWD\Rule\TokenStream\ObjectMethodCallerFactory;
use nicoSWD\Rule\TokenStream\Token\TokenFactory;
use nicoSWD\Rule\TokenStream\VariableRegistry;
/**
* Builder for creating a RuleEngine with custom configuration.
*
* Usage:
* ```php
* $engine = RuleEngine::builder()
* ->withGrammar(new MyCustomGrammar())
* ->withTokenizer(new MyTokenizer())
* ->withDefaultVariables(['env' => 'prod'])
* ->build();
* ```
*/
final class RuleEngineBuilder
{
private ?Grammar $grammar = null;
private ?Lexer $tokenizer = null;
private array $defaultVariables = [];
/**
* Create a new builder instance.
*/
public static function create(): self
{
return new self();
}
/**
* Set a custom grammar for the rule engine.
*
* The grammar defines the syntax rules (operators, keywords, functions, methods).
*
* @param Grammar $grammar
* @return $this
*/
public function withGrammar(Grammar $grammar): self
{
$this->grammar = $grammar;
return $this;
}
/**
* Set a custom tokenizer for the rule engine.
*
* The tokenizer converts a rule string into a stream of tokens.
*
* @param Lexer $tokenizer
* @return $this
*/
public function withTokenizer(Lexer $tokenizer): self
{
$this->tokenizer = $tokenizer;
return $this;
}
/**
* Set default variables that will be available for all rule evaluations.
*
* These variables can be overridden on a per-evaluation basis.
*
* @param array $variables
* @return $this
*/
public function withDefaultVariables(array $variables): self
{
$this->defaultVariables = $variables;
return $this;
}
/**
* Build the RuleEngine with the configured options.
*
* @return RuleEngine
*/
public function build(): RuleEngine
{
$tokenFactory = new TokenFactory();
$grammar = $this->grammar ?? new JavaScript();
$tokenizer = $this->tokenizer ?? new DefaultLexer($grammar, $tokenFactory);
$variableRegistry = new VariableRegistry([], $tokenFactory);
$functionRegistry = new FunctionRegistry($grammar);
$methodRegistry = new MethodRegistry($grammar, $tokenFactory, new ObjectMethodCallerFactory());
$parser = new Parser(
$tokenizer,
);
$astEvaluator = new AstEvaluator(
$variableRegistry,
$functionRegistry,
$methodRegistry,
$tokenFactory,
);
return new RuleEngine(
parser: $parser,
astEvaluator: $astEvaluator,
variableRegistry: $variableRegistry,
defaultVariables: $this->defaultVariables,
);
}
}