Merge branch '2.0'

Conflicts:
	Transformer/ElasticaToModelTransformerCollection.php
This commit is contained in:
Richard Miller 2012-11-28 21:15:13 +00:00
commit 8fde6b235c
6 changed files with 53 additions and 10 deletions

View file

@ -111,6 +111,14 @@ abstract class AbstractElasticaToModelTransformer implements ElasticaToModelTran
return $result; return $result;
} }
/**
* {@inheritdoc}
*/
public function getIdentifierField()
{
return $this->options['identifier'];
}
/** /**
* Fetches objects by theses identifier values * Fetches objects by theses identifier values
* *

View file

@ -100,6 +100,14 @@ class ElasticaToModelTransformer implements ElasticaToModelTransformerInterface
return $this->objectClass; return $this->objectClass;
} }
/**
* {@inheritdoc}
*/
public function getIdentifierField()
{
return $this->options['identifier'];
}
/** /**
* Fetch objects for theses identifier values * Fetch objects for theses identifier values
* *

View file

@ -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"> <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" /> <!-- transformers -->
<argument type="collection" /> <!-- options -->
</service> </service>
<service id="foq_elastica.provider_registry" class="%foq_elastica.provider_registry.class%"> <service id="foq_elastica.provider_registry" class="%foq_elastica.provider_registry.class%">

View file

@ -19,11 +19,19 @@ class ElasticaToModelTransformerCollectionTest extends \PHPUnit_Framework_TestCa
->method('getObjectClass') ->method('getObjectClass')
->will($this->returnValue('FOQ\ElasticaBundle\Tests\Transformer\POPO')); ->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 = $this->getMock('FOQ\ElasticaBundle\Transformer\ElasticaToModelTransformerInterface');
$transformer2->expects($this->any()) $transformer2->expects($this->any())
->method('getObjectClass') ->method('getObjectClass')
->will($this->returnValue('FOQ\ElasticaBundle\Tests\Transformer\POPO2')); ->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( $this->collection = new ElasticaToModelTransformerCollection($this->transformers = array(
'type1' => $transformer1, 'type1' => $transformer1,
'type2' => $transformer2, 'type2' => $transformer2,

View file

@ -13,14 +13,10 @@ use Symfony\Component\Form\Util\PropertyPath;
class ElasticaToModelTransformerCollection implements ElasticaToModelTransformerInterface class ElasticaToModelTransformerCollection implements ElasticaToModelTransformerInterface
{ {
protected $transformers = array(); protected $transformers = array();
protected $options = array(
'identifier' => 'id'
);
public function __construct(array $transformers, array $options) public function __construct(array $transformers)
{ {
$this->transformers = $transformers; $this->transformers = $transformers;
$this->options = array_merge($this->options, $options);
} }
public function getObjectClass() public function getObjectClass()
@ -30,6 +26,16 @@ class ElasticaToModelTransformerCollection implements ElasticaToModelTransformer
}, $this->transformers); }, $this->transformers);
} }
/**
* {@inheritdoc}
*/
public function getIdentifierField()
{
return array_map(function ($transformer) {
return $transformer->getIdentifierField();
}, $this->transformers);
}
public function transform(array $elasticaObjects) public function transform(array $elasticaObjects)
{ {
$sorted = array(); $sorted = array();
@ -37,12 +43,19 @@ class ElasticaToModelTransformerCollection implements ElasticaToModelTransformer
$sorted[$object->getType()][] = $object; $sorted[$object->getType()][] = $object;
} }
$identifierProperty = new PropertyPath($this->options['identifier']);
$transformed = array(); $transformed = array();
foreach ($sorted AS $type => $objects) { foreach ($sorted AS $type => $objects) {
$transformedObjects = $this->transformers[$type]->transform($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(); $result = array();
@ -71,4 +84,4 @@ class ElasticaToModelTransformerCollection implements ElasticaToModelTransformer
return $transformer->getObjectClass(); return $transformer->getObjectClass();
}, $this->transformers); }, $this->transformers);
} }
} }

View file

@ -24,4 +24,11 @@ interface ElasticaToModelTransformerInterface
* @return string * @return string
*/ */
function getObjectClass(); function getObjectClass();
/**
* Returns the identifier field from the options
*
* @return string the identifier field
*/
function getIdentifierField();
} }