Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge branch '2.0'

Conflicts:
	Transformer/ElasticaToModelTransformerCollection.php
  • Loading branch information...
commit 8fde6b235cb330e67d0f29e625d3cdc67c0f82c3 2 parents 62b28a8 + 51f03eb
@richardmiller richardmiller authored
View
8 Doctrine/AbstractElasticaToModelTransformer.php
@@ -112,6 +112,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
@@ -101,6 +101,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
@@ -61,7 +61,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>
<service id="foq_elastica.provider_registry" class="%foq_elastica.provider_registry.class%">
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
@@ -13,14 +13,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()
@@ -30,6 +26,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();
@@ -37,12 +43,19 @@ public function transform(array $elasticaObjects)
$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 @@ 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();
}
Please sign in to comment.
Something went wrong with that request. Please try again.