diff --git a/src/Component/Action/FilterAction.php b/src/Component/Action/FilterAction.php index 3ba20bff..4a6f4580 100644 --- a/src/Component/Action/FilterAction.php +++ b/src/Component/Action/FilterAction.php @@ -4,6 +4,7 @@ use Misery\Component\Common\Options\OptionsInterface; use Misery\Component\Common\Options\OptionsTrait; +use Misery\Component\Converter\Matcher; class FilterAction implements OptionsInterface { @@ -24,7 +25,7 @@ public function apply($item) { $field = $this->getOption('key', $this->getOption('field')); # legacy support $listItem = $item[$field] ?? null; - if (empty($listItem) || empty($field)) { + if (null === $listItem || null === $field) { return $item; } diff --git a/src/Component/Action/FilterFieldAction.php b/src/Component/Action/FilterFieldAction.php new file mode 100644 index 00000000..165e6377 --- /dev/null +++ b/src/Component/Action/FilterFieldAction.php @@ -0,0 +1,55 @@ + [], + 'excludes' => [], + 'starts_with' => null, + 'ends_with' => null, + 'contains' => null, + 'reverse' => false, + ]; + + public function apply($item) + { + $excludes = $this->getOption('excludes'); + $fields = $this->getOption('fields'); + if ($fields !== []) { + foreach ($fields as $field) { + if (false === in_array($field, $excludes)) { + unset($item[$field]); + } + } + return $item; + } + + $startsWith = $this->getOption('starts_with'); + $endsWith = $this->getOption('ends_with'); + $contains = $this->getOption('contains'); + $reverse = $this->getOption('reverse'); + + $fields = array_filter(array_keys($item), function ($field) use ($startsWith, $endsWith, $contains, $reverse) { + return ($startsWith && \str_starts_with($field, $startsWith) === $reverse) || + ($endsWith && \str_ends_with($field, $endsWith) === $reverse) || + ($contains && \str_contains($field, $contains) === $reverse); + }); + + if ($fields !== []) { + $this->setOptions(['fields' => $fields]); + return $this->apply($item); + } + + return $item; + } +} \ No newline at end of file diff --git a/src/Component/Common/Pipeline/PipelineFactory.php b/src/Component/Common/Pipeline/PipelineFactory.php index b252fb7d..59a04689 100644 --- a/src/Component/Common/Pipeline/PipelineFactory.php +++ b/src/Component/Common/Pipeline/PipelineFactory.php @@ -41,6 +41,12 @@ public function createFromConfiguration( case $key === 'output' && isset($configuration['output']['writer']): $writer = $configurationManager->createWriter($configuration['output']['writer']); + + if (isset($configuration['output']['writer']['converter'])) { + $converter = $configurationManager->createConverter($configuration['output']['writer']['converter']); + $pipeline->line(new RevertPipe($converter)); + } + $pipeline->output(new PipeWriter($writer)); $pipeline->invalid( diff --git a/src/Component/Converter/Akeneo/Csv/Product.php b/src/Component/Converter/Akeneo/Csv/Product.php index c67e0771..15a81c78 100644 --- a/src/Component/Converter/Akeneo/Csv/Product.php +++ b/src/Component/Converter/Akeneo/Csv/Product.php @@ -22,6 +22,7 @@ class Product implements ConverterInterface, RegisteredByNameInterface, OptionsI private ItemEncoder $encoder; private $options = [ + 'container' => 'values', 'attribute_types:list' => null, 'properties' => [ 'sku' => [ @@ -122,7 +123,36 @@ public function convert(array $item): array public function revert(array $item): array { - return $item; + $container = $this->getOption('container'); + + $output = []; + $output['sku'] = $item['sku'] ?? $item['identifier'] ?? null; + if (array_key_exists('family', $output)) { + $output['family'] = $item['family']; + } + + foreach ($item as $key => $itemValue) { + $matcher = $itemValue['matcher'] ?? null; + /** @var $matcher Matcher */ + if ($matcher && $matcher->matches($container)) { + unset($itemValue['matcher']); + unset($item[$key]); + if (is_array($itemValue['data']) && array_key_exists('unit', $itemValue['data'])) { + $output[$matcher->getRowKey()] = $itemValue['data']['amount']; + $output[$matcher->getRowKey().'-unit'] = $itemValue['data']['unit']; + continue; + } + if (is_array($itemValue['data'])) { + $output[$matcher->getRowKey()] = implode(',', $itemValue['data']); + continue; + } + if (isset($itemValue['data'])) { + $output[$matcher->getRowKey()] = $itemValue['data']; + } + } + } + + return $output; } public function getName(): string diff --git a/src/bootstrap.php b/src/bootstrap.php index bacfe9bc..82473735 100644 --- a/src/bootstrap.php +++ b/src/bootstrap.php @@ -126,10 +126,12 @@ ->register(Misery\Component\Action\FilterAction::NAME, new Misery\Component\Action\FilterAction()) ->register(Misery\Component\Action\PopAction::NAME, new Misery\Component\Action\PopAction()) ->register(Misery\Component\Action\ColumnValueMapperAction::NAME, new Misery\Component\Action\ColumnValueMapperAction()) + ->register(Misery\Component\Action\FilterFieldAction::NAME, new Misery\Component\Action\FilterFieldAction()) ->register(\Misery\Component\Action\AkeneoValueFormatterAction::NAME, new Misery\Component\Action\AkeneoValueFormatterAction()) ->register(\Misery\Component\Action\ConvergenceAction::NAME, new Misery\Component\Action\ConvergenceAction()) ->register(\Misery\Component\Action\ConverterAction::NAME, new Misery\Component\Action\ConverterAction()) ->register(\Misery\Component\Action\ReverterAction::NAME, new Misery\Component\Action\ReverterAction()) + ; #$statementRegistry = new Misery\Component\Common\Registry\Registry('statement');