Skip to content

Commit

Permalink
Fixed Walk not evaluating path in current record context.
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul committed Jun 12, 2017
1 parent 9792608 commit 9c5b994
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 17 deletions.
17 changes: 4 additions & 13 deletions src/Strategy/Walk.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
<?php
namespace ScriptFUSION\Mapper\Strategy;

use ScriptFUSION\Mapper\Mapper;
use ScriptFUSION\Mapper\Mapping;

/**
* Walks a nested structure to the specified element in the same manner as Copy.
*/
class Walk extends Delegate
{
/**
* @var Copy
*/
private $copy;
private $path;

/**
* @param Strategy|Mapping|array|mixed $expression Expression to walk.
Expand All @@ -23,18 +19,13 @@ public function __construct($expression, $path)
{
parent::__construct($expression);

$this->copy = new Copy($path);
$this->path = $path;
}

public function __invoke($data, $context = null)
{
return call_user_func($this->copy, parent::__invoke($data, $context), $context);
}

public function setMapper(Mapper $mapper)
{
$this->copy->setMapper($mapper);
$copy = (new Copy($this->delegate($this->path, $data, $context)))->setMapper($this->getMapper());

return parent::setMapper($mapper);
return $copy(parent::__invoke($data, $context), $context);
}
}
15 changes: 11 additions & 4 deletions test/Integration/Mapper/Strategy/WalkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,24 @@
namespace ScriptFUSIONTest\Integration\Mapper\Strategy;

use ScriptFUSION\Mapper\Mapper;
use ScriptFUSION\Mapper\Strategy\Copy;
use ScriptFUSION\Mapper\Strategy\Walk;

final class WalkTest extends \PHPUnit_Framework_TestCase
{
public function testWalk()
public function testWalkFixedPath()
{
/** @var Walk $walk */
$walk = (new Walk(['foo' => ['bar' => 'baz']], 'foo->bar'))
->setMapper(new Mapper)
;
->setMapper(new Mapper);

self::assertSame('baz', $walk([]));
}

public function testWalkStrategyPath()
{
$walk = (new Walk(['bar' => 'baz'], new Copy('foo')))
->setMapper(new Mapper);

self::assertSame('baz', $walk(['foo' => 'bar']));
}
}

0 comments on commit 9c5b994

Please sign in to comment.