forked from FriendsOfSymfony/FOSElasticaBundle
/
AbstractElasticaToModelTransformer.php
161 lines (140 loc) · 5.17 KB
/
AbstractElasticaToModelTransformer.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
/*
* This file is part of the FOSElasticaBundle package.
*
* (c) FriendsOfSymfony <https://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FOS\ElasticaBundle\Doctrine;
use Doctrine\Persistence\ManagerRegistry;
use Elastica\Result;
use FOS\ElasticaBundle\HybridResult;
use FOS\ElasticaBundle\Transformer\AbstractElasticaToModelTransformer as BaseTransformer;
use FOS\ElasticaBundle\Transformer\HighlightableModelInterface;
/**
* Maps Elastica documents with Doctrine objects
* This mapper assumes an exact match between
* elastica documents ids and doctrine object ids.
*/
abstract class AbstractElasticaToModelTransformer extends BaseTransformer
{
/**
* Manager registry.
*/
protected ManagerRegistry $registry;
/**
* Class of the model to map to the elastica documents.
*
* @phpstan-var class-string
*/
protected string $objectClass;
/**
* Optional parameters.
*/
protected array $options = [
'hints' => [],
'hydrate' => true,
'identifier' => 'id',
'ignore_missing' => false,
'query_builder_method' => 'createQueryBuilder',
];
/**
* Instantiates a new Mapper.
*/
public function __construct(ManagerRegistry $registry, string $objectClass, array $options = [])
{
$this->registry = $registry;
$this->objectClass = $objectClass;
$this->options = \array_merge($this->options, $options);
}
/**
* Returns the object class that is used for conversion.
*/
public function getObjectClass(): string
{
return $this->objectClass;
}
/**
* Transforms an array of elastica objects into an array of
* model objects fetched from the doctrine repository.
*
* @param Result[] $elasticaObjects of elastica objects
*
* @throws \RuntimeException
*
* @return array
*/
public function transform(array $elasticaObjects)
{
$ids = $highlights = [];
foreach ($elasticaObjects as $elasticaObject) {
$ids[] = $elasticaObject->getId();
$highlights[$elasticaObject->getId()] = $elasticaObject->getHighlights();
}
$objects = $this->findByIdentifiers($ids, $this->options['hydrate']);
$objectsCnt = \count($objects);
$elasticaObjectsCnt = \count($elasticaObjects);
$propertyAccessor = $this->propertyAccessor;
$identifier = $this->options['identifier'];
if (!$this->options['ignore_missing'] && $objectsCnt < $elasticaObjectsCnt) {
$missingIds = \array_diff($ids, \array_map(static function ($object) use ($propertyAccessor, $identifier) {
return $propertyAccessor->getValue($object, $identifier);
}, $objects));
throw new \RuntimeException(\sprintf('Cannot find corresponding Doctrine objects (%d) for all Elastica results (%d). Missing IDs: %s. IDs: %s', $objectsCnt, $elasticaObjectsCnt, \implode(', ', $missingIds), \implode(', ', $ids)));
}
foreach ($objects as $object) {
if ($object instanceof HighlightableModelInterface) {
$id = $propertyAccessor->getValue($object, $identifier);
$object->setElasticHighlights($highlights[(string) $id]);
}
}
// sort objects in the order of ids
$idPos = \array_flip($ids);
\usort(
$objects,
function ($a, $b) use ($idPos, $identifier, $propertyAccessor) {
if ($this->options['hydrate']) {
return $idPos[(string) $propertyAccessor->getValue(
$a,
$identifier
)] <=> $idPos[(string) $propertyAccessor->getValue($b, $identifier)];
}
return $idPos[$a[$identifier]] <=> $idPos[$b[$identifier]];
}
);
return $objects;
}
public function hybridTransform(array $elasticaObjects)
{
$indexedElasticaResults = [];
foreach ($elasticaObjects as $elasticaObject) {
$indexedElasticaResults[(string) $elasticaObject->getId()] = $elasticaObject;
}
$objects = $this->transform($elasticaObjects);
$result = [];
foreach ($objects as $object) {
if ($this->options['hydrate']) {
$id = $this->propertyAccessor->getValue($object, $this->options['identifier']);
} else {
$id = $object[$this->options['identifier']];
}
$result[] = new HybridResult($indexedElasticaResults[(string) $id], $object);
}
return $result;
}
public function getIdentifierField(): string
{
return $this->options['identifier'];
}
/**
* Fetches objects by theses identifier values.
*
* @param array $identifierValues ids values
* @param bool $hydrate whether or not to hydrate the objects, false returns arrays
*
* @return array of objects or arrays
*/
abstract protected function findByIdentifiers(array $identifierValues, bool $hydrate);
}