Skip to content

Commit

Permalink
small changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ArekX committed Apr 27, 2019
1 parent 552de23 commit 9355ea7
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 11 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ like from JSON string, PHP files, etc.
These expressions are used to configure the expression parser engine which
runs a value through the rules defined in the array expression to return a result.

## Installation



## Usage

```php
Expand Down Expand Up @@ -38,14 +42,21 @@ foreach ($values as $value) {

### Operators

#### Summary

Following operators are available:

Operator | Name | Definition | Example
-------- | ---- | ---------- | -------
AND | AND operator | `['and', <expression1>, ..., <expressionN>]` | `['and', ['compare', 'name', 'test'], ['compare', 'age', '>', 2]]`
OR | OR operator | `['or', <expression1>, ..., <expressionN>]` | `['or', ['compare', 'name', 'test'], ['compare', 'age', '>', 2]]`
XOR | XOR operator (exclusive OR) | `['xor', <expression1>, ..., <expressionN>]` | `['xor', ['compare', 'name', 'test'], ['compare', 'age', '>', 2]]`
NOT | NOT operator (inverts check) | `['not', <expression>]` | `['not', ['compare', 'name', 'test']]`
COMPARE | Comparison operator | `['compare', 'value']`,<br> `['compare', 'name', 'value']`,<br> `['compare, 'name', '=', 'value']`,<br>`['compare, 'name', '>=', 0, 'default' => 0]` | `['compare', 'age', '>', 2]`
REGEX | Regex operator | `['regex', '/pattern/']`, `['regex', 'name', '/pattern/']` | `['regex', 'name', '/snow/']`

Custom operators can be created and added to the evaluator as needed.


## Tests

Expand Down
28 changes: 20 additions & 8 deletions src/Evaluator.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,9 @@ public static function create($expressionParser = null)
*/
public function run(array $expression, $value)
{
$valueParser = $this->determineValueParser($value);

if ($this->expressionParser === null) {
$this->expressionParser = new DefaultExpressionParser();
}

return $this->expressionParser->parse($expression)->evaluate($valueParser);
return $this->getExpressionParser()
->parse($expression)
->evaluate($this->determineValueParser($value));
}

/**
Expand All @@ -93,7 +89,9 @@ protected function determineValueParser($value)

$valueParser->setRaw($value);
return $valueParser;
} elseif (is_object($value)) {
}

if (is_object($value)) {
if (!($this->valueParser instanceof ObjectValueParser)) {
$valueParser = $this->valueParser = new ObjectValueParser();
}
Expand All @@ -109,4 +107,18 @@ protected function determineValueParser($value)

return $valueParser;
}

/**
* Returns expression parser.
*
* @return ExpressionParser
*/
public function getExpressionParser()
{
if ($this->expressionParser === null) {
$this->expressionParser = new DefaultExpressionParser();
}

return $this->expressionParser;
}
}
4 changes: 2 additions & 2 deletions tests/EvaluateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ class EvaluateTest extends TestCase
public function testCreateInitialInstance()
{
$i = $this->createInstance();
$this->assertEquals(null, $i->getExpressionParser());
$this->assertEquals(null, $i->getCurrentExpressionParser());
}

public function testParserGetsPassedInConstructor()
{
$parser = new ExpressionParser();
$i = $this->createInstance($parser);
$this->assertEquals($parser, $i->getExpressionParser());
$this->assertEquals($parser, $i->getCurrentExpressionParser());
}

public function testEvaluatorRun()
Expand Down
2 changes: 1 addition & 1 deletion tests/Spies/EvaluatorSpy.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

class EvaluatorSpy extends Evaluator
{
public function getExpressionParser()
public function getCurrentExpressionParser()
{
return $this->expressionParser;
}
Expand Down

0 comments on commit 9355ea7

Please sign in to comment.