forked from Thijzer/csv-validation-header.wip
-
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.
feat: Implement ExplodeItemCollectionLoader to load items based on pr…
…ovided lists.
- Loading branch information
Showing
4 changed files
with
102 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,43 @@ | ||
<?php | ||
|
||
namespace Misery\Component\Converter; | ||
|
||
use Misery\Component\Reader\ItemCollection; | ||
|
||
class ExplodeItemCollectionLoader implements ItemCollectionLoaderInterface | ||
{ | ||
private array $listItemsToLoad; | ||
private array $listItemsToLoop; | ||
|
||
public function __construct($listItemsToLoad, $listItemsToLoop) | ||
{ | ||
$this->listItemsToLoad = $listItemsToLoad; | ||
$this->listItemsToLoop = $listItemsToLoop; | ||
} | ||
|
||
public function load(array $item): ItemCollection | ||
{ | ||
return new ItemCollection($this->convert($item)); | ||
} | ||
|
||
private function convert(array $item): array | ||
{ | ||
$rows = []; | ||
|
||
if (empty($this->listItemsToLoop)) { | ||
// if no list items to loop are provided, we use the keys of the item and exclude the items to load | ||
$this->listItemsToLoop = array_diff(array_keys($item), $this->listItemsToLoad); | ||
} | ||
|
||
foreach ($this->listItemsToLoop as $itemToLoop) { | ||
$rowData = []; | ||
foreach ($this->listItemsToLoad as $itemToLoad) { | ||
$rowData[$itemToLoad] = $item[$itemToLoad] ?? null; | ||
} | ||
$rowData[$itemToLoop] = $item[$itemToLoop] ?? null; | ||
$rows[] = $rowData; | ||
} | ||
|
||
return $rows; | ||
} | ||
} |
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
47 changes: 47 additions & 0 deletions
47
tests/Component/Converter/ExplodeItemCollectionLoaderTest.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,47 @@ | ||
<?php | ||
|
||
namespace Tests\Misery\Component\Converter; | ||
|
||
use Misery\Component\Converter\ExplodeItemCollectionLoader; | ||
use Misery\Component\Reader\ItemCollection; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class ExplodeItemCollectionLoaderTest extends TestCase | ||
{ | ||
public function testLoad() | ||
{ | ||
// Sample input data | ||
$listItemsToLoad = ['item1', 'item2']; | ||
$listItemsToLoop = ['item3', 'item4']; | ||
$item = ['item1' => 'value1', 'item2' => 'value2', 'item3' => 'value3', 'item4' => 'value4']; | ||
|
||
// Construct the loader | ||
$loader = new ExplodeItemCollectionLoader($listItemsToLoad, $listItemsToLoop); | ||
|
||
// Load the item | ||
$loadedCollection = $loader->load($item); | ||
|
||
// Expected output | ||
$expectedRows = [ | ||
['item1' => 'value1', 'item2' => 'value2', 'item3' => 'value3'], | ||
['item1' => 'value1', 'item2' => 'value2', 'item4' => 'value4'], | ||
]; | ||
|
||
// Assert the output | ||
$this->assertInstanceOf(ItemCollection::class, $loadedCollection); | ||
$this->assertEquals($expectedRows, $loadedCollection->getItems()); | ||
|
||
// Test with empty $listItemsToLoop | ||
$loaderWithEmptyListItemsToLoop = new ExplodeItemCollectionLoader([], $listItemsToLoad); | ||
$loadedCollectionWithEmptyListItemsToLoop = $loaderWithEmptyListItemsToLoop->load($item); | ||
|
||
// Expected output when $listItemsToLoop is empty | ||
$expectedRowsWithEmptyListItemsToLoop = [ | ||
['item1' => 'value1'], ['item2' => 'value2'] | ||
]; | ||
|
||
// Assert the output when $listItemsToLoop is empty | ||
$this->assertInstanceOf(ItemCollection::class, $loadedCollectionWithEmptyListItemsToLoop); | ||
$this->assertEquals($expectedRowsWithEmptyListItemsToLoop, $loadedCollectionWithEmptyListItemsToLoop->getItems()); | ||
} | ||
} |
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