Fixing identifier field not being set in TransformerCollection

This commit is contained in:
Richard Miller 2012-09-24 23:15:05 +01:00
commit 5f1e1326d5
6 changed files with 53 additions and 10 deletions

View file

@ -12,14 +12,10 @@ use FOQ\ElasticaBundle\HybridResult;
class ElasticaToModelTransformerCollection implements ElasticaToModelTransformerInterface
{
protected $transformers = array();
protected $options = array(
'identifier' => 'id'
);
public function __construct(array $transformers, array $options)
public function __construct(array $transformers)
{
$this->transformers = $transformers;
$this->options = array_merge($this->options, $options);
}
public function getObjectClass()
@ -29,6 +25,16 @@ class ElasticaToModelTransformerCollection implements ElasticaToModelTransformer
}, $this->transformers);
}
/**
* {@inheritdoc}
*/
public function getIdentifierField()
{
return array_map(function ($transformer) {
return $transformer->getIdentifierField();
}, $this->transformers);
}
public function transform(array $elasticaObjects)
{
$sorted = array();
@ -36,12 +42,19 @@ class ElasticaToModelTransformerCollection implements ElasticaToModelTransformer
$sorted[$object->getType()][] = $object;
}
$identifierGetter = 'get' . ucfirst($this->options['identifier']);
$transformed = array();
foreach ($sorted AS $type => $objects) {
$transformedObjects = $this->transformers[$type]->transform($objects);
$transformed[$type] = array_combine(array_map(function($o) use ($identifierGetter) {return $o->$identifierGetter();},$transformedObjects),$transformedObjects);
$identifierGetter = 'get' . ucfirst($this->transformers[$type]->getIdentifierField());
$transformed[$type] = array_combine(
array_map(
function($o) use ($identifierGetter) {
return $o->$identifierGetter();
},
$transformedObjects
),
$transformedObjects
);
}
$result = array();
@ -70,4 +83,4 @@ class ElasticaToModelTransformerCollection implements ElasticaToModelTransformer
return $transformer->getObjectClass();
}, $this->transformers);
}
}
}