forked from samdark/hydrator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hydrator.php
129 lines (113 loc) · 3.53 KB
/
Hydrator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
namespace samdark\hydrator;
/**
* Hydrator can be used for two purposes:
*
* - To extract data from a class to be futher stored in a persistent storage.
* - To instantiate a class having its data.
*
* In both cases it is saving and filling protected and private properties without calling
* any methods which leads to ability to persist state of an object with properly incapsulated
* data.
*/
class Hydrator
{
/**
* Mapping of keys in data array to property names.
* @var array
*/
private $map;
/**
* Local cache of reflection class instances
* @var array
*/
private $reflectionClassMap = [];
public function __construct(array $map)
{
$this->map = $map;
}
/**
* Creates an instance of a class filled with data according to map
*
* @param array $data
* @param string $className
* @return object
*
* @since 1.0.2
*/
public function hydrate($data, $className)
{
$reflection = $this->getReflectionClass($className);
$object = $reflection->newInstanceWithoutConstructor();
foreach ($this->map as $dataKey => $propertyName) {
if (!$reflection->hasProperty($propertyName)) {
throw new \InvalidArgumentException("There's no $propertyName property in $className.");
}
if (isset($data[$dataKey])) {
$property = $reflection->getProperty($propertyName);
$property->setAccessible(true);
$property->setValue($object, $data[$dataKey]);
}
}
return $object;
}
/**
* Fills an object passed with data according to map
*
* @param array $data
* @param object $object
* @return object
*
* @since 1.0.2
*/
public function hydrateInto($data, $object)
{
$className = get_class($object);
$reflection = $this->getReflectionClass($className);
foreach ($this->map as $dataKey => $propertyName) {
if (!$reflection->hasProperty($propertyName)) {
throw new \InvalidArgumentException("There's no $propertyName property in $className.");
}
if (isset($data[$dataKey])) {
$property = $reflection->getProperty($propertyName);
$property->setAccessible(true);
$property->setValue($object, $data[$dataKey]);
}
}
return $object;
}
/**
* Extracts data from an object according to map
*
* @param object $object
* @return array
*/
public function extract($object)
{
$data = [];
$className = get_class($object);
$reflection = $this->getReflectionClass($className);
foreach ($this->map as $dataKey => $propertyName) {
if ($reflection->hasProperty($propertyName)) {
$property = $reflection->getProperty($propertyName);
$property->setAccessible(true);
$data[$dataKey] = $property->getValue($object);
}
}
return $data;
}
/**
* Returns instance of reflection class for class name passed
*
* @param string $className
* @return \ReflectionClass
* @throws \ReflectionException
*/
protected function getReflectionClass($className)
{
if (!isset($this->reflectionClassMap[$className])) {
$this->reflectionClassMap[$className] = new \ReflectionClass($className);
}
return $this->reflectionClassMap[$className];
}
}