Move common sorting code to base Transformer class

This commit is contained in:
Tim Nagel 2015-03-12 22:17:57 +11:00
parent 925410a66e
commit 4af9f442fd
3 changed files with 57 additions and 46 deletions

View file

@ -3,7 +3,7 @@
namespace FOS\ElasticaBundle\Doctrine; namespace FOS\ElasticaBundle\Doctrine;
use FOS\ElasticaBundle\HybridResult; use FOS\ElasticaBundle\HybridResult;
use FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerInterface; use FOS\ElasticaBundle\Transformer\AbstractElasticaToModelTransformer as BaseTransformer;
use FOS\ElasticaBundle\Transformer\HighlightableModelInterface; use FOS\ElasticaBundle\Transformer\HighlightableModelInterface;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface; use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
@ -12,7 +12,7 @@ use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
* This mapper assumes an exact match between * This mapper assumes an exact match between
* elastica documents ids and doctrine object ids. * elastica documents ids and doctrine object ids.
*/ */
abstract class AbstractElasticaToModelTransformer implements ElasticaToModelTransformerInterface abstract class AbstractElasticaToModelTransformer extends BaseTransformer
{ {
/** /**
* Manager registry. * Manager registry.
@ -38,13 +38,6 @@ abstract class AbstractElasticaToModelTransformer implements ElasticaToModelTran
'query_builder_method' => 'createQueryBuilder', 'query_builder_method' => 'createQueryBuilder',
); );
/**
* PropertyAccessor instance.
*
* @var PropertyAccessorInterface
*/
protected $propertyAccessor;
/** /**
* Instantiates a new Mapper. * Instantiates a new Mapper.
* *
@ -69,16 +62,6 @@ abstract class AbstractElasticaToModelTransformer implements ElasticaToModelTran
return $this->objectClass; return $this->objectClass;
} }
/**
* Set the PropertyAccessor.
*
* @param PropertyAccessorInterface $propertyAccessor
*/
public function setPropertyAccessor(PropertyAccessorInterface $propertyAccessor)
{
$this->propertyAccessor = $propertyAccessor;
}
/** /**
* Transforms an array of elastica objects into an array of * Transforms an array of elastica objects into an array of
* model objects fetched from the doctrine repository. * model objects fetched from the doctrine repository.
@ -111,10 +94,7 @@ abstract class AbstractElasticaToModelTransformer implements ElasticaToModelTran
// sort objects in the order of ids // sort objects in the order of ids
$idPos = array_flip($ids); $idPos = array_flip($ids);
$identifier = $this->options['identifier']; $identifier = $this->options['identifier'];
$propertyAccessor = $this->propertyAccessor; usort($objects, $this->getSortingClosure($idPos, $identifier));
usort($objects, function ($a, $b) use ($idPos, $identifier, $propertyAccessor) {
return $idPos[$propertyAccessor->getValue($a, $identifier)] > $idPos[$propertyAccessor->getValue($b, $identifier)];
});
return $objects; return $objects;
} }

View file

@ -3,6 +3,7 @@
namespace FOS\ElasticaBundle\Propel; namespace FOS\ElasticaBundle\Propel;
use FOS\ElasticaBundle\HybridResult; use FOS\ElasticaBundle\HybridResult;
use FOS\ElasticaBundle\Transformer\AbstractElasticaToModelTransformer;
use FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerInterface; use FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerInterface;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface; use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
@ -14,7 +15,7 @@ use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
* *
* @author William Durand <william.durand1@gmail.com> * @author William Durand <william.durand1@gmail.com>
*/ */
class ElasticaToModelTransformer implements ElasticaToModelTransformerInterface class ElasticaToModelTransformer extends AbstractElasticaToModelTransformer
{ {
/** /**
* Propel model class to map to Elastica documents. * Propel model class to map to Elastica documents.
@ -33,13 +34,6 @@ class ElasticaToModelTransformer implements ElasticaToModelTransformerInterface
'identifier' => 'id', 'identifier' => 'id',
); );
/**
* PropertyAccessor instance.
*
* @var PropertyAccessorInterface
*/
protected $propertyAccessor;
/** /**
* Constructor. * Constructor.
* *
@ -52,16 +46,6 @@ class ElasticaToModelTransformer implements ElasticaToModelTransformerInterface
$this->options = array_merge($this->options, $options); $this->options = array_merge($this->options, $options);
} }
/**
* Set the PropertyAccessor instance.
*
* @param PropertyAccessorInterface $propertyAccessor
*/
public function setPropertyAccessor(PropertyAccessorInterface $propertyAccessor)
{
$this->propertyAccessor = $propertyAccessor;
}
/** /**
* Transforms an array of Elastica document into an array of Propel entities * Transforms an array of Elastica document into an array of Propel entities
* fetched from the database. * fetched from the database.
@ -82,11 +66,7 @@ class ElasticaToModelTransformer implements ElasticaToModelTransformerInterface
// Sort objects in the order of their 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; $sortCallback = $this->getSortingClosure($idPos, $identifier);
$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($sortCallback); $objects->uasort($sortCallback);

View file

@ -0,0 +1,51 @@
<?php
/**
* This file is part of the FOSElasticaBundle project.
*
* (c) FriendsOfSymfony <https://github.com/FriendsOfSymfony/FOSElasticaBundle/graphs/contributors>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FOS\ElasticaBundle\Transformer;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
abstract class AbstractElasticaToModelTransformer implements ElasticaToModelTransformerInterface
{
/**
* PropertyAccessor instance.
*
* @var PropertyAccessorInterface
*/
protected $propertyAccessor;
/**
* Set the PropertyAccessor instance.
*
* @param PropertyAccessorInterface $propertyAccessor
*/
public function setPropertyAccessor(PropertyAccessorInterface $propertyAccessor)
{
$this->propertyAccessor = $propertyAccessor;
}
/**
* Returns a sorting closure to be used with usort() to put retrieved objects
* back in the order that they were returned by ElasticSearch.
*
* @param array $idPos
* @param string $identifierPath
* @return callable
*/
protected function getSortingClosure(array $idPos, $identifierPath)
{
$propertyAccessor = $this->propertyAccessor;
return function ($a, $b) use ($idPos, $identifierPath, $propertyAccessor) {
return $idPos[$propertyAccessor->getValue($a, $identifierPath)] > $idPos[$propertyAccessor->getValue($b, $identifierPath)];
};
}
}