-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
78 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace DMT\Import\Reader\Decorators; | ||
|
||
use Closure; | ||
|
||
class CallbackDecorator implements DecoratorInterface | ||
{ | ||
|
||
private Closure $callback; | ||
|
||
public function __construct(Closure $callback) | ||
{ | ||
$this->callback = $callback; | ||
} | ||
|
||
public function decorate(object $currentRow): object | ||
{ | ||
return call_user_func($this->callback, $currentRow) ?? $currentRow; | ||
} | ||
} |
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,56 @@ | ||
<?php | ||
|
||
namespace DMT\Test\Import\Reader\Decorators; | ||
|
||
use ArrayObject; | ||
use Closure; | ||
use DMT\Import\Reader\Decorators\CallbackDecorator; | ||
use PHPUnit\Framework\TestCase; | ||
use SimpleXMLElement; | ||
|
||
class CallbackDecoratorTest extends TestCase | ||
{ | ||
/** | ||
* @dataProvider provideRow | ||
* | ||
* @param Closure $callback | ||
* @param object $currentRow | ||
* @param object $expected | ||
*/ | ||
public function testDecorate(Closure $callback, object $currentRow, object $expected): void | ||
{ | ||
$decorator = new CallbackDecorator($callback); | ||
|
||
$this->assertEquals($expected, $decorator->decorate($currentRow)); | ||
} | ||
|
||
public function provideRow(): iterable | ||
{ | ||
return [ | ||
'xml' => [ | ||
function (SimpleXMLElement $currentRow) { | ||
$currentRow->addChild('type', (string) $currentRow->car->attributes()['type']); | ||
foreach ($currentRow->xpath('car/@*') as $attribute) { | ||
unset($attribute[0]); | ||
} | ||
}, | ||
simplexml_load_string('<root><car type="focus">ford</car></root>'), | ||
simplexml_load_string('<root><car>ford</car><type>focus</type></root>') | ||
], | ||
'json' => [ | ||
function (object $currentRow) { | ||
$currentRow->lorem = 'ipsum'; | ||
}, | ||
json_decode('{"foo": "bar"}'), | ||
(object) ['foo' => 'bar', 'lorem' => 'ipsum'] | ||
], | ||
'csv (pass through)' => [ | ||
function (object $currentRow) { | ||
$currentRow['col2'] = 'value2'; | ||
}, | ||
new ArrayObject(['col1' => 'value1']), | ||
new ArrayObject(['col1' => 'value1', 'col2' => 'value2']), | ||
], | ||
]; | ||
} | ||
} |