Repository navigation
Expand file tree
/
Copy pathColumnAggregatorTask.php
More file actions
114 lines (94 loc) · 3.4 KB
/
Copy pathColumnAggregatorTask.php
File metadata and controls
114 lines (94 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
declare(strict_types=1);
/*
* This file is part of the CleverAge/ProcessBundle package.
*
* Copyright (c) Clever-Age
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CleverAge\ProcessBundle\Task;
use CleverAge\ProcessBundle\Model\AbstractConfigurableTask;
use CleverAge\ProcessBundle\Model\BlockingTaskInterface;
use CleverAge\ProcessBundle\Model\ProcessState;
use CleverAge\ProcessBundle\Transformer\ConditionTrait;
use Psr\Log\LoggerInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
/**
* @todo @vclavreul describe this task
*/
class ColumnAggregatorTask extends AbstractConfigurableTask implements BlockingTaskInterface
{
use ConditionTrait;
protected array $result = [];
public function __construct(
PropertyAccessorInterface $accessor,
protected LoggerInterface $logger,
) {
$this->accessor = $accessor;
}
public function execute(ProcessState $state): void
{
$input = $state->getInput();
$columns = $this->getOption($state, 'columns');
$conditions = $this->getOption($state, 'condition');
$missingColumns = [];
foreach ($columns as $column) {
$hasColumn = \is_array($input) ? \array_key_exists($column, $input) : isset($input[$column]);
if (!$hasColumn) {
$missingColumns[] = $column;
continue;
}
if ($this->checkCondition([
'input_column_value' => $input[$column],
'input' => $input,
], $conditions)) {
$this->addValueToAggregationGroup(
$column,
$input,
$this->getOption($state, 'reference_key'),
$this->getOption($state, 'aggregation_key')
);
}
}
if ([] !== $missingColumns) {
$colStr = implode(', ', $missingColumns);
$message = "Missing columns [{$colStr}] in input";
if ($this->getOption($state, 'ignore_missing')) {
$this->logger->warning($message);
} else {
throw new \UnexpectedValueException($message);
}
}
}
public function proceed(ProcessState $state): void
{
$state->setOutput($this->result);
}
protected function addValueToAggregationGroup(
mixed $column,
mixed $input,
string $referenceKey,
string $aggregationKey,
): void {
$this->result[$column] ??= [
$referenceKey => $column,
$aggregationKey => [],
];
$this->result[$column][$aggregationKey][] = $input;
}
protected function configureOptions(OptionsResolver $resolver): void
{
$resolver->setRequired('columns');
$resolver->setAllowedTypes('columns', 'array');
$resolver->setDefault('reference_key', 'column');
$resolver->setAllowedTypes('reference_key', 'string');
$resolver->setDefault('aggregation_key', 'values');
$resolver->setAllowedTypes('aggregation_key', 'string');
$this->configureWrappedConditionOptions('condition', $resolver);
$resolver->setDefault('ignore_missing', false);
$resolver->setAllowedTypes('ignore_missing', 'boolean');
}
}