Completed
Push — master ( d59c85...ce3b36 )
by Joachim
17:57 queued 02:59
created

AbstractEntity::hydrate()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 8
nc 4
nop 3
dl 0
loc 17
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace Loevgaard\DandomainFoundation\Entity;
4
5
use Loevgaard\DandomainDateTime\DateTimeImmutable;
6
use Zend\Hydrator\ClassMethods;
7
use Zend\Hydrator\HydratorInterface;
8
9
abstract class AbstractEntity
10
{
11
    /**
12
     * This will hold the conversions applied when calling hydrate
13
     *
14
     * @var array
15
     */
16
    protected $hydrateConversions;
17
18
    /**
19
     * This will hold the conversions applied when calling extract
20
     *
21
     * @var array
22
     */
23
    protected $extractConversions;
24
25
    /**
26
     * @param array $data
27
     * @param bool $useConversions
28
     * @param bool $scalarsOnly If true, it will only hydrate scalars, i.e. floats, integers, strings, and booleans
29
     */
30
    public function hydrate(array $data, bool $useConversions = false, $scalarsOnly = true)
31
    {
32
        $hydrator = $this->getHydrator();
33
34
        if($scalarsOnly) {
35
            foreach ($data as $key => $val) {
36
                if(!is_scalar($val)) {
37
                    unset($data[$key]);
38
                }
39
            }
40
        }
41
42
        if($useConversions && is_array($this->hydrateConversions)) {
43
            $data = $this->convert($data, $this->hydrateConversions);
44
        }
45
46
        $hydrator->hydrate($data, $this);
47
    }
48
49
    public function extract(bool $useConversions = false) : array
50
    {
51
        $hydrator = $this->getHydrator();
52
53
        $data = $hydrator->extract($this);
54
55
        if($useConversions && is_array($this->extractConversions)) {
56
            $data = $this->convert($data, $this->extractConversions);
57
        }
58
59
        return $data;
60
    }
61
62
    protected function convert(array $data, $conversions) : array
63
    {
64
        foreach ($conversions as $from => $to) {
65
            if(isset($data[$to])) {
66
                throw new \InvalidArgumentException('The $to argument in the $data object is already set');
67
            }
68
69
            $tmp = $data[$from] ?? null;
70
            if($tmp) {
71
                unset($data[$from]);
72
                $data[$to] = $tmp;
73
            }
74
        }
75
76
        return $data;
77
    }
78
79
    protected function getDateTimeFromJson($val = null) : ?DateTimeImmutable
80
    {
81
        if (!$val) {
82
            return null;
83
        }
84
85
        if ($val instanceof DateTimeImmutable) {
86
            return $val;
87
        }
88
89
        return DateTimeImmutable::createFromJson($val);
90
    }
91
92
    /**
93
     * @return HydratorInterface
94
     */
95
    protected function getHydrator() : HydratorInterface
96
    {
97
        return new ClassMethods(false);
98
    }
99
}
100