1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the FOSElasticaBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace FOS\ElasticaBundle\Doctrine; |
13
|
|
|
|
14
|
|
|
use Doctrine\Common\Persistence\ManagerRegistry; |
15
|
|
|
use FOS\ElasticaBundle\HybridResult; |
16
|
|
|
use FOS\ElasticaBundle\Transformer\AbstractElasticaToModelTransformer as BaseTransformer; |
17
|
|
|
use FOS\ElasticaBundle\Transformer\HighlightableModelInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Maps Elastica documents with Doctrine objects |
21
|
|
|
* This mapper assumes an exact match between |
22
|
|
|
* elastica documents ids and doctrine object ids. |
23
|
|
|
*/ |
24
|
|
|
abstract class AbstractElasticaToModelTransformer extends BaseTransformer |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* Manager registry. |
28
|
|
|
* |
29
|
|
|
* @var ManagerRegistry |
30
|
|
|
*/ |
31
|
|
|
protected $registry = null; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Class of the model to map to the elastica documents. |
35
|
|
|
* |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
protected $objectClass = null; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Optional parameters. |
42
|
|
|
* |
43
|
|
|
* @var array |
44
|
|
|
*/ |
45
|
|
|
protected $options = [ |
46
|
|
|
'hints' => [], |
47
|
|
|
'hydrate' => true, |
48
|
|
|
'identifier' => 'id', |
49
|
|
|
'ignore_missing' => false, |
50
|
|
|
'query_builder_method' => 'createQueryBuilder', |
51
|
|
|
]; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Instantiates a new Mapper. |
55
|
|
|
* |
56
|
|
|
* @param ManagerRegistry $registry |
57
|
|
|
* @param string $objectClass |
58
|
|
|
* @param array $options |
59
|
|
|
*/ |
60
|
|
|
public function __construct(ManagerRegistry $registry, string $objectClass, array $options = []) |
61
|
|
|
{ |
62
|
|
|
$this->registry = $registry; |
63
|
|
|
$this->objectClass = $objectClass; |
64
|
|
|
$this->options = array_merge($this->options, $options); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Returns the object class that is used for conversion. |
69
|
|
|
*/ |
70
|
|
|
public function getObjectClass(): string |
71
|
|
|
{ |
72
|
|
|
return $this->objectClass; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Transforms an array of elastica objects into an array of |
77
|
|
|
* model objects fetched from the doctrine repository. |
78
|
|
|
* |
79
|
|
|
* @param array $elasticaObjects of elastica objects |
80
|
|
|
* |
81
|
|
|
* @throws \RuntimeException |
82
|
|
|
* |
83
|
|
|
* @return array |
84
|
|
|
**/ |
85
|
|
|
public function transform(array $elasticaObjects) |
86
|
|
|
{ |
87
|
|
|
$ids = $highlights = []; |
88
|
|
|
foreach ($elasticaObjects as $elasticaObject) { |
89
|
|
|
$ids[] = $elasticaObject->getId(); |
90
|
|
|
$highlights[$elasticaObject->getId()] = $elasticaObject->getHighlights(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$objects = $this->findByIdentifiers($ids, $this->options['hydrate']); |
94
|
|
|
$objectsCnt = count($objects); |
95
|
|
|
$elasticaObjectsCnt = count($elasticaObjects); |
96
|
|
|
$propertyAccessor = $this->propertyAccessor; |
97
|
|
|
$identifier = $this->options['identifier']; |
98
|
|
|
if (!$this->options['ignore_missing'] && $objectsCnt < $elasticaObjectsCnt) { |
99
|
|
|
$missingIds = array_diff($ids, array_map(function ($object) use ($propertyAccessor, $identifier) { |
100
|
|
|
return $propertyAccessor->getValue($object, $identifier); |
101
|
|
|
}, $objects)); |
102
|
|
|
|
103
|
|
|
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))); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
foreach ($objects as $object) { |
107
|
|
|
if ($object instanceof HighlightableModelInterface) { |
108
|
|
|
$id = $propertyAccessor->getValue($object, $identifier); |
109
|
|
|
$object->setElasticHighlights($highlights[(string) $id]); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
// sort objects in the order of ids |
114
|
|
|
$idPos = array_flip($ids); |
115
|
|
|
usort( |
116
|
|
|
$objects, |
117
|
|
|
function ($a, $b) use ($idPos, $identifier, $propertyAccessor) { |
118
|
|
|
if ($this->options['hydrate']) { |
119
|
|
|
return $idPos[(string) $propertyAccessor->getValue( |
120
|
|
|
$a, |
121
|
|
|
$identifier |
122
|
|
|
)] > $idPos[(string) $propertyAccessor->getValue($b, $identifier)]; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return $idPos[$a[$identifier]] > $idPos[$b[$identifier]]; |
126
|
|
|
} |
127
|
|
|
); |
128
|
|
|
|
129
|
|
|
return $objects; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function hybridTransform(array $elasticaObjects) |
133
|
|
|
{ |
134
|
|
|
$indexedElasticaResults = []; |
135
|
|
|
foreach ($elasticaObjects as $elasticaObject) { |
136
|
|
|
$indexedElasticaResults[(string) $elasticaObject->getId()] = $elasticaObject; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
$objects = $this->transform($elasticaObjects); |
140
|
|
|
|
141
|
|
|
$result = []; |
142
|
|
|
foreach ($objects as $object) { |
143
|
|
|
if ($this->options['hydrate']) { |
144
|
|
|
$id = $this->propertyAccessor->getValue($object, $this->options['identifier']); |
145
|
|
|
} else { |
146
|
|
|
$id = $object[$this->options['identifier']]; |
147
|
|
|
} |
148
|
|
|
$result[] = new HybridResult($indexedElasticaResults[(string) $id], $object); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return $result; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* {@inheritdoc} |
156
|
|
|
*/ |
157
|
|
|
public function getIdentifierField(): string |
158
|
|
|
{ |
159
|
|
|
return $this->options['identifier']; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Fetches objects by theses identifier values. |
164
|
|
|
* |
165
|
|
|
* @param array $identifierValues ids values |
166
|
|
|
* @param bool $hydrate whether or not to hydrate the objects, false returns arrays |
167
|
|
|
* |
168
|
|
|
* @return array of objects or arrays |
169
|
|
|
*/ |
170
|
|
|
abstract protected function findByIdentifiers(array $identifierValues, bool $hydrate); |
171
|
|
|
} |
172
|
|
|
|