Kernel::getClassParser()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Mapper;
6
7
/**
8
 * Class Kernel
9
 * @package Mapper
10
 */
11
class Kernel {
12
13
    /**
14
     * @var array
15
     */
16
    private $data;
17
18
    /**
19
     * @var object
20
     */
21
    private $object;
22
23
    /**
24
     * @var ClassParser
25
     */
26
    private $classParser;
27
28
    /**
29
     * Kernel constructor.
30
     * @param array $data
31
     * @param object $object
32
     */
33 5
    public function __construct(array $data, $object) {
34 5
        $this->setData($data)
35 5
            ->setupClassParser($object)
36 5
            ->setObject($object);
37 5
    }
38
39
    /**
40
     * @return array
41
     */
42 3
    public function getData(): array {
43 3
        return $this->data;
44
    }
45
46
    /**
47
     * @param array $data
48
     * @return Kernel
49
     */
50 5
    public function setData(array $data): Kernel {
51 5
        $this->data = $data;
52
53 5
        return $this;
54
    }
55
56
    /**
57
     * @return object
58
     */
59 5
    public function getObject() {
60 5
        return $this->object;
61
    }
62
63
    /**
64
     * @param object $object
65
     * @return Kernel
66
     */
67 5
    public function setObject($object): Kernel {
68 5
        $this->object = $object;
69
70 5
        $class = get_class($this->getObject());
71 5
        $this->getClassParser()->setClass($class);
72
73 5
        return $this;
74
    }
75
76
    /**
77
     * @param ClassParser $parser
78
     * @return Kernel
79
     */
80 5
    protected function setClassParser(ClassParser $parser): Kernel {
81 5
        $this->classParser = $parser;
82
83 5
        return $this;
84
    }
85
86
    /**
87
     * @return ClassParser
88
     */
89 5
    protected function getClassParser(): ClassParser {
90 5
        return $this->classParser;
91
    }
92
93
    /**
94
     * @param object $object
95
     * @return Kernel
96
     */
97 5
    private function setupClassParser($object): Kernel {
98 5
        $class = get_class($object);
99
100 5
        return $this->setClassParser(new ClassParser($class));
101
    }
102
103
    /**
104
     * @return object
105
     */
106 3
    public function map() {
107 3
        $classFields = $this->getClassParser()->getClassFields();
108
109 3
        foreach ($this->getData() as $field => $value) {
110 3
            $classField = $classFields->getClassField($field);
111
112 3
            if ($classField) {
113 3
                $valueToMap = $this->buildValueToMap($value, $classField);
114
115 3
                if (!is_null($valueToMap)) {
116
                    $this
117 3
                        ->getObject()
118 3
                        ->{$classField->getSetter()}($valueToMap);
119
                }
120
            }
121
        }
122
123 3
        return $this->getObject();
124
    }
125
126
    /**
127
     * @param mixed $value
128
     * @param ClassField $classField
129
     * @return mixed
130
     * @throws \Exception
131
     */
132 3
    private function buildValueToMap($value, ClassField $classField) {
133 3
        $valueToMap = $value;
134
135 3
        if ($this->isArray($value)) {
136 3
            if ($this->isClass($value)) {
137 3
                if ($classField->isClass()) {
138 3
                    $type = $classField->getType();
139
140 3
                    $valueToMap = (new static($value, new $type()))->map();
141
                }
142
            } else {
143 3
                if ($classField->isSequential()) {
144 3
                    $valueToMap = array_map(function($value) use ($classField) {
145 3
                        $type = $classField->getType();
146
147 3
                        return (new static($value, new $type()))->map();
148 3
                    }, $value);
149
                }
150
            }
151
        }
152
153 3
        return $valueToMap;
154
    }
155
156
    /**
157
     * @param mixed $value
158
     * @return bool
159
     */
160 3
    protected function isArray($value): bool {
161 3
        return is_array($value);
162
    }
163
164
    /**
165
     * @param array $value
166
     * @return bool
167
     */
168 3
    protected function isClass(array $value): bool {
169 3
        return !$this->isSequential($value);
170
    }
171
172
    /**
173
     * @param array $value
174
     * @return bool
175
     */
176 3
    protected function isSequential(array $value): bool {
177 3
        return array_keys($value) === range(0, count($value) - 1);
178
    }
179
180
}