Merge pull request #167 from Exercise/fixing_collection_identifier

Fixing identifier field not being set in TransformerCollection
This commit is contained in:
Richard Miller 2012-11-28 12:45:34 -08:00
commit 51f03ebbc3
6 changed files with 53 additions and 10 deletions

View file

@ -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
*

View file

@ -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
*

View file

@ -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 file

@ -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,

View file

@ -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);
}
}
}

View file

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