File tree Expand file tree Collapse file tree 4 files changed +48
-2
lines changed Expand file tree Collapse file tree 4 files changed +48
-2
lines changed Original file line number Diff line number Diff line change @@ -24,7 +24,7 @@ Find me on Twitter: @[nicoSWD](https://twitter.com/nicoSWD)
2424Via Composer
2525
2626``` bash
27- $ composer require " nicoswd/php-rule-parser" : " 0.3.* "
27+ $ composer require nicoswd/php-rule-parser
2828```
2929
3030Via git
@@ -71,6 +71,7 @@ $rule = new Rule($ruleStr, $variables);
7171var_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
115116var_dump($rule->isTrue()); // bool(true)
116117```
Original file line number Diff line number Diff 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 )
Original file line number Diff line number Diff 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 ();
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments