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