1 | <?php |
||
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 | 14 | public function __construct(ManagerRegistry $registry, string $objectClass, array $options = []) |
|
66 | |||
67 | /** |
||
68 | * Returns the object class that is used for conversion. |
||
69 | */ |
||
70 | 1 | public function getObjectClass(): string |
|
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 | 7 | public function transform(array $elasticaObjects) |
|
86 | { |
||
87 | 7 | $ids = $highlights = []; |
|
88 | 7 | foreach ($elasticaObjects as $elasticaObject) { |
|
89 | 7 | $ids[] = $elasticaObject->getId(); |
|
90 | 7 | $highlights[$elasticaObject->getId()] = $elasticaObject->getHighlights(); |
|
91 | } |
||
92 | |||
93 | 7 | $objects = $this->findByIdentifiers($ids, $this->options['hydrate']); |
|
94 | 7 | $objectsCnt = count($objects); |
|
95 | 7 | $elasticaObjectsCnt = count($elasticaObjects); |
|
96 | 7 | $propertyAccessor = $this->propertyAccessor; |
|
97 | 7 | $identifier = $this->options['identifier']; |
|
98 | 7 | 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 | 1 | }, $objects)); |
|
102 | |||
103 | 1 | 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 | 6 | foreach ($objects as $object) { |
|
107 | 5 | if ($object instanceof HighlightableModelInterface) { |
|
108 | 4 | $id = $propertyAccessor->getValue($object, $identifier); |
|
109 | 4 | $object->setElasticHighlights($highlights[(string) $id]); |
|
110 | } |
||
111 | } |
||
112 | |||
113 | // sort objects in the order of ids |
||
114 | 6 | $idPos = array_flip($ids); |
|
115 | 6 | usort( |
|
116 | 6 | $objects, |
|
117 | function ($a, $b) use ($idPos, $identifier, $propertyAccessor) { |
||
118 | 5 | if ($this->options['hydrate']) { |
|
119 | 5 | return $idPos[(string) $propertyAccessor->getValue( |
|
120 | 5 | $a, |
|
121 | $identifier |
||
122 | 5 | )] > $idPos[(string) $propertyAccessor->getValue($b, $identifier)]; |
|
123 | } |
||
124 | |||
125 | return $idPos[$a[$identifier]] > $idPos[$b[$identifier]]; |
||
126 | 6 | } |
|
127 | ); |
||
128 | |||
129 | 6 | return $objects; |
|
130 | } |
||
131 | |||
132 | 2 | public function hybridTransform(array $elasticaObjects) |
|
153 | |||
154 | /** |
||
155 | * {@inheritdoc} |
||
156 | */ |
||
157 | 1 | public function getIdentifierField(): string |
|
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 |