ObjectFactory::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 3
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace WebComplete\core\factory;
4
5
use WebComplete\core\utils\container\ContainerInterface;
6
use WebComplete\core\utils\hydrator\HydratorInterface;
7
8
/**
9
 * Class ObjectFactory
10
 */
11
class ObjectFactory extends AbstractFactory
12
{
13
14
    /**
15
     * @var string
16
     */
17
    protected $objectClass;
18
19
    /**
20
     * @var HydratorInterface
21
     */
22
    protected $hydrator;
23
24
    /**
25
     * @param ContainerInterface $container
26
     * @param HydratorInterface $hydrator
27
     * @param string|null $objectClass
28
     */
29
    public function __construct(
30
        ContainerInterface $container,
31
        HydratorInterface $hydrator,
32
        string $objectClass = null
33
    ) {
34
        parent::__construct($container);
35
        if ($objectClass !== null) {
36
            $this->objectClass = $objectClass;
37
        }
38
        $this->hydrator = $hydrator;
39
    }
40
41
    /**
42
     * @return object
43
     */
44
    public function create()
45
    {
46
        return $this->make($this->objectClass);
47
    }
48
49
    /**
50
     * @param array $data
51
     * @param array|null $map
52
     *
53
     * @return object
54
     */
55
    public function createFromData(array $data, array $map = null)
56
    {
57
        $object = $this->create();
58
        $this->hydrator->hydrate($data, $object, $map);
59
        return $object;
60
    }
61
}
62