1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DoctrineElastic\Hydrate; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Persistence\Mapping\RuntimeReflectionService; |
6
|
|
|
use Doctrine\ORM\Mapping\Reflection\ReflectionPropertiesGetter; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Common Entity Hydrator |
10
|
|
|
* Uses ReflectionPropertiesGetter from Doctrine |
11
|
|
|
* |
12
|
|
|
* @author Andsalves <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class SimpleEntityHydrator implements SimpleHydratorInterface { |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var ReflectionPropertiesGetter |
18
|
|
|
*/ |
19
|
|
|
protected $reflectionPropertiesGetter; |
20
|
|
|
|
21
|
|
|
public function __construct() { |
22
|
|
|
$this->reflectionPropertiesGetter = new ReflectionPropertiesGetter(new RuntimeReflectionService()); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param object $entity |
27
|
|
|
* @param array $data |
28
|
|
|
* @return object |
29
|
|
|
*/ |
30
|
|
|
public function hydrate($entity, array $data) { |
31
|
|
|
$classProperties = $this->reflectionPropertiesGetter->getProperties(get_class($entity)); |
32
|
|
|
|
33
|
|
|
foreach ($classProperties as $prop) { |
34
|
|
|
$prop->setAccessible(true); |
35
|
|
|
|
36
|
|
|
if (array_key_exists($prop->name, $data)) { |
37
|
|
|
$prop->setValue($entity, $data[$prop->name]); |
38
|
|
|
} else { |
39
|
|
|
$name = self::decamelizeString($prop->name); |
40
|
|
|
|
41
|
|
|
if (array_key_exists($name, $data)) { |
42
|
|
|
$prop->setValue($entity, $data[$name]); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
if (method_exists($entity, 'exchangeArray')) { |
48
|
|
|
$entity->exchangeArray($data); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
if (method_exists($entity, 'populate')) { |
52
|
|
|
$entity->populate($data); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return $entity; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param object $entity |
60
|
|
|
* @param string|array $fieldOrFields |
61
|
|
|
* @return array|mixed |
62
|
|
|
*/ |
63
|
|
|
public function extract($entity, $fieldOrFields = null) { |
64
|
|
|
$filterFields = null; |
65
|
|
|
$data = []; |
66
|
|
|
/** @var \ReflectionProperty[] $classProperties */ |
67
|
|
|
$classProperties = $this->reflectionPropertiesGetter->getProperties(get_class($entity)); |
68
|
|
|
|
69
|
|
|
if (!is_array($fieldOrFields) && is_string($fieldOrFields)) { |
70
|
|
|
$filterFields = [$fieldOrFields]; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
foreach ($classProperties as $prop) { |
74
|
|
|
if (is_array($filterFields)) { |
75
|
|
|
$filtered = in_array($prop->name, $filterFields); |
76
|
|
|
$filtered |= in_array(self::decamelizeString($prop->name), $filterFields); |
77
|
|
|
|
78
|
|
|
if (!$filtered) { |
79
|
|
|
continue; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$prop->setAccessible(true); |
84
|
|
|
$value = $prop->getValue($entity); |
85
|
|
|
|
86
|
|
|
$decamelName = self::decamelizeString($prop->name); |
87
|
|
|
|
88
|
|
|
if ($decamelName == $fieldOrFields |
89
|
|
|
|| $prop->name == $fieldOrFields |
90
|
|
|
|| $prop->name == self::camelizeString($fieldOrFields) |
91
|
|
|
) { |
92
|
|
|
return $value; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$data[$decamelName] = $value; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
if (empty($data) && is_string($fieldOrFields)) { |
99
|
|
|
return null; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $data; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public static function decamelizeString($string) { |
106
|
|
|
if (is_string($string) && !empty($string)) { |
107
|
|
|
$prefix = ''; |
108
|
|
|
if (substr($string, 0, 1) == '_') { |
109
|
|
|
$prefix = '_'; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return $prefix . ltrim(strtolower(preg_replace('/[A-Z]([A-Z](?![a-z]))*/', '_$0', $string)), '_'); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return $string; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public static function camelizeString($string) { |
119
|
|
|
if (is_string($string) && !empty($string)) { |
120
|
|
|
if (substr($string, 0, 1) == '_') { |
121
|
|
|
$string = substr($string, 1); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
$words = str_replace('_', ' ', $string); |
125
|
|
|
$ucWords = ucwords($words); |
126
|
|
|
|
127
|
|
|
return lcfirst(str_replace(' ', '', $ucWords)); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return $string; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|