Complete provider, finder, transformers and configuration refactoring
This commit is contained in:
parent
7be25f92bb
commit
0db0490be5
17 changed files with 444 additions and 320 deletions
85
Transformer/ElasticaToModelDoctrineTransformer.php
Normal file
85
Transformer/ElasticaToModelDoctrineTransformer.php
Normal file
|
|
@ -0,0 +1,85 @@
|
|||
<?php
|
||||
|
||||
namespace FOQ\ElasticaBundle\Transformer;
|
||||
|
||||
use Doctrine\Common\Persistence\ObjectManager;
|
||||
use Elastica_Document;
|
||||
|
||||
/**
|
||||
* Maps Elastica documents with Doctrine objects
|
||||
* This mapper assumes an exact match between
|
||||
* elastica documents ids and doctrine object ids
|
||||
*/
|
||||
class ElasticaToModelDoctrineTransformer implements ElasticaToModelTransformerInterface
|
||||
{
|
||||
/**
|
||||
* Repository to fetch the objects from
|
||||
*
|
||||
* @var ObjectManager
|
||||
*/
|
||||
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'
|
||||
);
|
||||
|
||||
/**
|
||||
* Instanciates a new Mapper
|
||||
*
|
||||
* @param ObjectManager objectManager
|
||||
* @param string $objectClass
|
||||
* @param array $options
|
||||
*/
|
||||
public function __construct(ObjectManager $objectManager, $objectClass, array $options = array())
|
||||
{
|
||||
$this->objectManager = $objectManager;
|
||||
$this->objectClass = $objectClass;
|
||||
$this->options = array_merge($this->options, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 = array_map(function($elasticaObject) {
|
||||
return $elasticaObject->getId();
|
||||
}, $elasticaObjects);
|
||||
|
||||
$objects = $this->objectManager
|
||||
->createQueryBuilder($this->objectClass)
|
||||
->field($this->options['identifier'])->in($ids)
|
||||
->hydrate($this->options['hydrate'])
|
||||
->getQuery()
|
||||
->execute()
|
||||
->toArray();
|
||||
|
||||
$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;
|
||||
}
|
||||
}
|
||||
18
Transformer/ElasticaToModelTransformerInterface.php
Normal file
18
Transformer/ElasticaToModelTransformerInterface.php
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
<?php
|
||||
|
||||
namespace FOQ\ElasticaBundle\Transformer;
|
||||
|
||||
/**
|
||||
* Maps Elastica documents with model objects
|
||||
*/
|
||||
interface ElasticaToModelTransformerInterface
|
||||
{
|
||||
/**
|
||||
* Transforms an array of elastica objects into an array of
|
||||
* model objects fetched from the doctrine repository
|
||||
*
|
||||
* @param array of elastica objects
|
||||
* @return array of model objects
|
||||
**/
|
||||
function transform(array $elasticaObjects);
|
||||
}
|
||||
78
Transformer/ModelToElasticaAutoTransformer.php
Normal file
78
Transformer/ModelToElasticaAutoTransformer.php
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
<?php
|
||||
|
||||
namespace FOQ\ElasticaBundle\Transformer;
|
||||
|
||||
use Doctrine\Common\Persistence\ObjectManager;
|
||||
use Elastica_Document;
|
||||
use Traversable;
|
||||
use ArrayAccess;
|
||||
|
||||
/**
|
||||
* Maps Elastica documents with Doctrine objects
|
||||
* This mapper assumes an exact match between
|
||||
* elastica documents ids and doctrine object ids
|
||||
*/
|
||||
class ModelToElasticaAutoTransformer implements ModelToElasticaTransformerInterface
|
||||
{
|
||||
/**
|
||||
* Optional parameters
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $options = array(
|
||||
'identifier' => 'id'
|
||||
);
|
||||
|
||||
/**
|
||||
* Instanciates a new Mapper
|
||||
*
|
||||
* @param array $options
|
||||
*/
|
||||
public function __construct(array $options = array())
|
||||
{
|
||||
$this->options = array_merge($this->options, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms an object into an elastica object having the required keys
|
||||
*
|
||||
* @param object $object the object to convert
|
||||
* @param array $fields the keys we want to have in the returned array
|
||||
* @return Elastica_Document
|
||||
**/
|
||||
public function transform($object, array $fields)
|
||||
{
|
||||
$class = get_class($object);
|
||||
$array = array();
|
||||
foreach ($fields as $key) {
|
||||
$getter = 'get'.ucfirst($key);
|
||||
if (!method_exists($class, $getter)) {
|
||||
throw new RuntimeException(sprintf('The getter %s::%s does not exist', $class, $getter));
|
||||
}
|
||||
$array[$key] = $this->normalizeValue($object->$getter());
|
||||
}
|
||||
$identifierGetter = 'get'.ucfirst($this->options['identifier']);
|
||||
$identifier = $object->$identifierGetter();
|
||||
|
||||
return new Elastica_Document($identifier, array_filter($array));
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to convert any type to a string or an array of strings
|
||||
*
|
||||
* @param mixed $value
|
||||
* @return string|array
|
||||
*/
|
||||
protected function normalizeValue($value)
|
||||
{
|
||||
if (is_array($value) || $value instanceof Traversable || $value instanceof ArrayAccess) {
|
||||
$value = array_map(function($v) {
|
||||
return (string) $v;
|
||||
}, is_array($value) ? $value : iterator_to_array($value));
|
||||
} else {
|
||||
$value = (string) $value;
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
18
Transformer/ModelToElasticaTransformerInterface.php
Normal file
18
Transformer/ModelToElasticaTransformerInterface.php
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
<?php
|
||||
|
||||
namespace FOQ\ElasticaBundle\Transformer;
|
||||
|
||||
/**
|
||||
* Maps Elastica documents with model objects
|
||||
*/
|
||||
interface ModelToElasticaTransformerInterface
|
||||
{
|
||||
/**
|
||||
* Transforms an object into an elastica object having the required keys
|
||||
*
|
||||
* @param object $object the object to convert
|
||||
* @param array $fields the keys we want to have in the returned array
|
||||
* @return Elastica_Document
|
||||
**/
|
||||
function transform($object, array $fields);
|
||||
}
|
||||
|
|
@ -1,50 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace FOQ\ElasticaBundle\Transformer;
|
||||
|
||||
use RuntimeException;
|
||||
use Traversable;
|
||||
use ArrayAccess;
|
||||
|
||||
/**
|
||||
* AutomaticObjectToArrayTransformer
|
||||
* Tries to convert objects by generating getters
|
||||
* based on the required keys
|
||||
*/
|
||||
class ObjectToArrayAutomaticTransformer implements ObjectToArrayTransformerInterface
|
||||
{
|
||||
/**
|
||||
* Transforms an object into an array having the required keys
|
||||
*
|
||||
* @param object $object the object to convert
|
||||
* @param array $requiredKeys the keys we want to have in the returned array
|
||||
* @return array
|
||||
**/
|
||||
public function transform($object, array $requiredKeys)
|
||||
{
|
||||
$class = get_class($object);
|
||||
$array = array();
|
||||
foreach ($requiredKeys as $key) {
|
||||
$getter = 'get'.ucfirst($key);
|
||||
if (!method_exists($class, $getter)) {
|
||||
throw new RuntimeException(sprintf('The getter %s::%s does not exist', $class, $getter));
|
||||
}
|
||||
$array[$key] = $this->normalizeValue($object->$getter());
|
||||
}
|
||||
|
||||
return array_filter($array);
|
||||
}
|
||||
|
||||
public function normalizeValue($value)
|
||||
{
|
||||
if (is_array($value) || $value instanceof Traversable || $value instanceof ArrayAccess) {
|
||||
$value = array_map(function($v) {
|
||||
return (string) $v;
|
||||
}, is_array($value) ? $value : iterator_to_array($value));
|
||||
} else {
|
||||
$value = (string) $value;
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,15 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace FOQ\ElasticaBundle\Transformer;
|
||||
|
||||
interface ObjectToArrayTransformerInterface
|
||||
{
|
||||
/**
|
||||
* Transforms an object into an array having the required keys
|
||||
*
|
||||
* @param object $object the object to convert
|
||||
* @param array $requiredKeys the keys we want to have in the returned array
|
||||
* @return array
|
||||
**/
|
||||
public function transform($object, array $requiredKeys);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue