Merge pull request #350

This commit is contained in:
Jeremy Mikola 2013-08-09 13:57:16 -04:00
commit 0b3c1ebbc2

View file

@ -2,46 +2,46 @@
namespace FOS\ElasticaBundle\Propel; namespace FOS\ElasticaBundle\Propel;
use Elastica\Document;
use FOS\ElasticaBundle\HybridResult; use FOS\ElasticaBundle\HybridResult;
use FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerInterface; use FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerInterface;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface; use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
/** /**
* Maps Elastica documents with Propel objects * Maps Elastica documents with Propel objects.
* This mapper assumes an exact match between *
* elastica documents ids and propel object ids * This mapper assumes an exact match between Elastica document IDs and Propel
* entity IDs.
* *
* @author William Durand <william.durand1@gmail.com> * @author William Durand <william.durand1@gmail.com>
*/ */
class ElasticaToModelTransformer implements ElasticaToModelTransformerInterface class ElasticaToModelTransformer implements ElasticaToModelTransformerInterface
{ {
/** /**
* Class of the model to map to the elastica documents * Propel model class to map to Elastica documents.
* *
* @var string * @var string
*/ */
protected $objectClass = null; protected $objectClass = null;
/** /**
* Optional parameters * Transformer options.
* *
* @var array * @var array
*/ */
protected $options = array( protected $options = array(
'hydrate' => true, 'hydrate' => true,
'identifier' => 'id' 'identifier' => 'id',
); );
/** /**
* PropertyAccessor instance * PropertyAccessor instance.
* *
* @var PropertyAccessorInterface * @var PropertyAccessorInterface
*/ */
protected $propertyAccessor; protected $propertyAccessor;
/** /**
* Instantiates a new Mapper * Constructor.
* *
* @param string $objectClass * @param string $objectClass
* @param array $options * @param array $options
@ -49,11 +49,11 @@ class ElasticaToModelTransformer implements ElasticaToModelTransformerInterface
public function __construct($objectClass, array $options = array()) public function __construct($objectClass, array $options = array())
{ {
$this->objectClass = $objectClass; $this->objectClass = $objectClass;
$this->options = array_merge($this->options, $options); $this->options = array_merge($this->options, $options);
} }
/** /**
* Set the PropertyAccessor * Set the PropertyAccessor instance.
* *
* @param PropertyAccessorInterface $propertyAccessor * @param PropertyAccessorInterface $propertyAccessor
*/ */
@ -63,32 +63,34 @@ class ElasticaToModelTransformer implements ElasticaToModelTransformerInterface
} }
/** /**
* Transforms an array of elastica objects into an array of * Transforms an array of Elastica document into an array of Propel entities
* model objects fetched from the propel repository * fetched from the database.
* *
* @param Document[] $elasticaObjects array of elastica objects * @param array $elasticaObjects
* @return array * @return array|\ArrayObject
*/ */
public function transform(array $elasticaObjects) public function transform(array $elasticaObjects)
{ {
$ids = array_map(function(Document $elasticaObject) { $ids = array();
return $elasticaObject->getId(); foreach ($elasticaObjects as $elasticaObject) {
}, $elasticaObjects); $ids[] = $elasticaObject->getId();
}
$objects = $this->findByIdentifiers($ids, $this->options['hydrate']); $objects = $this->findByIdentifiers($ids, $this->options['hydrate']);
// sort objects in the order of ids // Sort objects in the order of their IDs
$idPos = array_flip($ids); $idPos = array_flip($ids);
$identifier = $this->options['identifier']; $identifier = $this->options['identifier'];
$propertyAccessor = $this->propertyAccessor; $propertyAccessor = $this->propertyAccessor;
$sortCallback = function($a, $b) use ($idPos, $identifier, $propertyAccessor) {
return $idPos[$propertyAccessor->getValue($a, $identifier)] > $idPos[$propertyAccessor->getValue($b, $identifier)];
};
if (is_object($objects)) { if (is_object($objects)) {
$objects->uasort(function($a, $b) use ($idPos, $identifier, $propertyAccessor) { $objects->uasort($sortCallback);
return $idPos[$propertyAccessor->getValue($a, $identifier)] > $idPos[$propertyAccessor->getValue($b, $identifier)];
});
} else { } else {
usort($objects, function($a, $b) use ($idPos, $identifier, $propertyAccessor) { usort($objects, $sortCallback);
return $idPos[$propertyAccessor->getValue($a, $identifier)] > $idPos[$propertyAccessor->getValue($b, $identifier)];
});
} }
return $objects; return $objects;
@ -126,11 +128,14 @@ class ElasticaToModelTransformer implements ElasticaToModelTransformerInterface
} }
/** /**
* Fetch objects for theses identifier values * Fetch Propel entities for the given identifier values.
* *
* @param array $identifierValues ids values * If $hydrate is false, the returned array elements will be arrays.
* @param boolean $hydrate whether or not to hydrate the objects, false returns arrays * Otherwise, the results will be hydrated to instances of the model class.
* @return array of objects or arrays *
* @param array $identifierValues Identifier values
* @param boolean $hydrate Whether or not to hydrate the results
* @return array
*/ */
protected function findByIdentifiers(array $identifierValues, $hydrate) protected function findByIdentifiers(array $identifierValues, $hydrate)
{ {
@ -140,7 +145,7 @@ class ElasticaToModelTransformer implements ElasticaToModelTransformerInterface
$query = $this->createQuery($this->objectClass, $this->options['identifier'], $identifierValues); $query = $this->createQuery($this->objectClass, $this->options['identifier'], $identifierValues);
if (!$hydrate) { if ( ! $hydrate) {
return $query->toArray(); return $query->toArray();
} }
@ -150,9 +155,9 @@ class ElasticaToModelTransformer implements ElasticaToModelTransformerInterface
/** /**
* Create a query to use in the findByIdentifiers() method. * Create a query to use in the findByIdentifiers() method.
* *
* @param string $class the model class * @param string $class Propel model class
* @param string $identifierField like 'id' * @param string $identifierField Identifier field name (e.g. "id")
* @param array $identifierValues ids values * @param array $identifierValues Identifier values
* @return \ModelCriteria * @return \ModelCriteria
*/ */
protected function createQuery($class, $identifierField, array $identifierValues) protected function createQuery($class, $identifierField, array $identifierValues)
@ -160,9 +165,7 @@ class ElasticaToModelTransformer implements ElasticaToModelTransformerInterface
$queryClass = $class.'Query'; $queryClass = $class.'Query';
$filterMethod = 'filterBy'.$this->camelize($identifierField); $filterMethod = 'filterBy'.$this->camelize($identifierField);
return $queryClass::create() return $queryClass::create()->$filterMethod($identifierValues);
->$filterMethod($identifierValues)
;
} }
/** /**