MappingTransformer::transform()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 2
dl 0
loc 9
ccs 6
cts 6
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Darkilliant\ImportBundle\Transformer;
6
7
use Darkilliant\ImportBundle\Registry\TransformerRegistry;
8
9
class MappingTransformer
10
{
11
    /** @var TransformerRegistry */
12
    private $registry;
13
14 1
    public function __construct(TransformerRegistry $registry)
15
    {
16 1
        $this->registry = $registry;
17 1
    }
18
19 1
    public function transform(array $data, array $mapping)
20
    {
21 1
        foreach ($mapping as $key => $config) {
22 1
            $data[$key] = (is_array($config))
23 1
                ? $this->applyTransformer((string) $key, $config['value'], $config['transformers'])
24 1
                : $config;
25
        }
26
27 1
        return $data;
28
    }
29
30 1
    private function applyTransformer(string $name, $value, $transformers)
31
    {
32 1
        foreach ($transformers as $transformerKey => $transformerConfig) {
33 1
            $transformerName = (is_array($transformerConfig)) ? $transformerKey : $transformerConfig;
34 1
            $transformerOptions = (is_array($transformerConfig)) ? $transformerConfig : [];
35
36 1
            $transformer = $this->registry->get($transformerName);
37 1
            $transformer->validate($value, $name, $transformerOptions);
38
39 1
            $value = $transformer->transform($value, $name, $transformerOptions);
40
        }
41
42 1
        return $value;
43
    }
44
}
45