Skip to content

Commit

Permalink
Merge branch '2.0'
Browse files Browse the repository at this point in the history
Conflicts:
	Transformer/ElasticaToModelTransformerCollection.php
  • Loading branch information
richardmiller-zz committed Nov 28, 2012
2 parents 62b28a8 + 51f03eb commit 8fde6b2
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 10 deletions.
8 changes: 8 additions & 0 deletions Doctrine/AbstractElasticaToModelTransformer.php
Expand Up @@ -111,6 +111,14 @@ public function hybridTransform(array $elasticaObjects)
return $result;
}

/**
* {@inheritdoc}
*/
public function getIdentifierField()
{
return $this->options['identifier'];
}

/**
* Fetches objects by theses identifier values
*
Expand Down
8 changes: 8 additions & 0 deletions Propel/ElasticaToModelTransformer.php
Expand Up @@ -100,6 +100,14 @@ public function getObjectClass()
return $this->objectClass;
}

/**
* {@inheritdoc}
*/
public function getIdentifierField()
{
return $this->options['identifier'];
}

/**
* Fetch objects for theses identifier values
*
Expand Down
1 change: 0 additions & 1 deletion Resources/config/config.xml
Expand Up @@ -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%">
Expand Down
Expand Up @@ -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,
Expand Down
31 changes: 22 additions & 9 deletions Transformer/ElasticaToModelTransformerCollection.php
Expand Up @@ -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()
Expand All @@ -30,19 +26,36 @@ 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();
foreach ($elasticaObjects as $object) {
$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();
Expand Down Expand Up @@ -71,4 +84,4 @@ protected function getTypeToClassMap()
return $transformer->getObjectClass();
}, $this->transformers);
}
}
}
7 changes: 7 additions & 0 deletions Transformer/ElasticaToModelTransformerInterface.php
Expand Up @@ -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 8fde6b2

Please sign in to comment.