Completed
Push — master ( 07d150...58a98c )
by Alexpts
02:24
created

ModelClosure::createDataFn()   D

Complexity

Conditions 9
Paths 1

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 9.0118

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 26
ccs 18
cts 19
cp 0.9474
rs 4.9091
cc 9
eloc 17
nc 1
nop 0
crap 9.0118
1
<?php
2
namespace PTS\DataTransformer;
3
4
class ModelClosure
5
{
6
    /** @var \Closure */
7
    protected $fillFn;
8
    /** @var \Closure */
9
    protected $dataFn;
10
11
    /**
12
     * @return \Closure
13
     */
14 4
    public function getToDataFn()
15
    {
16 4
        if (!$this->dataFn) {
17 4
            $this->dataFn = $this->createDataFn();
18 4
        }
19
20 4
        return $this->dataFn;
21
    }
22
23
    /**
24
     * @return \Closure
25
     */
26 3
    public function getFillFn()
27
    {
28 3
        if (!$this->fillFn) {
29 3
            $this->fillFn = $this->createFillFn();
30 3
        }
31
32 3
        return $this->fillFn;
33
    }
34
35
    /**
36
     * @return \Closure
37
     */
38 4
    protected function createDataFn() {
39
        return function (array $mapping, TypeConverter $typeConverter) {
40 2
            $props = [];
41
42 2
            foreach ($mapping as $name => $prop) {
43 2
                $getter = array_key_exists('get', $prop) ? $prop['get'] : null;
44 2
                $propVal = array_key_exists('prop', $prop) ? $prop['prop'] : null;
45
46 2
                if ($getter) {
47 2
                    $method = is_array($getter) ? $getter[0] : $getter;
48 2
                    $args = is_array($getter) ? $getter[1] : [];
49 2
                    $val = call_user_func_array([$this, $method], $args);
50 2
                } elseif ($propVal) {
51 2
                    $val = $this->{$propVal};
52 2
                } else {
53
                    throw new \InvalidArgumentException('Bad mapping config');
54
                }
55
56 2
                if ($val !== null) {
57 2
                    $props[$name] = $typeConverter->toStorage($val, $prop);
58 2
                }
59 2
            }
60
61 1
            return $props;
62 4
        };
63
    }
64
65
    /**
66
     * @return \Closure
67
     */
68
    protected function createFillFn()
69
    {
70 3
        return function(array $doc, array $mapping, TypeConverter $typeConverter) {
71 1
            foreach ($doc as $name => $val) {
72 1
                if (!array_key_exists($name, $mapping)) {
73 1
                    continue;
74
                }
75
76 1
                $prop = $mapping[$name];
77
78 1
                $modelName = array_key_exists('prop', $prop) ? $prop['prop'] : $name;
79 1
                $setter = array_key_exists('set', $prop) ? $prop['set'] : null;
80
81 1
                $val = $typeConverter->toModel($val, $prop);
82
83
                $setter
84 1
                    ? call_user_func([$this, $setter], $val)
85 1
                    : $this->{$modelName} = $val;
86 1
            }
87 3
        };
88
    }
89
}