-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from Minstel/Eval_processor
Evaluate processor
- Loading branch information
Showing
5 changed files
with
133 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters