Skip to content

Commit

Permalink
Merge pull request #15 from legalthings/merge-fix
Browse files Browse the repository at this point in the history
Fix issue with merge processor
  • Loading branch information
svenstm authored Jul 11, 2016
2 parents c4d9a4d + 2d69297 commit 768131f
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 8 deletions.
45 changes: 37 additions & 8 deletions src/DataEnricher/Processor/Merge.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,56 @@ class Merge implements Processor
*/
public function applyToNode(Node $node)
{
$list = $node->getInstruction($this);
$instruction = $node->getInstruction($this);

$list = $this->resolve($instruction);

$result = $this->merge($list);
$node->setResult($result);
}

/**
* Merge properties of an object
* Resolve processing nodes in the instruction
*
* @param array $merge
* @param array $list
* @return array
*/
protected function merge($merge)
public function resolve($list)
{
$result = (object)[];
if ($list instanceof Node) {
$list = $list->getResult();
}

foreach ($list as &$item) {
if ($item instanceof Node) {
$item = $item->getResult();
}
}

foreach ($merge as $object) {
foreach ($object as $key => $value) {
$result->$key = $value;
return $list;
}

/**
* Merge properties of an object
*
* @param array $list
* @return \stdClass|array
*/
protected function merge(array $list)
{
foreach ($list as &$item) {
if (is_object($item)) {
$item = get_object_vars($item);
}
}

$result = call_user_func_array('array_merge', $list);

// Is associative array
if (array_keys($result) !== array_keys(array_keys($result))) {
$result = (object)$result;
}

return $result;
}
}
97 changes: 97 additions & 0 deletions tests/DataEnricher/Processor/MergeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

namespace LegalThings\DataEnricher\Processor;

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

/**
* @covers LegalThings\DataEnricher\Processor\Merge
*/
class MergeTest extends \PHPUnit_Framework_TestCase
{
public function testApplyToNodeWithObjects()
{
$first = ['foo' => 'red', 'bar' => 'Sir'];
$second = (object)['bird' => 'duck', 'mammal' => 'monkey'];

$processor = new Processor\Merge('<merge>');
$node = $this->getMockBuilder(Node::class)
->disableOriginalConstructor()
->disableProxyingToOriginalMethods()
->getMock();

$node->expects($this->atLeastOnce())
->method('getInstruction')
->with($processor)
->willReturn([$first, $second]);

$node->expects($this->atLeastOnce())
->method('setResult')
->with((object)['foo' => 'red', 'bar' => 'Sir', 'bird' => 'duck', 'mammal' => 'monkey']);

$processor->applyToNode($node);
}

public function testApplyToNodeWithArrays()
{
$first = ['red', 'Sir'];
$second = ['duck', 'monkey'];

$processor = new Processor\Merge('<merge>');
$node = $this->getMockBuilder(Node::class)
->disableOriginalConstructor()
->disableProxyingToOriginalMethods()
->getMock();

$node->expects($this->atLeastOnce())
->method('getInstruction')
->with($processor)
->willReturn([$first, $second]);

$node->expects($this->atLeastOnce())
->method('setResult')
->with(['red', 'Sir', 'duck', 'monkey']);

$processor->applyToNode($node);
}

public function testApplyToNodeWithRefNode()
{
$refNode = $this->getMockBuilder(Node::class)
->disableOriginalConstructor()
->disableProxyingToOriginalMethods()
->getMock();

$data = new \stdClass;
$data->foo = 'red';
$data->bar = 'Sir';

$refNode->expects($this->atLeastOnce())
->method('getResult')
->willReturn($data);

$processor = new Processor\Merge('<merge>');
$node = $this->getMockBuilder(Node::class)
->disableOriginalConstructor()
->disableProxyingToOriginalMethods()
->getMock();

$node->expects($this->atLeastOnce())
->method('getInstruction')
->with($processor)
->willReturn([
$refNode,
(object)[
'bird' => 'duck',
'mammal' => 'monkey'
]
]);

$node->expects($this->atLeastOnce())
->method('setResult')
->with((object)['foo' => 'red', 'bar' => 'Sir', 'bird' => 'duck', 'mammal' => 'monkey']);

$processor->applyToNode($node);
}
}

0 comments on commit 768131f

Please sign in to comment.