1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WebComplete\core\utils\hydrator; |
4
|
|
|
|
5
|
|
|
class Hydrator implements HydratorInterface |
6
|
|
|
{ |
7
|
|
|
|
8
|
|
|
private $reflections = []; |
9
|
|
|
private $reflectionsPropertyList = []; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* create or update object properties with data |
13
|
|
|
* |
14
|
|
|
* @param array $data |
15
|
|
|
* @param $objectOrClass |
16
|
|
|
* @param array|null $map fieldName: propertyName |
17
|
|
|
* |
18
|
|
|
* @return object |
19
|
|
|
* @throws \ReflectionException |
20
|
|
|
*/ |
21
|
|
|
public function hydrate(array $data, $objectOrClass, array $map = null) |
22
|
|
|
{ |
23
|
|
|
if (\is_object($objectOrClass)) { |
24
|
|
|
$object = $objectOrClass; |
25
|
|
|
$reflection = $this->getReflection(\get_class($object)); |
26
|
|
|
} else { |
27
|
|
|
$reflection = $this->getReflection($objectOrClass); |
28
|
|
|
$object = $reflection->newInstanceWithoutConstructor(); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
if (!\is_array($map)) { |
32
|
|
|
$map = \array_keys($data); |
33
|
|
|
$map = \array_combine($map, $map); |
34
|
|
|
} |
35
|
|
|
foreach ($map as $fieldName => $propertyName) { |
36
|
|
|
if ($reflection->hasProperty($propertyName) && isset($data[$fieldName])) { |
37
|
|
|
$property = $reflection->getProperty($propertyName); |
38
|
|
|
$property->setAccessible(true); |
39
|
|
|
$property->setValue($object, $data[$fieldName]); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
return $object; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* extract object properties to data |
48
|
|
|
* |
49
|
|
|
* @param object $object |
50
|
|
|
* @param array $map fieldName: propertyName |
51
|
|
|
* |
52
|
|
|
* @return array |
53
|
|
|
* @throws \ReflectionException |
54
|
|
|
*/ |
55
|
|
|
public function extract($object, array $map = null): array |
56
|
|
|
{ |
57
|
|
|
$result = []; |
58
|
|
|
|
59
|
|
|
$className = \get_class($object); |
60
|
|
|
$reflection = $this->getReflection($className); |
61
|
|
|
|
62
|
|
|
if (!\is_array($map)) { |
63
|
|
|
$propertyList = $this->getReflectionPropertyList($className); |
64
|
|
|
$map = \array_combine($propertyList, $propertyList); |
65
|
|
|
} |
66
|
|
|
foreach ($map as $fieldName => $propertyName) { |
67
|
|
|
if ($reflection->hasProperty($propertyName)) { |
68
|
|
|
$property = $reflection->getProperty($propertyName); |
69
|
|
|
$property->setAccessible(true); |
70
|
|
|
$result[$fieldName] = $property->getValue($object); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $result; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param $className |
79
|
|
|
* @return \ReflectionClass |
80
|
|
|
* @throws \ReflectionException |
81
|
|
|
*/ |
82
|
|
|
protected function getReflection($className): \ReflectionClass |
83
|
|
|
{ |
84
|
|
|
if (!isset($this->reflections[$className])) { |
85
|
|
|
$this->reflections[$className] = new \ReflectionClass($className); |
86
|
|
|
} |
87
|
|
|
return $this->reflections[$className]; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param $className |
92
|
|
|
* @return array |
93
|
|
|
* @throws \ReflectionException |
94
|
|
|
*/ |
95
|
|
|
protected function getReflectionPropertyList($className): array |
96
|
|
|
{ |
97
|
|
|
if (!isset($this->reflectionsPropertyList[$className])) { |
98
|
|
|
$this->reflectionsPropertyList[$className] = []; |
99
|
|
|
foreach ($this->getReflection($className)->getProperties() as $property) { |
100
|
|
|
$this->reflectionsPropertyList[$className][] = $property->getName(); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
return $this->reflectionsPropertyList[$className]; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|