diff --git a/Tests/Transformer/ElasticaToModelTransformerCollectionTest.php b/Tests/Transformer/ElasticaToModelTransformerCollectionTest.php index e4beef4..e739959 100644 --- a/Tests/Transformer/ElasticaToModelTransformerCollectionTest.php +++ b/Tests/Transformer/ElasticaToModelTransformerCollectionTest.php @@ -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 diff --git a/Transformer/ElasticaToModelTransformerCollection.php b/Transformer/ElasticaToModelTransformerCollection.php index 506a0a8..33bd698 100644 --- a/Transformer/ElasticaToModelTransformerCollection.php +++ b/Transformer/ElasticaToModelTransformerCollection.php @@ -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)); + $transformedObjects = $this->transformers[$type]->transform($objects); + $transformed[$type] = array_combine(array_map(function($o) use ($identifierGetter) {return $o->$identifierGetter();},$transformedObjects),$transformedObjects); } - $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)