Skip to content

Commit 71b098d

Browse files
author
Nico Oelgart
authored
Merge branch 'master' into php7
2 parents 7ffb814 + 8274220 commit 71b098d

File tree

4 files changed

+48
-2
lines changed

4 files changed

+48
-2
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Find me on Twitter: @[nicoSWD](https://twitter.com/nicoSWD)
2424
Via Composer
2525

2626
```bash
27-
$ composer require "nicoswd/php-rule-parser": "0.3.*"
27+
$ composer require nicoswd/php-rule-parser
2828
```
2929

3030
Via git
@@ -71,6 +71,7 @@ $rule = new Rule($ruleStr, $variables);
7171
var_dump($rule->isTrue()); // bool(true)
7272
```
7373

74+
7475
## Custom Functions
7576

7677
```php
@@ -110,7 +111,7 @@ $variables = [
110111
$rule = new Rule($ruleStr, $variables);
111112

112113
$rule->registerToken(Tokenizer::TOKEN_GREATER, '\bis\s+greater\s+than\b');
113-
$rule->registerToken(Tokenizer::TOKEN_VARIABLE, ':\w+');
114+
$rule->registerToken(Tokenizer::TOKEN_VARIABLE, ':[a-zA-Z_][\w-]*');
114115

115116
var_dump($rule->isTrue()); // bool(true)
116117
```

src/nicoSWD/Rules/Parser.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,17 @@ public function assignVariables(array $variables)
121121
}
122122

123123
/**
124+
* @param string $class
125+
* @param string $regex
126+
* @param int $priority
127+
*/
128+
public function registerToken($class, $regex, $priority = null)
129+
{
130+
$this->tokenizer->registerToken($class, $regex, $priority);
131+
}
132+
133+
/**
134+
* @param Tokens\BaseToken $token
124135
* @throws Exceptions\ParserException
125136
*/
126137
protected function assignVariableValueFromToken(BaseToken $token)

src/nicoSWD/Rules/Tokenizer.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ public function __construct()
8383
$this->registerToken(self::TOKEN_UNKNOWN, '.', 5);
8484
}
8585

86+
8687
public function tokenize(string $string) : Stack
8788
{
8889
$stack = new Stack();
@@ -128,6 +129,7 @@ private function getMatchedToken(array $matches) : string
128129
return 'Unknown';
129130
}
130131

132+
131133
private function getRegex() : string
132134
{
133135
if (!$this->regex || $this->regexRequiresReassembly) {
@@ -144,6 +146,7 @@ private function getRegex() : string
144146
return $this->regex;
145147
}
146148

149+
147150
private function getQueue() : SplPriorityQueue
148151
{
149152
$queue = new SplPriorityQueue();
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
/**
4+
* @license http://opensource.org/licenses/mit-license.php MIT
5+
* @link https://github.com/nicoSWD
6+
* @since 0.4.0
7+
* @author Nicolas Oelgart <nico@oelgart.com>
8+
*/
9+
namespace nicoSWD\Rules\tests\operators;
10+
11+
use nicoSWD\Rules\Rule;
12+
use nicoSWD\Rules\Tokenizer;
13+
14+
/**
15+
* Class OperatorsTest
16+
*/
17+
class CustomOperatorsTest extends \AbstractTestBase
18+
{
19+
public function testCustomOperators()
20+
{
21+
$rule = new Rule(':this is greater than :that', [
22+
':this' => 8,
23+
':that' => 7
24+
]);
25+
26+
$rule->registerToken(Tokenizer::TOKEN_GREATER, '\bis\s+greater\s+than\b');
27+
$rule->registerToken(Tokenizer::TOKEN_VARIABLE, ':\w+');
28+
29+
$this->assertTrue($rule->isTrue());
30+
}
31+
}

0 commit comments

Comments
 (0)