Completed
Push — master ( 28dad9...dfe6a1 )
by Joachim
37:07 queued 22:04
created

AbstractEntity   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 33
rs 10
c 0
b 0
f 0
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A hydrate() 0 4 1
A getHydrator() 0 3 1
A getDateTimeFromJson() 0 11 3
A extract() 0 4 1
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
    public function hydrate(array $data)
12
    {
13
        $hydrator = $this->getHydrator();
14
        $hydrator->hydrate($data, $this);
15
    }
16
17
    public function extract() : array
18
    {
19
        $hydrator = $this->getHydrator();
20
        return $hydrator->extract($this);
21
    }
22
23
    protected function getDateTimeFromJson($val = null) : ?DateTimeImmutable
24
    {
25
        if(!$val) {
26
            return null;
27
        }
28
29
        if($val instanceof DateTimeImmutable) {
30
            return $val;
31
        }
32
33
        return DateTimeImmutable::createFromJson($val);
34
    }
35
36
    /**
37
     * @return HydratorInterface
38
     */
39
    protected function getHydrator() : HydratorInterface
40
    {
41
        return new ClassMethods();
42
    }
43
}
44