130 lines
3.6 KiB
PHP
Executable file
130 lines
3.6 KiB
PHP
Executable file
<?php
|
|
|
|
namespace FOS\ElasticaBundle\Doctrine;
|
|
|
|
use FOS\ElasticaBundle\HybridResult;
|
|
use FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerInterface;
|
|
use FOS\ElasticaBundle\Transformer\HighlightableModelInterface;
|
|
use Elastica_Document;
|
|
|
|
/**
|
|
* Maps Elastica documents with Doctrine objects
|
|
* This mapper assumes an exact match between
|
|
* elastica documents ids and doctrine object ids
|
|
*/
|
|
abstract class AbstractElasticaToModelTransformer implements ElasticaToModelTransformerInterface
|
|
{
|
|
/**
|
|
* Repository to fetch the objects from
|
|
*/
|
|
protected $objectManager = null;
|
|
|
|
/**
|
|
* Class of the model to map to the elastica documents
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $objectClass = null;
|
|
|
|
/**
|
|
* Optional parameters
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $options = array(
|
|
'hydrate' => true,
|
|
'identifier' => 'id'
|
|
);
|
|
|
|
/**
|
|
* Instantiates a new Mapper
|
|
*
|
|
* @param object $objectManager
|
|
* @param string $objectClass
|
|
* @param array $options
|
|
*/
|
|
public function __construct($objectManager, $objectClass, array $options = array())
|
|
{
|
|
$this->objectManager = $objectManager;
|
|
$this->objectClass = $objectClass;
|
|
$this->options = array_merge($this->options, $options);
|
|
}
|
|
|
|
/**
|
|
* Returns the object class that is used for conversion.
|
|
*
|
|
* @return string
|
|
*/
|
|
public function getObjectClass()
|
|
{
|
|
return $this->objectClass;
|
|
}
|
|
|
|
/**
|
|
* Transforms an array of elastica objects into an array of
|
|
* model objects fetched from the doctrine repository
|
|
*
|
|
* @param array of elastica objects
|
|
* @return array
|
|
**/
|
|
public function transform(array $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
|
|
$idPos = array_flip($ids);
|
|
usort($objects, function($a, $b) use ($idPos, $identifierGetter)
|
|
{
|
|
return $idPos[$a->$identifierGetter()] > $idPos[$b->$identifierGetter()];
|
|
});
|
|
|
|
return $objects;
|
|
}
|
|
|
|
public function hybridTransform(array $elasticaObjects)
|
|
{
|
|
$objects = $this->transform($elasticaObjects);
|
|
|
|
$result = array();
|
|
for ($i = 0; $i < count($elasticaObjects); $i++) {
|
|
$result[] = new HybridResult($elasticaObjects[$i], $objects[$i]);
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function getIdentifierField()
|
|
{
|
|
return $this->options['identifier'];
|
|
}
|
|
|
|
/**
|
|
* Fetches objects by theses identifier values
|
|
*
|
|
* @param array $identifierValues ids values
|
|
* @param Boolean $hydrate whether or not to hydrate the objects, false returns arrays
|
|
* @return array of objects or arrays
|
|
*/
|
|
protected abstract function findByIdentifiers(array $identifierValues, $hydrate);
|
|
}
|