Skip to content

Commit f7372d5

Browse files
committed
feat: new Iterator processor
1 parent 3026ec0 commit f7372d5

File tree

7 files changed

+129
-7
lines changed

7 files changed

+129
-7
lines changed

src/bundle/Resources/config/workflow/component.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,9 @@ services:
1616
tags:
1717
- { name: almaviacx.import_export.component, alias: processor.aggregator}
1818

19+
AlmaviaCX\Bundle\IbexaImportExport\Processor\Iterator\IteratorProcessor:
20+
arguments:
21+
$sourceResolver: '@AlmaviaCX\Bundle\IbexaImportExport\Item\Transformer\SourceResolver'
22+
tags:
23+
- { name: almaviacx.import_export.component, alias: processor.iterator}
24+

src/lib/Component/ComponentBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function __invoke(
2727
if ($runtimeProcessConfiguration) {
2828
$options->merge($runtimeProcessConfiguration);
2929
}
30-
$options->replaceComponentReferences($this);
30+
$options->replaceComponentReferences($this, $runtimeProcessConfiguration);
3131
$component->setOptions(
3232
$options
3333
);

src/lib/Component/ComponentOptions.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,11 @@ public function merge(ComponentOptions $overrideOptions): ComponentOptions
8686

8787
/**
8888
* @param callable(ComponentReference $componentReference): ComponentInterface $buildComponentCallback
89+
* @param ?ComponentOptions $runtimeProcessConfiguration
8990
*/
90-
public function replaceComponentReferences($buildComponentCallback): void
91-
{
91+
public function replaceComponentReferences(
92+
$buildComponentCallback,
93+
?ComponentOptions $runtimeProcessConfiguration = null
94+
): void {
9295
}
9396
}

src/lib/Item/ValueTransformer/Utils/SlugTransformer.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,18 @@ public function __construct(SlugConverter $slugConverter)
1818

1919
/**
2020
* @param string $value
21-
*
22-
* @return string
2321
*/
2422
public function transform($value, array $options = [])
2523
{
24+
if (is_array($value)) {
25+
$values = [];
26+
foreach ($value as $string) {
27+
$values[] = $this->slugConverter->convert($string);
28+
}
29+
30+
return $values;
31+
}
32+
2633
return $this->slugConverter->convert($value);
2734
}
2835
}

src/lib/Processor/Aggregator/ProcessorAggregatorOptions.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@ public function merge(ComponentOptions $overrideOptions): ComponentOptions
2727
/**
2828
* {@inheritDoc}
2929
*/
30-
public function replaceComponentReferences($buildComponentCallback): void
31-
{
30+
public function replaceComponentReferences(
31+
$buildComponentCallback,
32+
?ComponentOptions $runtimeProcessConfiguration = null
33+
): void {
3234
foreach ($this->processors as $key => $processor) {
3335
$this->processors[$key] = call_user_func(
3436
$buildComponentCallback,
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace AlmaviaCX\Bundle\IbexaImportExport\Processor\Iterator;
6+
7+
use AlmaviaCX\Bundle\IbexaImportExport\Item\Transformer\SourceResolver;
8+
use AlmaviaCX\Bundle\IbexaImportExport\Monolog\WorkflowLoggerInterface;
9+
use AlmaviaCX\Bundle\IbexaImportExport\Processor\AbstractProcessor;
10+
use AlmaviaCX\Bundle\IbexaImportExport\Processor\ProcessorInterface;
11+
use Symfony\Component\Translation\TranslatableMessage;
12+
13+
class IteratorProcessor extends AbstractProcessor implements ProcessorInterface
14+
{
15+
protected SourceResolver $sourceResolver;
16+
17+
public function __construct(
18+
SourceResolver $sourceResolver
19+
) {
20+
$this->sourceResolver = $sourceResolver;
21+
}
22+
23+
public function processItem($item)
24+
{
25+
/** @var ProcessorInterface $processor */
26+
$processor = $this->getOption('processor', []);
27+
$source = $this->getOption('value', []);
28+
29+
$values = ($this->sourceResolver)($source, $item);
30+
if (!is_array($values)) {
31+
$values = [$values];
32+
}
33+
34+
foreach ($values as $value) {
35+
($processor)(
36+
[
37+
'iteration_value' => $value,
38+
'item' => $item,
39+
]
40+
);
41+
}
42+
43+
return $item;
44+
}
45+
46+
public static function getName()
47+
{
48+
return new TranslatableMessage(/* @Desc("Iterator") */ 'processor.iterator.name');
49+
}
50+
51+
public static function getOptionsType(): ?string
52+
{
53+
return IteratorProcessorOptions::class;
54+
}
55+
56+
public function setLogger(WorkflowLoggerInterface $logger): void
57+
{
58+
parent::setLogger($logger);
59+
$processor = $this->getOption('processor', []);
60+
$processor->setLogger($logger);
61+
}
62+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace AlmaviaCX\Bundle\IbexaImportExport\Processor\Iterator;
6+
7+
use AlmaviaCX\Bundle\IbexaImportExport\Component\ComponentOptions;
8+
use AlmaviaCX\Bundle\IbexaImportExport\Component\ComponentReference;
9+
use AlmaviaCX\Bundle\IbexaImportExport\Processor\ProcessorInterface;
10+
use AlmaviaCX\Bundle\IbexaImportExport\Processor\ProcessorOptions;
11+
12+
/**
13+
* @property $value string|\AlmaviaCX\Bundle\IbexaImportExport\Item\Transformer\Source
14+
*/
15+
class IteratorProcessorOptions extends ProcessorOptions
16+
{
17+
protected ComponentReference|ProcessorInterface $processor;
18+
19+
/** @var string|\AlmaviaCX\Bundle\IbexaImportExport\Item\Transformer\Source */
20+
protected $value;
21+
22+
public function setProcessor(string $class, ?ComponentOptions $options = null): void
23+
{
24+
$this->processor = new ComponentReference($class, $options);
25+
}
26+
27+
/**
28+
* @param callable $buildComponentCallback
29+
* @param \AlmaviaCX\Bundle\IbexaImportExport\Reader\InputAwareReaderOptions|null $runtimeProcessConfiguration
30+
*/
31+
public function replaceComponentReferences(
32+
$buildComponentCallback,
33+
?ComponentOptions $runtimeProcessConfiguration = null
34+
): void {
35+
$options = $runtimeProcessConfiguration->processor ?? null;
36+
$this->processor = call_user_func(
37+
$buildComponentCallback,
38+
$this->processor,
39+
$options
40+
);
41+
}
42+
}

0 commit comments

Comments
 (0)