Skip to content

Commit

Permalink
Merge pull request #23 from legalthings/mustache-template-types
Browse files Browse the repository at this point in the history
Support multiple types in mustache enricher
  • Loading branch information
moesjarraf authored Dec 6, 2016
2 parents a15b0cb + 4fc9aa0 commit a351453
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 1 deletion.
46 changes: 45 additions & 1 deletion src/DataEnricher/Processor/Mustache.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,55 @@ class Mustache implements Processor
public function applyToNode(Node $node)
{
$template = $node->getInstruction($this);
$result = $this->parse($template);

if (!is_string($template) && !is_array($template) && !is_object($template)) {
return trigger_error("Unable to parse given template of type: " . gettype($template), E_WARNING);
}

$result = $this->getParsedResult($template);
$node->setResult($result);
}

/**
* Parse a template by mustache if possible and return the result
*
* @param mixed $template
*
* @return mixed $result
*/
protected function getParsedResult($template)
{
if (is_string($template)) {
return $this->parse($template);
} elseif (is_array($template)) {
return array_map([$this, 'parse'], $template);
} elseif (is_object($template)) {
return $this->parseObject($template);
}

return $template;
}

/**
* Parse an object with mustache
*
* @param object $template
*
* @return object $result
*/
protected function parseObject($template)
{
$result = new \stdClass();

foreach ($template as $key => $value) {
$parsedKey = $this->parse($key);
$parsedValue = $this->getParsedResult($value);
$result->$parsedKey = $parsedValue;
}

return $result;
}

/**
* Parse as mustache template
*
Expand Down
89 changes: 89 additions & 0 deletions tests/DataEnricher/Processor/MustacheTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

namespace LegalThings\DataEnricher\Processor;

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

/**
* @covers LegalThings\DataEnricher\Processor\Mustache
*/
class MustacheTest extends \PHPUnit_Framework_TestCase
{
/**
* @var Processor\Mustache;
*/
protected $processor;

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


public function instructionProvider()
{
return [
[
'hello {{ name }}',
(object)['name' => 'world'],
[],
'hello world'
],
[
['{{ today }}', '{{ tomorrow }}'],
(object)['today' => 'monday', 'tomorrow' => 'tuesday'],
[],
['monday', 'tuesday']
],
[
(object)['{{ key }}' => '{{ value }}'],
(object)['key' => 'name', 'value' => 'John Doe'],
[],
(object)['name' => 'John Doe']
],
[
(object)[
'{{ key }}' => (object)[
'{{ nested_key }}' => '{{ nested_value }}'
],
'ignore_this' => 'okay'
],
(object)['key' => 'organization', 'nested_key' => 'name', 'nested_value' => 'Acme'],
[],
(object)[
'organization' => (object)[
'name' => 'Acme'
],
'ignore_this' => 'okay'
]
]
];
}

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

$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);
}
}

0 comments on commit a351453

Please sign in to comment.