1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Darkilliant\ImportBundle\Step; |
6
|
|
|
|
7
|
|
|
use Darkilliant\ImportBundle\Registry\TransformerRegistry; |
8
|
|
|
use Darkilliant\ProcessBundle\State\ProcessState; |
9
|
|
|
use Darkilliant\ProcessBundle\Step\AbstractConfigurableStep; |
10
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
11
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccessor; |
12
|
|
|
|
13
|
|
|
class TransformStep extends AbstractConfigurableStep |
14
|
|
|
{ |
15
|
|
|
/** @var PropertyAccessor */ |
16
|
|
|
private $accessor; |
17
|
|
|
|
18
|
|
|
/** @var TransformerRegistry */ |
19
|
|
|
private $registry; |
20
|
|
|
|
21
|
2 |
|
public function __construct(PropertyAccessor $accessor, TransformerRegistry $registry) |
22
|
|
|
{ |
23
|
2 |
|
$this->accessor = $accessor; |
24
|
2 |
|
$this->registry = $registry; |
25
|
2 |
|
} |
26
|
|
|
|
27
|
1 |
|
public function configureOptionResolver(OptionsResolver $resolver): OptionsResolver |
28
|
|
|
{ |
29
|
1 |
|
$resolver->setRequired('transforms'); |
30
|
|
|
|
31
|
1 |
|
return parent::configureOptionResolver($resolver); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @throws \TypeError |
36
|
|
|
*/ |
37
|
1 |
|
public function execute(ProcessState $state) |
38
|
|
|
{ |
39
|
1 |
|
$transforms = $state->getOptions()['transforms']; |
40
|
1 |
|
$data = $state->getData(); |
41
|
|
|
|
42
|
1 |
|
foreach ($transforms as $transformConfig) { |
43
|
1 |
|
if ($this->accessor->isWritable($data, $transformConfig['target'])) { |
44
|
1 |
|
$transformer = $this->registry->get($transformConfig['type']); |
45
|
|
|
|
46
|
1 |
|
$originalValue = $transformConfig['source'] ?? null; |
47
|
|
|
|
48
|
1 |
|
$transformer->validate($originalValue, $transformConfig['descrition'] ?? $transformConfig['target'], $transformConfig['options'] ?? []); |
49
|
1 |
|
$finalValue = $transformer->transform($originalValue, $transformConfig['descrition'] ?? $transformConfig['target'], $transformConfig['options'] ?? []); |
50
|
|
|
|
51
|
1 |
|
$this->accessor->setValue($data, $transformConfig['target'], $finalValue); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
1 |
|
$state->setData($data); |
56
|
1 |
|
} |
57
|
|
|
} |
58
|
|
|
|