DataTransformer::getConverter()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types = 1);
3
4
namespace PTS\DataTransformer;
5
6
class DataTransformer implements DataTransformerInterface
7
{
8
    /** @var ModelClosure */
9
    protected $fn;
10
    /** @var TypeConverter */
11
    protected $typeConverter;
12
    /** @var MapsManager */
13
    protected $mapsManager;
14
15 10
    public function __construct(TypeConverter $converter, MapsManager $mapsManager, ModelClosure $closuresFn)
16
    {
17 10
        $this->mapsManager = $mapsManager;
18 10
        $this->typeConverter = $converter;
19 10
        $this->fn = $closuresFn;
20 10
    }
21
22 7
    public function getMapsManager(): MapsManager
23
    {
24 7
        return $this->mapsManager;
25
    }
26
27 10
    public function getConverter(): TypeConverter
28
    {
29 10
        return $this->typeConverter;
30
    }
31
32 1
    public function getDataCollection(array $models, string $mapType = 'dto', array $excludeFields = []): array
33
    {
34 1
        $result = [];
35
36 1
        foreach ($models as $i => $model) {
37 1
            $result[$i] = $this->getData($model, $mapType, $excludeFields);
38
        }
39
40 1
        return $result;
41
    }
42
43 4
    public function getData($model, string $mapType = 'dto', array $excludeFields = []): array
44
    {
45 4
        $class = get_class($model);
46 4
        $map = $this->mapsManager->getMap($class, $mapType);
47
48 4
        $fromModelFn = $this->fn->getFromModel();
49 4
        $props = [];
50
51 4
        foreach ($map as $dataKey => $propRule) {
52 4
            if (in_array($dataKey, $excludeFields, true)) {
53 1
                continue;
54
            }
55
56 4
            $propRule = new PropRule($propRule);
57 4
            $val = $fromModelFn->call($model, $propRule->getGetter(), $propRule->getProp($dataKey));
58 4
            if ($val !== null) {
59 4
                $props[$dataKey] = $this->getConverter()->toData($val, $propRule, $this);
60
            }
61
        }
62
63 4
        return $props;
64
    }
65
66 2
    public function fillModel(array $data, $model, string $mapType = 'dto')
67
    {
68 2
        $class = get_class($model);
69 2
        $map = $this->mapsManager->getMap($class, $mapType);
70
71 2
        $typeConverter = $this->getConverter();
72 2
        $setModelFn = $this->fn->setToModel();
73
74 2
        foreach ($data as $name => $val) {
75 2
            if (!array_key_exists($name, $map)) {
76 1
                continue;
77
            }
78
79 2
            $propRule = new PropRule($map[$name]);
80 2
            $val = $typeConverter->toModel($val, $propRule, $this);
81
82 2
            $setModelFn->call($model, $propRule->getSet(), $val, $propRule->getProp($name));
83
        }
84 2
    }
85
86 2
    public function createModel(string $class)
87
    {
88 2
        $emptyModel = new \ReflectionClass($class);
89 2
        return $emptyModel->newInstanceWithoutConstructor();
90
    }
91
}
92