1 | <?php |
||
15 | abstract class AbstractElasticaToModelTransformer extends BaseTransformer |
||
16 | { |
||
17 | /** |
||
18 | * Manager registry. |
||
19 | * |
||
20 | * @var ManagerRegistry |
||
21 | */ |
||
22 | protected $registry = null; |
||
23 | |||
24 | /** |
||
25 | * Class of the model to map to the elastica documents. |
||
26 | * |
||
27 | * @var string |
||
28 | */ |
||
29 | protected $objectClass = null; |
||
30 | |||
31 | /** |
||
32 | * Optional parameters. |
||
33 | * |
||
34 | * @var array |
||
35 | */ |
||
36 | protected $options = array( |
||
37 | 'hints' => array(), |
||
38 | 'hydrate' => true, |
||
39 | 'identifier' => 'id', |
||
40 | 'ignore_missing' => false, |
||
41 | 'query_builder_method' => 'createQueryBuilder', |
||
42 | ); |
||
43 | |||
44 | /** |
||
45 | * Instantiates a new Mapper. |
||
46 | * |
||
47 | * @param ManagerRegistry $registry |
||
48 | * @param string $objectClass |
||
49 | * @param array $options |
||
50 | */ |
||
51 | 4 | public function __construct(ManagerRegistry $registry, $objectClass, array $options = array()) |
|
52 | { |
||
53 | 4 | $this->registry = $registry; |
|
54 | 4 | $this->objectClass = $objectClass; |
|
55 | 4 | $this->options = array_merge($this->options, $options); |
|
56 | 4 | } |
|
57 | |||
58 | /** |
||
59 | * Returns the object class that is used for conversion. |
||
60 | * |
||
61 | * @return string |
||
62 | */ |
||
63 | public function getObjectClass() |
||
64 | { |
||
65 | return $this->objectClass; |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * Transforms an array of elastica objects into an array of |
||
70 | * model objects fetched from the doctrine repository. |
||
71 | * |
||
72 | * @param array $elasticaObjects of elastica objects |
||
73 | * |
||
74 | * @throws \RuntimeException |
||
75 | * |
||
76 | * @return array |
||
77 | **/ |
||
78 | 1 | public function transform(array $elasticaObjects) |
|
79 | { |
||
80 | 1 | $ids = $highlights = array(); |
|
81 | 1 | foreach ($elasticaObjects as $elasticaObject) { |
|
82 | 1 | $ids[] = $elasticaObject->getId(); |
|
83 | 1 | $highlights[$elasticaObject->getId()] = $elasticaObject->getHighlights(); |
|
84 | } |
||
85 | |||
86 | 1 | $objects = $this->findByIdentifiers($ids, $this->options['hydrate']); |
|
87 | 1 | if (!$this->options['ignore_missing'] && count($objects) < count($elasticaObjects)) { |
|
88 | throw new \RuntimeException('Cannot find corresponding Doctrine objects for all Elastica results.'); |
||
89 | }; |
||
90 | |||
91 | 1 | foreach ($objects as $object) { |
|
92 | 1 | if ($object instanceof HighlightableModelInterface) { |
|
93 | 1 | $object->setElasticHighlights($highlights[$object->getId()]); |
|
94 | } |
||
95 | } |
||
96 | |||
97 | // sort objects in the order of ids |
||
98 | 1 | $idPos = array_flip($ids); |
|
99 | 1 | $identifier = $this->options['identifier']; |
|
100 | 1 | usort($objects, $this->getSortingClosure($idPos, $identifier)); |
|
101 | |||
102 | 1 | return $objects; |
|
103 | } |
||
104 | |||
105 | 1 | public function hybridTransform(array $elasticaObjects) |
|
106 | { |
||
107 | 1 | $indexedElasticaResults = array(); |
|
108 | 1 | foreach ($elasticaObjects as $elasticaObject) { |
|
109 | 1 | $indexedElasticaResults[$elasticaObject->getId()] = $elasticaObject; |
|
110 | } |
||
111 | |||
112 | 1 | $objects = $this->transform($elasticaObjects); |
|
113 | |||
114 | 1 | $result = array(); |
|
115 | 1 | foreach ($objects as $object) { |
|
116 | 1 | $id = $this->propertyAccessor->getValue($object, $this->options['identifier']); |
|
117 | 1 | $result[] = new HybridResult($indexedElasticaResults[$id], $object); |
|
118 | } |
||
119 | |||
120 | 1 | return $result; |
|
121 | } |
||
122 | |||
123 | /** |
||
124 | * {@inheritDoc} |
||
125 | */ |
||
126 | public function getIdentifierField() |
||
127 | { |
||
128 | return $this->options['identifier']; |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * Fetches objects by theses identifier values. |
||
133 | * |
||
134 | * @param array $identifierValues ids values |
||
135 | * @param Boolean $hydrate whether or not to hydrate the objects, false returns arrays |
||
136 | * |
||
137 | * @return array of objects or arrays |
||
138 | */ |
||
139 | abstract protected function findByIdentifiers(array $identifierValues, $hydrate); |
||
140 | } |
||
141 |