diff --git a/Doctrine/AbstractElasticaToModelTransformer.php b/Doctrine/AbstractElasticaToModelTransformer.php index 8774106..9a74cfa 100755 --- a/Doctrine/AbstractElasticaToModelTransformer.php +++ b/Doctrine/AbstractElasticaToModelTransformer.php @@ -111,6 +111,14 @@ abstract class AbstractElasticaToModelTransformer implements ElasticaToModelTran return $result; } + /** + * {@inheritdoc} + */ + public function getIdentifierField() + { + return $this->options['identifier']; + } + /** * Fetches objects by theses identifier values * diff --git a/Propel/ElasticaToModelTransformer.php b/Propel/ElasticaToModelTransformer.php index 16499c6..824b523 100644 --- a/Propel/ElasticaToModelTransformer.php +++ b/Propel/ElasticaToModelTransformer.php @@ -100,6 +100,14 @@ class ElasticaToModelTransformer implements ElasticaToModelTransformerInterface return $this->objectClass; } + /** + * {@inheritdoc} + */ + public function getIdentifierField() + { + return $this->options['identifier']; + } + /** * Fetch objects for theses identifier values * diff --git a/Resources/config/config.xml b/Resources/config/config.xml index 95c5406..b6b9c92 100644 --- a/Resources/config/config.xml +++ b/Resources/config/config.xml @@ -61,7 +61,6 @@ - diff --git a/Tests/Transformer/ElasticaToModelTransformerCollectionTest.php b/Tests/Transformer/ElasticaToModelTransformerCollectionTest.php index e739959..05ddaaa 100644 --- a/Tests/Transformer/ElasticaToModelTransformerCollectionTest.php +++ b/Tests/Transformer/ElasticaToModelTransformerCollectionTest.php @@ -19,11 +19,19 @@ class ElasticaToModelTransformerCollectionTest extends \PHPUnit_Framework_TestCa ->method('getObjectClass') ->will($this->returnValue('FOQ\ElasticaBundle\Tests\Transformer\POPO')); + $transformer1->expects($this->any()) + ->method('getIdentifierField') + ->will($this->returnValue('id')); + $transformer2 = $this->getMock('FOQ\ElasticaBundle\Transformer\ElasticaToModelTransformerInterface'); $transformer2->expects($this->any()) ->method('getObjectClass') ->will($this->returnValue('FOQ\ElasticaBundle\Tests\Transformer\POPO2')); + $transformer2->expects($this->any()) + ->method('getIdentifierField') + ->will($this->returnValue('id')); + $this->collection = new ElasticaToModelTransformerCollection($this->transformers = array( 'type1' => $transformer1, 'type2' => $transformer2, diff --git a/Transformer/ElasticaToModelTransformerCollection.php b/Transformer/ElasticaToModelTransformerCollection.php index 2418e2e..539f03d 100644 --- a/Transformer/ElasticaToModelTransformerCollection.php +++ b/Transformer/ElasticaToModelTransformerCollection.php @@ -13,14 +13,10 @@ use Symfony\Component\Form\Util\PropertyPath; 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() @@ -30,6 +26,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(); @@ -37,12 +43,19 @@ class ElasticaToModelTransformerCollection implements ElasticaToModelTransformer $sorted[$object->getType()][] = $object; } - $identifierProperty = new PropertyPath($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 ($identifierProperty) {return $identifierProperty->getValue($o);},$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(); @@ -71,4 +84,4 @@ class ElasticaToModelTransformerCollection implements ElasticaToModelTransformer return $transformer->getObjectClass(); }, $this->transformers); } -} \ No newline at end of file +} diff --git a/Transformer/ElasticaToModelTransformerInterface.php b/Transformer/ElasticaToModelTransformerInterface.php index 5f3f5aa..971bdfe 100644 --- a/Transformer/ElasticaToModelTransformerInterface.php +++ b/Transformer/ElasticaToModelTransformerInterface.php @@ -24,4 +24,11 @@ interface ElasticaToModelTransformerInterface * @return string */ function getObjectClass(); + + /** + * Returns the identifier field from the options + * + * @return string the identifier field + */ + function getIdentifierField(); }