diff --git a/src/Component/Converter/ExplodeItemCollectionLoader.php b/src/Component/Converter/ExplodeItemCollectionLoader.php new file mode 100644 index 00000000..bf775c1b --- /dev/null +++ b/src/Component/Converter/ExplodeItemCollectionLoader.php @@ -0,0 +1,43 @@ +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; + } +} \ No newline at end of file diff --git a/src/Component/Reader/ItemReaderFactory.php b/src/Component/Reader/ItemReaderFactory.php index 52d0e1d2..a2c35a0a 100644 --- a/src/Component/Reader/ItemReaderFactory.php +++ b/src/Component/Reader/ItemReaderFactory.php @@ -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; @@ -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']); diff --git a/tests/Component/Converter/ExplodeItemCollectionLoaderTest.php b/tests/Component/Converter/ExplodeItemCollectionLoaderTest.php new file mode 100644 index 00000000..1aafe01a --- /dev/null +++ b/tests/Component/Converter/ExplodeItemCollectionLoaderTest.php @@ -0,0 +1,47 @@ + '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()); + } +} diff --git a/tests/Component/Modifier/IconvEncodingModifierTest.php b/tests/Component/Modifier/IconvEncodingModifierTest.php index 566e7ec9..45ee3426 100644 --- a/tests/Component/Modifier/IconvEncodingModifierTest.php +++ b/tests/Component/Modifier/IconvEncodingModifierTest.php @@ -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')); } } } \ No newline at end of file