Merge remote-tracking branch 'nurikabe/2.0' into 2.0
This commit is contained in:
commit
43e6e84c5b
15
Doctrine/AbstractElasticaToModelTransformer.php
Normal file → Executable file
15
Doctrine/AbstractElasticaToModelTransformer.php
Normal file → Executable file
|
@ -4,6 +4,7 @@ namespace FOQ\ElasticaBundle\Doctrine;
|
|||
|
||||
use FOQ\ElasticaBundle\HybridResult;
|
||||
use FOQ\ElasticaBundle\Transformer\ElasticaToModelTransformerInterface;
|
||||
use FOQ\ElasticaBundle\Transformer\HighlightableModelInterface;
|
||||
use Elastica_Document;
|
||||
|
||||
/**
|
||||
|
@ -68,15 +69,23 @@ abstract class AbstractElasticaToModelTransformer implements ElasticaToModelTran
|
|||
**/
|
||||
public function transform(array $elasticaObjects)
|
||||
{
|
||||
$ids = array_map(function($elasticaObject) {
|
||||
return $elasticaObject->getId();
|
||||
}, $elasticaObjects);
|
||||
$ids = $highlights = array();
|
||||
foreach ($elasticaObjects as $elasticaObject) {
|
||||
$ids[] = $elasticaObject->getId();
|
||||
$highlights[$elasticaObject->getId()] = $elasticaObject->getHighlights();
|
||||
}
|
||||
|
||||
$objects = $this->findByIdentifiers($ids, $this->options['hydrate']);
|
||||
if (count($objects) < count($elasticaObjects)) {
|
||||
throw new \RuntimeException('Cannot find corresponding Doctrine objects for all Elastica results.');
|
||||
};
|
||||
|
||||
foreach ($objects as $object) {
|
||||
if ($object instanceof HighlightableModelInterface) {
|
||||
$object->setElasticHighlights($highlights[$object->getId()]);
|
||||
}
|
||||
}
|
||||
|
||||
$identifierGetter = 'get'.ucfirst($this->options['identifier']);
|
||||
|
||||
// sort objects in the order of ids
|
||||
|
|
16
Transformer/HighlightableModelInterface.php
Normal file
16
Transformer/HighlightableModelInterface.php
Normal file
|
@ -0,0 +1,16 @@
|
|||
<?php
|
||||
|
||||
namespace FOQ\ElasticaBundle\Transformer;
|
||||
|
||||
/**
|
||||
* Maps Elastica documents with model objects
|
||||
*/
|
||||
interface HighlightableModelInterface
|
||||
{
|
||||
/**
|
||||
* Set ElasticSearch highlight data.
|
||||
*
|
||||
* @param array of highlight strings
|
||||
*/
|
||||
public function setElasticHighlights(array $highlights);
|
||||
}
|
Loading…
Reference in a new issue