Skip to content

Commit 396f593

Browse files
author
Nico Oelgart
committed
Update README.md
1 parent f5dface commit 396f593

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,30 @@ $rule = new Rule($ruleStr, $variables);
6969
var_dump($rule->isTrue()); // bool(true)
7070
```
7171

72+
## Redefine Tokens
73+
Tokens can be customized, if desired. Note that it's very easy to break stuff by doing that, if you have colliding regular expressions.
74+
You may want to set a different `$priority` when registering a token: `::registerToken($type, $regex, $priority)`
75+
(take a look at `Tokenizer::__construct()` for more info).
76+
77+
```php
78+
$ruleStr = ':this is greater than :that';
79+
80+
$variables = [
81+
':this' => 8,
82+
':that' => 7
83+
];
84+
85+
$rule = new Rule($ruleStr, $variables);
86+
87+
$rule->registerToken(Tokenizer::TOKEN_GREATER, '\bis\s+greater\s+than\b');
88+
$rule->registerToken(Tokenizer::TOKEN_VARIABLE, ':\w+');
89+
90+
var_dump($rule->isTrue()); // bool(true)
91+
```
92+
93+
Also note that the original tokens will no longer be recognized after overwriting them. Thus, if you want to implement aliases
94+
for custom tokens, you have to group them into one regular expression: `(?:\b(?:is\s+)?greater\s+than\b|>)`
95+
7296
## Error Handling
7397
Both, `$rule->isTrue()` and `$rule->isFalse()` will throw an exception if the syntax is invalid. These calls can either be placed inside a `try` / `catch` block, or it can be checked prior using `$rule->isValid()`.
7498

0 commit comments

Comments
 (0)