2011-12-22 00:05:24 +01:00
|
|
|
<?php
|
|
|
|
|
2013-03-27 18:44:06 +01:00
|
|
|
namespace FOS\ElasticaBundle\Propel;
|
2011-12-22 00:05:24 +01:00
|
|
|
|
2013-03-27 18:44:06 +01:00
|
|
|
use FOS\ElasticaBundle\HybridResult;
|
|
|
|
use FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerInterface;
|
2013-04-04 15:44:42 +02:00
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
|
2011-12-22 00:05:24 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Maps Elastica documents with Propel objects
|
|
|
|
* This mapper assumes an exact match between
|
|
|
|
* elastica documents ids and propel object ids
|
|
|
|
*
|
|
|
|
* @author William Durand <william.durand1@gmail.com>
|
|
|
|
*/
|
|
|
|
class ElasticaToModelTransformer implements ElasticaToModelTransformerInterface
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* 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'
|
|
|
|
);
|
|
|
|
|
2013-04-04 15:44:42 +02:00
|
|
|
/**
|
|
|
|
* PropertyAccessor instance
|
|
|
|
*
|
|
|
|
* @var PropertyAccessorInterface
|
|
|
|
*/
|
|
|
|
protected $propertyAccessor;
|
|
|
|
|
2011-12-22 00:05:24 +01:00
|
|
|
/**
|
|
|
|
* Instantiates a new Mapper
|
|
|
|
*
|
|
|
|
* @param string $objectClass
|
|
|
|
* @param array $options
|
|
|
|
*/
|
|
|
|
public function __construct($objectClass, array $options = array())
|
|
|
|
{
|
|
|
|
$this->objectClass = $objectClass;
|
|
|
|
$this->options = array_merge($this->options, $options);
|
|
|
|
}
|
|
|
|
|
2013-04-04 15:44:42 +02:00
|
|
|
/**
|
|
|
|
* Set the PropertyAccessor
|
|
|
|
*
|
|
|
|
* @param PropertyAccessorInterface $propertyAccessor
|
|
|
|
*/
|
|
|
|
public function setPropertyAccessor(PropertyAccessorInterface $propertyAccessor)
|
|
|
|
{
|
|
|
|
$this->propertyAccessor = $propertyAccessor;
|
|
|
|
}
|
|
|
|
|
2011-12-22 00:05:24 +01:00
|
|
|
/**
|
|
|
|
* Transforms an array of elastica objects into an array of
|
|
|
|
* model objects fetched from the propel repository
|
|
|
|
*
|
2013-04-22 13:20:10 +02:00
|
|
|
* @param Document[] $elasticaObjects array of elastica objects
|
2011-12-22 00:05:24 +01:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function transform(array $elasticaObjects)
|
|
|
|
{
|
2013-08-06 20:48:38 +02:00
|
|
|
$ids = array();
|
|
|
|
foreach ($elasticaObjects as $elasticaObject) {
|
|
|
|
$ids[] = $elasticaObject->getId();
|
|
|
|
}
|
2011-12-22 00:05:24 +01:00
|
|
|
|
2012-07-10 22:28:31 +02:00
|
|
|
$objects = $this->findByIdentifiers($ids, $this->options['hydrate']);
|
2011-12-22 00:05:24 +01:00
|
|
|
|
|
|
|
// sort objects in the order of ids
|
|
|
|
$idPos = array_flip($ids);
|
2013-04-04 15:44:42 +02:00
|
|
|
$identifier = $this->options['identifier'];
|
|
|
|
$propertyAccessor = $this->propertyAccessor;
|
2012-05-04 15:44:00 +02:00
|
|
|
if (is_object($objects)) {
|
2013-04-04 15:44:42 +02:00
|
|
|
$objects->uasort(function($a, $b) use ($idPos, $identifier, $propertyAccessor) {
|
|
|
|
return $idPos[$propertyAccessor->getValue($a, $identifier)] > $idPos[$propertyAccessor->getValue($b, $identifier)];
|
2012-05-04 15:44:00 +02:00
|
|
|
});
|
|
|
|
} else {
|
2013-04-04 15:44:42 +02:00
|
|
|
usort($objects, function($a, $b) use ($idPos, $identifier, $propertyAccessor) {
|
|
|
|
return $idPos[$propertyAccessor->getValue($a, $identifier)] > $idPos[$propertyAccessor->getValue($b, $identifier)];
|
2012-05-04 15:44:00 +02:00
|
|
|
});
|
|
|
|
}
|
2011-12-22 00:05:24 +01:00
|
|
|
|
|
|
|
return $objects;
|
|
|
|
}
|
|
|
|
|
2012-05-04 15:44:00 +02:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
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 getObjectClass()
|
|
|
|
{
|
|
|
|
return $this->objectClass;
|
|
|
|
}
|
2011-12-22 00:05:24 +01:00
|
|
|
|
2012-09-25 00:15:05 +02:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function getIdentifierField()
|
|
|
|
{
|
|
|
|
return $this->options['identifier'];
|
|
|
|
}
|
|
|
|
|
2011-12-22 00:05:24 +01:00
|
|
|
/**
|
|
|
|
* Fetch objects for theses identifier values
|
|
|
|
*
|
|
|
|
* @param array $identifierValues ids values
|
2013-03-02 12:23:44 +01:00
|
|
|
* @param boolean $hydrate whether or not to hydrate the objects, false returns arrays
|
2011-12-22 00:05:24 +01:00
|
|
|
* @return array of objects or arrays
|
|
|
|
*/
|
2012-07-10 22:28:31 +02:00
|
|
|
protected function findByIdentifiers(array $identifierValues, $hydrate)
|
2011-12-22 00:05:24 +01:00
|
|
|
{
|
|
|
|
if (empty($identifierValues)) {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
2012-07-10 22:28:31 +02:00
|
|
|
$query = $this->createQuery($this->objectClass, $this->options['identifier'], $identifierValues);
|
2011-12-22 00:05:24 +01:00
|
|
|
|
|
|
|
if (!$hydrate) {
|
|
|
|
return $query->toArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $query->find();
|
|
|
|
}
|
|
|
|
|
2012-05-05 18:43:56 +02:00
|
|
|
/**
|
|
|
|
* Create a query to use in the findByIdentifiers() method.
|
|
|
|
*
|
|
|
|
* @param string $class the model class
|
|
|
|
* @param string $identifierField like 'id'
|
|
|
|
* @param array $identifierValues ids values
|
|
|
|
* @return \ModelCriteria
|
|
|
|
*/
|
|
|
|
protected function createQuery($class, $identifierField, array $identifierValues)
|
|
|
|
{
|
|
|
|
$queryClass = $class.'Query';
|
|
|
|
$filterMethod = 'filterBy'.$this->camelize($identifierField);
|
|
|
|
|
|
|
|
return $queryClass::create()
|
|
|
|
->$filterMethod($identifierValues)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2011-12-22 00:05:24 +01:00
|
|
|
/**
|
|
|
|
* @see https://github.com/doctrine/common/blob/master/lib/Doctrine/Common/Util/Inflector.php
|
|
|
|
*/
|
|
|
|
private function camelize($str)
|
|
|
|
{
|
|
|
|
return ucfirst(str_replace(" ", "", ucwords(strtr($str, "_-", " "))));
|
|
|
|
}
|
|
|
|
}
|