Skip to content
Browse files

Fixing identifier field not being set in TransformerCollection

  • Loading branch information...
1 parent 285e51c commit 5f1e1326d54e9b816b1f5d2b11bb2fa4abc752cc @richardmiller richardmiller committed
View
8 Doctrine/AbstractElasticaToModelTransformer.php
@@ -111,6 +111,14 @@ public function hybridTransform(array $elasticaObjects)
}
/**
+ * {@inheritdoc}
+ */
+ public function getIdentifierField()
+ {
+ return $this->options['identifier'];
+ }
+
+ /**
* Fetches objects by theses identifier values
*
* @param array $identifierValues ids values
View
8 Propel/ElasticaToModelTransformer.php
@@ -100,6 +100,14 @@ public function getObjectClass()
}
/**
+ * {@inheritdoc}
+ */
+ public function getIdentifierField()
+ {
+ return $this->options['identifier'];
+ }
+
+ /**
* Fetch objects for theses identifier values
*
* @param string $class the model class
View
1 Resources/config/config.xml
@@ -64,7 +64,6 @@
<service id="foq_elastica.elastica_to_model_transformer.collection.prototype" class="%foq_elastica.elastica_to_model_transformer.collection.class%" public="true" abstract="true">
<argument type="collection" /> <!-- transformers -->
- <argument type="collection" /> <!-- options -->
</service>
</services>
View
8 Tests/Transformer/ElasticaToModelTransformerCollectionTest.php
@@ -19,11 +19,19 @@ protected function collectionSetup()
->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,
View
31 Transformer/ElasticaToModelTransformerCollection.php
@@ -12,14 +12,10 @@
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 @@ public function getObjectClass()
}, $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 @@ public function transform(array $elasticaObjects)
$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 @@ protected function getTypeToClassMap()
return $transformer->getObjectClass();
}, $this->transformers);
}
-}
+}
View
7 Transformer/ElasticaToModelTransformerInterface.php
@@ -24,4 +24,11 @@ function hybridTransform(array $elasticaObjects);
* @return string
*/
function getObjectClass();
+
+ /**
+ * Returns the identifier field from the options
+ *
+ * @return string the identifier field
+ */
+ function getIdentifierField();
}

0 comments on commit 5f1e132

Please sign in to comment.
Something went wrong with that request. Please try again.