Skip to content

Commit e8808c4

Browse files
author
Nico Oelgart
authored
Merge pull request #4 from nicoSWD/php7
PHP7-master
2 parents 8274220 + f588876 commit e8808c4

File tree

110 files changed

+738
-1229
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

110 files changed

+738
-1229
lines changed

.travis.yml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
language: php
22

33
php:
4-
- 5.4
5-
- 5.5
6-
- 5.6
7-
- '7.0'
8-
- hhvm
9-
- nightly
4+
- 7.0
5+
- 7.1
106

117
script: phpunit --coverage-clover=coverage.clover
128

README.md

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## Rules Parser and Evaluator for PHP 5.4+
1+
## Rules Parser and Evaluator for PHP 7
22

33
| | Build Status | Code Quality | Coverage | HHVM |
44
|:----------------:|:----------------:|:----------:|:----------:|:----------:|
@@ -8,6 +8,8 @@
88

99
[![Latest Stable Version](https://img.shields.io/packagist/v/nicoswd/php-rule-parser.svg)](https://packagist.org/packages/nicoswd/php-rule-parser)
1010

11+
*Note:* This is a development branch. Use at own risk until tagged stable.
12+
1113
You're looking at a PHP library to parse and evaluate text based rules with a Javascript-like syntax. This project was born out of the necessity to evaluate hundreds of rules that were originally written and evaluated in JavaScript, and now needed to be evaluated on the server-side, using PHP.
1214

1315
This library has initially been used to change and configure the behavior of certain "Workflows" (without changing actual code) in an intranet application, but it may serve a purpose elsewhere.
@@ -69,6 +71,30 @@ $rule = new Rule($ruleStr, $variables);
6971
var_dump($rule->isTrue()); // bool(true)
7072
```
7173

74+
75+
## Custom Functions
76+
77+
```php
78+
$ruleStr = 'double(value) === result';
79+
80+
$variables = [
81+
'value' => 2,
82+
'result' => 4
83+
];
84+
85+
$rule = new Rule($ruleStr, $variables);
86+
87+
$rule->registerFunction('double', function (BaseToken $multiplier) : BaseToken {
88+
if (!$multiplier instanceof TokenInteger) {
89+
throw new \Exception;
90+
}
91+
92+
return new TokenInteger($multiplier->getValue() * 2);
93+
});
94+
95+
var_dump($rule->isTrue()); // bool(true)
96+
```
97+
7298
## Redefine Tokens
7399
Tokens can be customized, if desired. Note that it's very easy to break stuff by doing that, if you have colliding regular expressions.
74100
You may want to set a different `$priority` when registering a token: `::registerToken($type, $regex, $priority)`
@@ -148,8 +174,8 @@ $highlighter = new Rules\Highlighter(new Rules\Tokenizer());
148174

149175
// Optional custom styles
150176
$highlighter->setStyle(
151-
Rules\Constants::GROUP_VARIABLE,
152-
'color: #007694; font-weight: 900;'
177+
Rules\Constants::GROUP_VARIABLE,
178+
'color: #007694; font-weight: 900;'
153179
);
154180

155181
echo $highlighter->highlightString($ruleStr);
@@ -179,7 +205,7 @@ If you discover any security related issues, please email [email protected] inste
179205
## Testing
180206

181207
```bash
182-
$ phpunit
208+
$ composer test
183209
```
184210

185211
## Contributing

composer.json

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "nicoswd/php-rule-parser",
33
"type": "library",
4-
"description": "PHP 5.4 Rule Parser and Evaluator",
4+
"description": "PHP 7 Rule Parser and Evaluator",
55
"keywords": ["rule", "parser", "tokenizer", "highlighter", "evaluator", "javascript", "workflow", "dsl"],
66
"homepage": "https://github.com/nicoSWD/php-rule-parser",
77
"license": "MIT",
@@ -12,12 +12,16 @@
1212
"homepage": "https://nicoswd.com"
1313
}
1414
],
15+
"psr-4": {
16+
"nicoSWD\\Rules": "src/nicoSWD/Rules"
17+
},
1518
"require": {
16-
"php": ">=5.4"
19+
"php": "^7.0"
20+
},
21+
"require-dev": {
22+
"phpunit/phpunit": "^5.1"
1723
},
18-
"autoload": {
19-
"classmap": [
20-
"src/"
21-
]
24+
"scripts": {
25+
"test": "phpunit"
2226
}
2327
}

src/nicoSWD/Rules/AST.php

Lines changed: 41 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,36 @@
33
/**
44
* @license http://opensource.org/licenses/mit-license.php MIT
55
* @link https://github.com/nicoSWD
6-
* @since 0.3.4
76
* @author Nicolas Oelgart <[email protected]>
87
*/
8+
declare(strict_types=1);
9+
910
namespace nicoSWD\Rules;
1011

1112
use Iterator;
12-
use nicoSWD\Rules\Tokens\BaseToken;
13-
use nicoSWD\Rules\Tokens\TokenFactory;
13+
use nicoSWD\Rules\Tokens\{
14+
BaseToken,
15+
TokenFactory,
16+
TokenFunction,
17+
TokenOpeningArray,
18+
TokenRegex,
19+
TokenString,
20+
TokenVariable
21+
};
22+
use nicoSWD\Rules\AST\Nodes\{
23+
NodeArray,
24+
NodeFunction,
25+
NodeString,
26+
NodeVariable
27+
};
1428

15-
/**
16-
* Class AST
17-
* @package nicoSWD\Rules
18-
*/
1929
final class AST implements Iterator
2030
{
31+
/**
32+
* @var Parser
33+
*/
34+
public $parser;
35+
2136
/**
2237
* @var Stack
2338
*/
@@ -29,53 +44,45 @@ final class AST implements Iterator
2944
protected $variables = [];
3045

3146
/**
32-
* @param Stack $stack
33-
* @param mixed[] $variables
47+
* @param Stack $stack
48+
* @param Parser $parser
3449
*/
35-
public function __construct(Stack $stack, array $variables = [])
50+
public function __construct(Stack $stack, Parser $parser)
3651
{
3752
$this->stack = $stack;
38-
$this->variables = $variables;
53+
$this->variables = $parser->variables;
54+
$this->parser = $parser;
3955
}
4056

41-
/**
42-
* @return void
43-
*/
4457
public function next()
4558
{
4659
$this->stack->next();
4760
}
4861

49-
/**
50-
* @return bool
51-
*/
52-
public function valid()
62+
public function valid() : bool
5363
{
5464
return $this->stack->valid();
5565
}
5666

57-
/**
58-
* @return Tokens\BaseToken
59-
*/
6067
public function current()
6168
{
6269
$current = $this->stack->current();
6370

64-
switch (\true) {
71+
switch (true) {
6572
default:
6673
return $current;
67-
case $current instanceof Tokens\TokenString:
68-
case $current instanceof Tokens\TokenRegex:
69-
$current = new AST\Nodes\NodeString($this);
74+
case $current instanceof TokenString:
75+
case $current instanceof TokenRegex:
76+
$current = new NodeString($this);
7077
break;
71-
case $current instanceof Tokens\TokenOpeningArray:
72-
$current = new AST\Nodes\NodeArray($this);
78+
case $current instanceof TokenOpeningArray:
79+
$current = new NodeArray($this);
7380
break;
74-
case $current instanceof Tokens\TokenVariable:
75-
$current = new AST\Nodes\NodeVariable($this);
81+
case $current instanceof TokenVariable:
82+
$current = new NodeVariable($this);
7683
break;
77-
case $current instanceof Tokens\TokenFunction:
78-
$current = new AST\Nodes\NodeFunction($this);
84+
case $current instanceof TokenFunction:
85+
$current = new NodeFunction($this);
7986
break;
8087
}
8188

@@ -84,27 +91,21 @@ public function current()
8491

8592
/**
8693
* @codeCoverageIgnore
87-
* @return mixed
8894
*/
89-
public function key()
95+
public function key() : int
9096
{
9197
return $this->stack->key();
9298
}
9399

94-
/**
95-
* @return void
96-
*/
97100
public function rewind()
98101
{
99102
$this->stack->rewind();
100103
}
101104

102105
/**
103-
* @param string $name
104-
* @return BaseToken
105106
* @throws Exceptions\ParserException
106107
*/
107-
public function getVariable($name)
108+
public function getVariable(string $name) : BaseToken
108109
{
109110
if (!array_key_exists($name, $this->variables)) {
110111
$token = $this->stack->current();
@@ -120,10 +121,7 @@ public function getVariable($name)
120121
return TokenFactory::createFromPHPType($this->variables[$name]);
121122
}
122123

123-
/**
124-
* @return Stack
125-
*/
126-
public function getStack()
124+
public function getStack() : Stack
127125
{
128126
return $this->stack;
129127
}

0 commit comments

Comments
 (0)