Skip to content

Commit

Permalink
Merge pull request #36 from Minstel/Eval_processor
Browse files Browse the repository at this point in the history
Evaluate processor
  • Loading branch information
jasny committed May 27, 2019
2 parents 880422a + 7529357 commit c8eef9d
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/DataEnricher.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class DataEnricher
public static $defaultProcessors = [
'<ifset>' => Processor\IfSet::class,
'<ref>' => Processor\Reference::class,
'<eval>' => Processor\Evaluate::class,
'<switch>' => Processor\SwitchChoose::class,
'<merge>' => Processor\Merge::class,
'<tpl>' => Processor\Mustache::class,
Expand Down
34 changes: 34 additions & 0 deletions src/DataEnricher/Processor/Evaluate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace LegalThings\DataEnricher\Processor;

use LegalThings\DataEnricher\Node;
use LegalThings\DataEnricher\Processor;
use function JmesPath\search as jmespath_search;

/**
* Process JMESPath compatible expression
* @see http://jmespath.org/
*/
class Evaluate implements Processor
{
use Processor\Implementation,
Helper\GetByReference
{
Helper\GetByReference::withSourceAndTarget insteadof Processor\Implementation;
}

/**
* Apply processing to a single node
*
* @param Node $node
*/
public function applyToNode(Node $node)
{
$instruction = $node->getInstruction($this);

$result = jmespath_search($instruction, $this->source);

$node->setResult($result);
}
}
92 changes: 92 additions & 0 deletions tests/DataEnricher/Processor/EvaluateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

namespace LegalThings\DataEnricher\Processor;

use LegalThings\DataEnricher\Node;
use LegalThings\DataEnricher\Processor;

/**
* @covers LegalThings\DataEnricher\Processor\Evaluate
*/
class EvaluateTest extends \PHPUnit_Framework_TestCase
{
/**
* @var Processor\Reference;
*/
protected $processor;

public function setUp()
{
$this->processor = new Processor\Evaluate('<eval>');
}

public function instructionProvider()
{
return [
[
"foo.bar == 'test'",
(object)['foo' => (object)['bar' => 'test']],
true
],
[
"foo.bar == 'test' && baz.zoo[0] == 'rest'",
(object)[
'foo' => (object)['bar' => 'test'],
'baz' => (object)['zoo' => ['rest']]
],
true
],
[
"foo.bar == 'test' || baz.zoo[0] == 'rest'",
(object)[
'foo' => (object)['bar' => 'test'],
'baz' => (object)['zoo' => [null]]
],
true
],
[
"foo.bar == null",
(object)['foo' => (object)['bar' => null]],
true
],
[
"foo.bar == 'tests'",
(object)['foo' => (object)['bar' => 'test']],
false
],
[
"foo.bar == 'test' && baz.zoo[0] == 'rest'",
(object)[
'foo' => (object)['bar' => 'test'],
'baz' => (object)['zoo' => ['rests']]
],
false
],
];
}

/**
* @dataProvider instructionProvider
*
* @param string|object|array $instruction
* @param object $source
* @param string|object|array $result
*/
public function testApplyToNode($instruction, $source, $result)
{
$processor = $this->processor->withSourceAndTarget($source, []);

$node = $this->createMock(Node::class);

$node->expects($this->atLeastOnce())
->method('getInstruction')
->with($processor)
->willReturn($instruction);

$node->expects($this->atLeastOnce())
->method('setResult')
->with($result);

$processor->applyToNode($node);
}
}
2 changes: 2 additions & 0 deletions tests/DataEnricherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ public function testApplyToIntegration()
],
'amount' => 12345,
'message' => 'I want to go to Amsterdam, Netherlands',
'is_dutch_capital' => true,
'shipping' => 'PostNL',
'profile' => (object) [
'qux' => 12345,
Expand Down Expand Up @@ -270,6 +271,7 @@ public function testApplyToWithSourceIntegration()
],
'amount' => 'foo-bar-cux',
'message' => 'I want to go to foo-city, foo-country',
'is_dutch_capital' => false,
'shipping' => null,
'profile' => (object) [
'qux' => 'foo-bar-cux',
Expand Down
5 changes: 4 additions & 1 deletion tests/_data/example.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
"message": {
"<tpl>": "I want to go to {{ foo.city }}, {{ foo.country }}"
},
"is_dutch_capital": {
"<eval>": "foo.city == 'Amsterdam' && foo.country == 'Netherlands'"
},
"shipping": {
"<switch>": "foo.country",
"USA": "UPS",
Expand All @@ -28,4 +31,4 @@
}
]
}
}
}

0 comments on commit c8eef9d

Please sign in to comment.