Fixed bug when a transformer returns objects of different classes.\n The doctrine transformer can generate this kind of situations whene it returns proxy objects.

This commit is contained in:
fran6co 2012-05-02 23:06:41 -03:00
parent 21abb977ac
commit 1c1c33d327
2 changed files with 31 additions and 15 deletions

View file

@ -67,6 +67,28 @@ class ElasticaToModelTransformerCollectionTest extends \PHPUnit_Framework_TestCa
$result2,
), $results);
}
public function testTransformOrder()
{
$this->collectionSetup();
$document1 = new \Elastica_Document(123, array('data' => 'lots of data'), 'type1');
$document2 = new \Elastica_Document(124, array('data' => 'not so much data'), 'type1');
$result1 = new POPO(123, 'lots of data');
$result2 = new POPO2(124, 'not so much data');
$this->transformers['type1']->expects($this->once())
->method('transform')
->with(array($document1,$document2))
->will($this->returnValue(array($result1,$result2)));
$results = $this->collection->transform(array($document1, $document2));
$this->assertEquals(array(
$result1,
$result2,
), $results);
}
}
class POPO

View file

@ -32,30 +32,24 @@ class ElasticaToModelTransformerCollection implements ElasticaToModelTransformer
public function transform(array $elasticaObjects)
{
$sorted = array();
$order = array();
foreach ($elasticaObjects as $object) {
$sorted[$object->getType()][] = $object;
$order[] = sprintf('%s-%s', $object->getType(), $object->getId());
}
$identifierGetter = 'get' . ucfirst($this->options['identifier']);
$transformed = array();
foreach ($sorted AS $type => $objects) {
$transformed = array_merge($transformed, $this->transformers[$type]->transform($objects));
$transformed[$type] = $this->transformers[$type]->transform($objects);
$transformed[$type] = array_combine(array_map(function($o) use ($identifierGetter) {return $o->$identifierGetter();},$transformed[$type]),$transformed[$type]);
}
$positions = array_flip($order);
$identifierGetter = 'get' . ucfirst($this->options['identifier']);
$classMap = $this->getTypeToClassMap();
$result = array();
foreach ($elasticaObjects as $object) {
$result[] = $transformed[$object->getType()][$object->getId()];
}
usort($transformed, function($a, $b) use ($positions, $identifierGetter, $classMap)
{
$aType = array_search(get_class($a), $classMap);
$bType = array_search(get_class($b), $classMap);
return $positions["{$aType}-{$a->$identifierGetter()}"] > $positions["{$bType}-{$b->$identifierGetter()}"];
});
return $transformed;
return $result;
}
public function hybridTransform(array $elasticaObjects)