ReflectionInstantiator::instantiate()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
cc 3
nc 3
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Linio\Component\Input\Instantiator;
6
7
use Doctrine\Common\Inflector\Inflector;
8
9
class ReflectionInstantiator implements InstantiatorInterface
10
{
11
    public function instantiate(string $class, array $data)
12
    {
13
        $object = new $class();
14
        $reflection = new \ReflectionClass($object);
15
16
        foreach ($data as $key => $value) {
17
            $property = $reflection->getProperty(Inflector::camelize($key));
18
            if (!$property->isPublic()) {
19
                $property->setAccessible(true);
20
            }
21
22
            $property->setValue($object, $value);
23
        }
24
25
        return $object;
26
    }
27
}
28