Skip to content

Commit 9c5b994

Browse files
author
Paul
committed
Fixed Walk not evaluating path in current record context.
1 parent 9792608 commit 9c5b994

File tree

2 files changed

+15
-17
lines changed

2 files changed

+15
-17
lines changed

src/Strategy/Walk.php

+4-13
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
<?php
22
namespace ScriptFUSION\Mapper\Strategy;
33

4-
use ScriptFUSION\Mapper\Mapper;
54
use ScriptFUSION\Mapper\Mapping;
65

76
/**
87
* Walks a nested structure to the specified element in the same manner as Copy.
98
*/
109
class Walk extends Delegate
1110
{
12-
/**
13-
* @var Copy
14-
*/
15-
private $copy;
11+
private $path;
1612

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

26-
$this->copy = new Copy($path);
22+
$this->path = $path;
2723
}
2824

2925
public function __invoke($data, $context = null)
3026
{
31-
return call_user_func($this->copy, parent::__invoke($data, $context), $context);
32-
}
33-
34-
public function setMapper(Mapper $mapper)
35-
{
36-
$this->copy->setMapper($mapper);
27+
$copy = (new Copy($this->delegate($this->path, $data, $context)))->setMapper($this->getMapper());
3728

38-
return parent::setMapper($mapper);
29+
return $copy(parent::__invoke($data, $context), $context);
3930
}
4031
}

test/Integration/Mapper/Strategy/WalkTest.php

+11-4
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,24 @@
22
namespace ScriptFUSIONTest\Integration\Mapper\Strategy;
33

44
use ScriptFUSION\Mapper\Mapper;
5+
use ScriptFUSION\Mapper\Strategy\Copy;
56
use ScriptFUSION\Mapper\Strategy\Walk;
67

78
final class WalkTest extends \PHPUnit_Framework_TestCase
89
{
9-
public function testWalk()
10+
public function testWalkFixedPath()
1011
{
11-
/** @var Walk $walk */
1212
$walk = (new Walk(['foo' => ['bar' => 'baz']], 'foo->bar'))
13-
->setMapper(new Mapper)
14-
;
13+
->setMapper(new Mapper);
1514

1615
self::assertSame('baz', $walk([]));
1716
}
17+
18+
public function testWalkStrategyPath()
19+
{
20+
$walk = (new Walk(['bar' => 'baz'], new Copy('foo')))
21+
->setMapper(new Mapper);
22+
23+
self::assertSame('baz', $walk(['foo' => 'bar']));
24+
}
1825
}

0 commit comments

Comments
 (0)