|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Pgs\ElasticOM\Hydrator; |
|
4
|
|
|
|
|
5
|
|
|
use Pgs\ElasticOM\Annotation\ClassMetadata; |
|
6
|
|
|
use Pgs\ElasticOM\Extractor; |
|
7
|
|
|
use Pgs\ElasticOM\ObjectPool; |
|
8
|
|
|
use Pgs\ElasticOM\ReflectionSingleton; |
|
9
|
|
|
use Zend\Hydrator\AbstractHydrator; |
|
10
|
|
|
use Zend\Hydrator\Reflection; |
|
11
|
|
|
|
|
12
|
|
|
class Hydrator |
|
13
|
|
|
{ |
|
14
|
|
|
/** @var Extractor */ |
|
15
|
|
|
protected $extractor; |
|
16
|
|
|
|
|
17
|
|
|
/** @var AbstractHydrator */ |
|
18
|
|
|
protected $hydrator; |
|
19
|
|
|
|
|
20
|
|
|
/** @var ObjectPool */ |
|
21
|
|
|
protected $objectPool; |
|
22
|
|
|
|
|
23
|
5 |
|
public function __construct(Extractor $extractor, AbstractHydrator $hydrator, ObjectPool $objectPool) |
|
24
|
|
|
{ |
|
25
|
5 |
|
$this->extractor = $extractor; |
|
26
|
5 |
|
$this->hydrator = $hydrator; |
|
27
|
5 |
|
$this->objectPool = $objectPool; |
|
28
|
5 |
|
} |
|
29
|
|
|
|
|
30
|
1 |
|
public function getIdName(string $entity): string |
|
31
|
|
|
{ |
|
32
|
1 |
|
return $this->extractor->getIdName($entity); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
3 |
|
public function hydrate(array $data, $object) |
|
36
|
|
|
{ |
|
37
|
3 |
|
if (!is_object($object)) { |
|
38
|
1 |
|
throw new \InvalidArgumentException('Missing object to hydrate'); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
2 |
|
$class = get_class($object); |
|
42
|
|
|
|
|
43
|
2 |
|
$metadata = ClassMetadata::getRawConfig($class); |
|
44
|
|
|
$nested = array_filter($metadata, function ($item) { |
|
45
|
2 |
|
return array_key_exists('targetClass', $item); |
|
46
|
2 |
|
}); |
|
47
|
|
|
|
|
48
|
2 |
|
$this->processNested($nested, $data); |
|
49
|
|
|
|
|
50
|
2 |
|
return $this->hydrator->hydrate( |
|
51
|
|
|
array_intersect_key( |
|
52
|
|
|
$data, |
|
53
|
2 |
|
array_flip($this->extractor->getFieldNames($class)) |
|
54
|
|
|
), |
|
55
|
|
|
$object |
|
56
|
|
|
); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
2 |
|
protected function processNested(array $config, array &$data) |
|
60
|
|
|
{ |
|
61
|
2 |
|
foreach (array_intersect_key($config, $data) as $field => $metadata) { |
|
62
|
1 |
|
$idField = array_keys(array_filter( |
|
63
|
1 |
|
$metadata['properties'], |
|
64
|
|
|
function ($item) { |
|
65
|
1 |
|
return array_key_exists('idField', $item); |
|
66
|
1 |
|
} |
|
67
|
|
|
)); |
|
68
|
1 |
|
$idField = reset($idField); |
|
69
|
|
|
|
|
70
|
1 |
|
$data[$field] = $this->getObject($data[$field], $idField, $metadata['targetClass']); |
|
71
|
|
|
} |
|
72
|
2 |
|
} |
|
73
|
|
|
|
|
74
|
1 |
|
protected function getObject(array $data, string $idField, string $class) |
|
75
|
|
|
{ |
|
76
|
1 |
|
$object = $this->objectPool->get($idField, $class); |
|
77
|
1 |
|
if (!$object) { |
|
78
|
1 |
|
$object = ReflectionSingleton::getInstance($class)->newInstanceWithoutConstructor(); |
|
79
|
1 |
|
$this->hydrate($data, $object); |
|
80
|
1 |
|
$this->objectPool->add($idField, $object); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
1 |
|
return $object; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
1 |
|
public function extract($object, bool $rootLevel = true): array |
|
87
|
|
|
{ |
|
88
|
1 |
|
$this->hydrator = new Reflection(); |
|
89
|
1 |
|
$this->hydrator->addStrategy('*', new HydrationStrategy($this)); |
|
90
|
1 |
|
if ($rootLevel) { |
|
91
|
1 |
|
$this->hydrator->addFilter('id', function ($property) use ($object) { |
|
92
|
|
|
return |
|
93
|
1 |
|
in_array($property, $this->extractor->getFieldNames(get_class($object)), true) |
|
94
|
1 |
|
&& $property !== $this->extractor->getIdName(get_class($object)); |
|
95
|
1 |
|
}); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
1 |
|
return $this->hydrator->extract($object); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|