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:
parent
da194b581c
commit
927030914c
|
@ -67,6 +67,28 @@ class ElasticaToModelTransformerCollectionTest extends \PHPUnit_Framework_TestCa
|
||||||
$result2,
|
$result2,
|
||||||
), $results);
|
), $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
|
class POPO
|
||||||
|
|
|
@ -32,30 +32,24 @@ class ElasticaToModelTransformerCollection implements ElasticaToModelTransformer
|
||||||
public function transform(array $elasticaObjects)
|
public function transform(array $elasticaObjects)
|
||||||
{
|
{
|
||||||
$sorted = array();
|
$sorted = array();
|
||||||
$order = array();
|
|
||||||
foreach ($elasticaObjects as $object) {
|
foreach ($elasticaObjects as $object) {
|
||||||
$sorted[$object->getType()][] = $object;
|
$sorted[$object->getType()][] = $object;
|
||||||
$order[] = sprintf('%s-%s', $object->getType(), $object->getId());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$identifierGetter = 'get' . ucfirst($this->options['identifier']);
|
||||||
|
|
||||||
$transformed = array();
|
$transformed = array();
|
||||||
foreach ($sorted AS $type => $objects) {
|
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);
|
$result = array();
|
||||||
$identifierGetter = 'get' . ucfirst($this->options['identifier']);
|
foreach ($elasticaObjects as $object) {
|
||||||
$classMap = $this->getTypeToClassMap();
|
$result[] = $transformed[$object->getType()][$object->getId()];
|
||||||
|
}
|
||||||
|
|
||||||
usort($transformed, function($a, $b) use ($positions, $identifierGetter, $classMap)
|
return $result;
|
||||||
{
|
|
||||||
$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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function hybridTransform(array $elasticaObjects)
|
public function hybridTransform(array $elasticaObjects)
|
||||||
|
|
Loading…
Reference in a new issue