Skip to content

Commit

Permalink
added: FilterFieldAction, akeneo/product/csv revert option
Browse files Browse the repository at this point in the history
  • Loading branch information
Thijzer committed Oct 18, 2023
1 parent 4c49caf commit 61fa458
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/Component/Action/FilterAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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;
}

Expand Down
55 changes: 55 additions & 0 deletions src/Component/Action/FilterFieldAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace Misery\Component\Action;

use Misery\Component\Common\Options\OptionsInterface;
use Misery\Component\Common\Options\OptionsTrait;

class FilterFieldAction implements OptionsInterface
{
use OptionsTrait;

public const NAME = 'filter_field';

/** @var array */
private $options = [
'fields' => [],
'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;
}
}
6 changes: 6 additions & 0 deletions src/Component/Common/Pipeline/PipelineFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
32 changes: 31 additions & 1 deletion src/Component/Converter/Akeneo/Csv/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Product implements ConverterInterface, RegisteredByNameInterface, OptionsI
private ItemEncoder $encoder;

private $options = [
'container' => 'values',
'attribute_types:list' => null,
'properties' => [
'sku' => [
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit 61fa458

Please sign in to comment.