Completed
Push — master ( f96168...dfe6c2 )
by Alexpts
04:31
created

ModelClosure::getKey()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 2
eloc 2
nc 2
nop 3
crap 2
1
<?php
2
namespace PTS\DataTransformer;
3
4
class ModelClosure
5
{
6
    /** @var \Closure */
7
    protected $fill;
8
    /** @var \Closure */
9
    protected $data;
10
11
    /**
12
     * @return \Closure
13
     */
14 4
    public function getToDataFn()
15
    {
16 4
        if (!$this->data) {
17 4
            $this->data = $this->createDataFn();
18 4
        }
19
20 4
        return $this->data;
21
    }
22
23
    /**
24
     * @return \Closure
25
     */
26 4
    protected function createDataFn() {
27
        return function (array $mapping, DataTransformer $transformer) {
28 2
            $typeConverter = $transformer->getConverter();
29 2
            $props = [];
30
31 2
            foreach ($mapping as $name => $propRule) {
32 2
                $propRule = new PropRule($propRule);
33 2
                $getter = $propRule->getGet();
34 2
                $propVal = $propRule->getProp();
35 2
                $val = null;
36
37 2
                if ($getter) {
38 2
                    $method = is_array($getter) ? $getter[0] : $getter;
39 2
                    $args = is_array($getter) ? $getter[1] : [];
40 2
                    $val = call_user_func_array([$this, $method], $args);
41 2
                } elseif ($propVal) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $propVal of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
42 2
                    $val = $this->{$propVal};
43 2
                }
44
45 2
                if ($val !== null) {
46 2
                    $props[$name] = $typeConverter->toData($val, $propRule, $transformer);
47 2
                }
48 2
            }
49
50 2
            return $props;
51 4
        };
52
    }
53
54
    /**
55
     * @return \Closure
56
     */
57 4
    public function getFillFn()
58
    {
59 4
        if (!$this->fill) {
60 4
            $this->fill = $this->createFillFn();
61 4
        }
62
63 4
        return $this->fill;
64
    }
65
66
    /**
67
     * @return \Closure
68
     */
69
    protected function createFillFn()
70
    {
71 4
        return function(array $doc, array $mapping, DataTransformer $transformer) {
72 2
            $typeConverter = $transformer->getConverter();
73
74 2
            foreach ($doc as $name => $val) {
75 2
                if (!array_key_exists($name, $mapping)) {
76 1
                    continue;
77
                }
78
79 2
                $propRule = new PropRule($mapping[$name]);
80
81 2
                $modelName = $propRule->getProp($name);
82 2
                $setter = $propRule->getSet();
83
84 2
                $val = $typeConverter->toModel($val, $propRule, $transformer);
85
86
                $setter
87 2
                    ? call_user_func([$this, $setter], $val)
88 2
                    : $this->{$modelName} = $val;
89 2
            }
90 4
        };
91
    }
92
}