Skip to content

Commit

Permalink
feat: Implement ExplodeItemCollectionLoader to load items based on pr…
Browse files Browse the repository at this point in the history
…ovided lists.
  • Loading branch information
gauquier committed Mar 12, 2024
1 parent 4244f44 commit 97c6456
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 1 deletion.
43 changes: 43 additions & 0 deletions src/Component/Converter/ExplodeItemCollectionLoader.php
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;
}
}
11 changes: 11 additions & 0 deletions src/Component/Reader/ItemReaderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
use Misery\Component\Common\Cursor\CondensedCursor;
use Misery\Component\Common\Cursor\CursorInterface;
use Misery\Component\Common\Cursor\SubFunctionalCollectionCursor;
use Misery\Component\Common\Cursor\SubItemCursor;
use Misery\Component\Common\Functions\ArrayFunctions;
use Misery\Component\Common\Registry\RegisteredByNameInterface;
use Misery\Component\Configurator\Configuration;
use Misery\Component\Configurator\ConfigurationManager;
use Misery\Component\Converter\ExplodeItemCollectionLoader;
use Misery\Component\Filter\ItemSortFilter;
use Misery\Component\Filter\ItemTreeSortFilter;

Expand Down Expand Up @@ -60,6 +62,15 @@ public function createFromConfiguration(CursorInterface $cursor, array $configur
return new ItemReader(new CondensedCursor($cursor, $config));
}

if (isset($configuration['x_filter']['type']) && $configuration['x_filter']['type'] === 'loader') {
if (isset($configuration['x_filter']['explode_list'])) {
$configuration['x_filter']['explode_list'] = $configurationObject->getList($configuration['x_filter']['explode_list']);
}

$config = $configuration['x_filter'];
return new ItemReader(new SubItemCursor($cursor, new ExplodeItemCollectionLoader($config['load_list'] ?? [], $config['explode_list'])));
}

$reader = new ItemReader($cursor);
if (isset($configuration['filter'])) {
$reader = $reader->find($configuration['filter']);
Expand Down
47 changes: 47 additions & 0 deletions tests/Component/Converter/ExplodeItemCollectionLoaderTest.php
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());
}
}
2 changes: 1 addition & 1 deletion tests/Component/Modifier/IconvEncodingModifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function test_it_should_encode_values_with_iconv(): void
if ($modifier->supports()) {
$modifier->setOptions(['out_charset' => 'ascii//TRANSLIT']);

$this->assertEquals('Foo Bar', $modifier->modify('Fóø Bår'));
$this->assertEquals('F?? B?r', $modifier->modify('Fóø Bår'));
}
}
}

0 comments on commit 97c6456

Please sign in to comment.