-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #95 from gries/nested_item_converter_fixed
Nested array item converter
- Loading branch information
Showing
3 changed files
with
147 additions
and
0 deletions.
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
51 changes: 51 additions & 0 deletions
51
src/Ddeboer/DataImport/ItemConverter/NestedMappingItemConverter.php
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,51 @@ | ||
<?php | ||
namespace Ddeboer\DataImport\ItemConverter; | ||
|
||
use Ddeboer\DataImport\ItemConverter\MappingItemConverter; | ||
|
||
/** | ||
* An item converter that takes an input containing nested arrays from a reader, and returns a modified item based on | ||
* mapped keys. | ||
* | ||
* @author Adam Paterson <hello@adampaterson.co.uk> | ||
*/ | ||
class NestedMappingItemConverter extends MappingItemConverter | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
protected $nestKey; | ||
|
||
/** | ||
* @param array $mappings | ||
* @param string $nestKey | ||
*/ | ||
public function __construct($nestKey, array $mappings = array()) | ||
{ | ||
parent::__construct($mappings); | ||
$this->nestKey = $nestKey; | ||
} | ||
|
||
/** | ||
* @param array $item | ||
* @param string $from | ||
* @param string $to | ||
* @return array | ||
*/ | ||
protected function applyMapping(array $item, $from, $to) | ||
{ | ||
if ($from !== $this->nestKey) { | ||
return parent::applyMapping($item, $from, $to); | ||
} | ||
|
||
foreach ($item[$this->nestKey] as $key => $nestedItem) { | ||
foreach ($to as $nestedFrom => $nestedTo) { | ||
$nestedItem = parent::applyMapping($nestedItem, $nestedFrom, $nestedTo); | ||
} | ||
|
||
$item[$this->nestKey][$key] = $nestedItem; | ||
} | ||
|
||
return $item; | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
tests/Ddeboer/DataImport/Tests/ItemConverter/NestedMappingItemConverterTest.php
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,43 @@ | ||
<?php | ||
|
||
namespace Ddeboer\DataImport\ItemConverter; | ||
|
||
class NestedMappingItemConverterTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testConvertSimple() | ||
{ | ||
$input = array( | ||
'bar' => 'value', | ||
'simple' => array('subsimple' => 'value'), | ||
'simple-key' => array('foo' => 'bar'), | ||
'nested' => array( | ||
array('another' => 'thing', 'something' => 's1'), | ||
array('another' => 'thing2', 'something' => 's2'), | ||
), | ||
); | ||
|
||
$mappings = array( | ||
'bar' => 'foo', | ||
'simple' => array('subsimple' => 'subsimple-foo'), | ||
'simple-key' => 'simple-key-foo', | ||
'nested' => array( | ||
'another' => 'different_thing', | ||
'something' => 'else' | ||
) | ||
); | ||
|
||
$converter = new NestedMappingItemConverter('nested', $mappings); | ||
$output = $converter->convert($input); | ||
|
||
$expected = array( | ||
'foo' => 'value', | ||
'simple' => array('subsimple-foo' => 'value'), | ||
'simple-key-foo' => array('foo' => 'bar'), | ||
'nested' => array( | ||
array('different_thing' => 'thing', 'else' => 's1'), | ||
array('different_thing' => 'thing2', 'else' => 's2'), | ||
) | ||
); | ||
$this->assertEquals($expected, $output); | ||
} | ||
} |