1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace SolBianca\Hydrator; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
class Hydrator implements HydratorInterface |
8
|
|
|
{ |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Local cache of reflection class instances |
12
|
|
|
* @var array |
13
|
|
|
*/ |
14
|
|
|
private $reflectionClassMap = []; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* {@inheritdoc} |
18
|
|
|
*/ |
19
|
|
|
public function hydrate($object, array $data) |
20
|
|
|
{ |
21
|
|
|
if (is_object($object)) { |
22
|
|
|
$className = get_class($object); |
23
|
|
|
$reflection = $this->getReflectionClass($className); |
24
|
|
|
} elseif (is_string($object)) { |
25
|
|
|
if (!class_exists($object)) { |
26
|
|
|
throw new HydrateObjectException("Given class name '{$object}' must be valid class name."); |
27
|
|
|
} |
28
|
|
|
$className = $object; |
29
|
|
|
$reflection = $this->getReflectionClass($className); |
30
|
|
|
$object = $reflection->newInstanceWithoutConstructor(); |
31
|
|
|
} else { |
32
|
|
|
throw new HydrateObjectException("Invalid object."); |
33
|
|
|
} |
34
|
|
View Code Duplication |
foreach ($data as $propertyName => $propertyValue) { |
|
|
|
|
35
|
|
|
if (!$reflection->hasProperty($propertyName)) { |
36
|
|
|
throw new HydrateObjectException("There's no '$propertyName' property in '$className'."); |
37
|
|
|
} |
38
|
|
|
$property = $reflection->getProperty($propertyName); |
39
|
|
|
if ($property->isStatic()) { |
40
|
|
|
continue; |
41
|
|
|
} |
42
|
|
|
$property->setAccessible(true); |
43
|
|
|
$property->setValue($object, $propertyValue); |
44
|
|
|
} |
45
|
|
|
return $object; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritdoc} |
50
|
|
|
*/ |
51
|
|
|
public function extract($object, array $properties = []) |
52
|
|
|
{ |
53
|
|
|
$data = []; |
54
|
|
|
$className = get_class($object); |
55
|
|
|
$reflection = $this->getReflectionClass($className); |
56
|
|
|
if ([] === $properties) { |
57
|
|
|
$properties = $this->getReflectionProperties($reflection); |
58
|
|
|
} |
59
|
|
View Code Duplication |
foreach ($properties as $propertyName) { |
|
|
|
|
60
|
|
|
if ($reflection->hasProperty($propertyName)) { |
61
|
|
|
$property = $reflection->getProperty($propertyName); |
62
|
|
|
if ($property->isStatic()) { |
63
|
|
|
continue; |
64
|
|
|
} |
65
|
|
|
$property->setAccessible(true); |
66
|
|
|
$data[$propertyName] = $property->getValue($object); |
67
|
|
|
} else { |
68
|
|
|
throw new ExctractObjectException("There's no '$propertyName' property in '$className'."); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
return $data; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param \ReflectionClass $reflection |
76
|
|
|
* @return array |
77
|
|
|
*/ |
78
|
|
|
protected function getReflectionProperties(\ReflectionClass $reflection) |
79
|
|
|
{ |
80
|
|
|
$properties = $reflection->getProperties(); |
81
|
|
|
$result = []; |
82
|
|
|
if (empty($properties)) { |
83
|
|
|
return $result; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
foreach ($properties as $property) { |
87
|
|
|
$result[] = $property->getName(); |
88
|
|
|
} |
89
|
|
|
return $result; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Returns instance of reflection class for class name passed |
94
|
|
|
* |
95
|
|
|
* @param string $className |
96
|
|
|
* @return \ReflectionClass |
97
|
|
|
*/ |
98
|
|
|
protected function getReflectionClass($className) |
99
|
|
|
{ |
100
|
|
|
if (!isset($this->reflectionClassMap[$className])) { |
101
|
|
|
$this->reflectionClassMap[$className] = new \ReflectionClass($className); |
102
|
|
|
} |
103
|
|
|
return $this->reflectionClassMap[$className]; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.