From 5f1e1326d54e9b816b1f5d2b11bb2fa4abc752cc Mon Sep 17 00:00:00 2001 From: Richard Miller Date: Mon, 24 Sep 2012 23:15:05 +0100 Subject: [PATCH] Fixing identifier field not being set in TransformerCollection --- .../AbstractElasticaToModelTransformer.php | 8 +++++ Propel/ElasticaToModelTransformer.php | 8 +++++ Resources/config/config.xml | 1 - ...asticaToModelTransformerCollectionTest.php | 8 +++++ .../ElasticaToModelTransformerCollection.php | 31 +++++++++++++------ .../ElasticaToModelTransformerInterface.php | 7 +++++ 6 files changed, 53 insertions(+), 10 deletions(-) diff --git a/Doctrine/AbstractElasticaToModelTransformer.php b/Doctrine/AbstractElasticaToModelTransformer.php index 019619d..df14d00 100755 --- a/Doctrine/AbstractElasticaToModelTransformer.php +++ b/Doctrine/AbstractElasticaToModelTransformer.php @@ -110,6 +110,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 ea3547b..a48a5e3 100644 --- a/Propel/ElasticaToModelTransformer.php +++ b/Propel/ElasticaToModelTransformer.php @@ -99,6 +99,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 942ddef..f8f180f 100644 --- a/Resources/config/config.xml +++ b/Resources/config/config.xml @@ -64,7 +64,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 33bd698..a11172b 100644 --- a/Transformer/ElasticaToModelTransformerCollection.php +++ b/Transformer/ElasticaToModelTransformerCollection.php @@ -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); } -} \ 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(); }