2011-04-15 01:22:14 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace FOQ\ElasticaBundle\Mapper;
|
|
|
|
|
|
|
|
use FOQ\ElasticaBundle\MapperInterface;
|
2011-04-20 22:53:59 +02:00
|
|
|
use Doctrine\Common\Persistence\ObjectManager;
|
2011-04-15 01:22:14 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Maps Elastica documents with Doctrine objects
|
|
|
|
* This mapper assumes an exact match between
|
|
|
|
* elastica documents ids and doctrine object ids
|
|
|
|
*/
|
|
|
|
class DoctrineMapper implements MapperInterface
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Repository to fetch the objects from
|
|
|
|
*
|
2011-04-20 22:53:59 +02:00
|
|
|
* @var ObjectManager
|
2011-04-15 01:22:14 +02:00
|
|
|
*/
|
2011-04-20 22:53:59 +02:00
|
|
|
protected $objectManager = null;
|
2011-04-15 01:22:14 +02:00
|
|
|
|
|
|
|
/**
|
2011-04-20 22:53:59 +02:00
|
|
|
* Class of the model to map to the elastica documents
|
2011-04-15 01:22:14 +02:00
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2011-04-20 22:53:59 +02:00
|
|
|
protected $objectClass = null;
|
2011-04-15 01:22:14 +02:00
|
|
|
|
2011-04-15 19:35:11 +02:00
|
|
|
/**
|
|
|
|
* Optional parameters
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $options = array(
|
2011-04-20 22:53:59 +02:00
|
|
|
'hydrate' => true,
|
|
|
|
'identifier' => 'id'
|
2011-04-15 19:35:11 +02:00
|
|
|
);
|
|
|
|
|
2011-04-15 01:22:14 +02:00
|
|
|
/**
|
|
|
|
* Instanciates a new Mapper
|
|
|
|
*
|
2011-04-20 22:53:59 +02:00
|
|
|
* @param ObjectManager objectManager
|
|
|
|
* @param string $objectClass
|
2011-04-15 19:35:11 +02:00
|
|
|
* @param array $options
|
2011-04-15 01:22:14 +02:00
|
|
|
*/
|
2011-04-20 22:53:59 +02:00
|
|
|
public function __construct(ObjectManager $objectManager, $objectClass, array $options = array())
|
2011-04-15 01:22:14 +02:00
|
|
|
{
|
2011-04-20 22:53:59 +02:00
|
|
|
$this->objectManager = $objectManager;
|
|
|
|
$this->objectClass = $objectClass;
|
|
|
|
$this->options = array_merge($this->options, $options);
|
2011-04-15 01:22:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Transforms an array of elastica objects into an array of
|
|
|
|
* model objects fetched from the doctrine repository
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
**/
|
|
|
|
public function fromElasticaObjects(array $elasticaObjects)
|
|
|
|
{
|
|
|
|
$ids = array_map(function($elasticaObject) {
|
|
|
|
return $elasticaObject->getId();
|
|
|
|
}, $elasticaObjects);
|
|
|
|
|
2011-04-20 22:53:59 +02:00
|
|
|
return $this->objectManager
|
|
|
|
->createQueryBuilder($this->objectClass)
|
|
|
|
->field($this->options['identifier'])->in($ids)
|
2011-04-15 19:35:11 +02:00
|
|
|
->hydrate($this->options['hydrate'])
|
2011-04-15 01:22:14 +02:00
|
|
|
->getQuery()
|
2011-04-20 02:25:53 +02:00
|
|
|
->execute()
|
|
|
|
->toArray();
|
2011-04-15 01:22:14 +02:00
|
|
|
}
|
|
|
|
}
|