Skip to content

Commit 764ce12

Browse files
committed
Fixed ability to use an object as unique identifier
Added missing blank line
1 parent 6958957 commit 764ce12

File tree

4 files changed

+49
-5
lines changed

4 files changed

+49
-5
lines changed

Doctrine/AbstractElasticaToModelTransformer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public function hybridTransform(array $elasticaObjects)
106106
{
107107
$indexedElasticaResults = array();
108108
foreach ($elasticaObjects as $elasticaObject) {
109-
$indexedElasticaResults[$elasticaObject->getId()] = $elasticaObject;
109+
$indexedElasticaResults[(string) $elasticaObject->getId()] = $elasticaObject;
110110
}
111111

112112
$objects = $this->transform($elasticaObjects);

Tests/Transformer/ElasticaToModelTransformerCollectionTest.php

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,32 @@ public function testTransformOrder()
100100
), $results);
101101
}
102102

103+
public function testTransformOrderWithIdAsObject()
104+
{
105+
$this->collectionSetup();
106+
107+
$id1 = 'yo';
108+
$id2 = 'lo';
109+
$idObject1 = new IDObject($id1);
110+
$idObject2 = new IDObject($id2);
111+
$document1 = new Document($idObject1, array('data' => 'lots of data'), 'type1');
112+
$document2 = new Document($idObject2, array('data' => 'not so much data'), 'type1');
113+
$result1 = new POPO($idObject1, 'lots of data');
114+
$result2 = new POPO2($idObject2, 'not so much data');
115+
116+
$this->transformers['type1']->expects($this->once())
117+
->method('transform')
118+
->with(array($document1, $document2))
119+
->will($this->returnValue(array($result1, $result2)));
120+
121+
$results = $this->collection->transform(array($document1, $document2));
122+
123+
$this->assertEquals(array(
124+
$result1,
125+
$result2,
126+
), $results);
127+
}
128+
103129
public function testGetIdentifierFieldReturnsAMapOfIdentifiers()
104130
{
105131
$collection = new ElasticaToModelTransformerCollection(array());
@@ -158,7 +184,7 @@ class POPO
158184
public $data;
159185

160186
/**
161-
* @param integer $id
187+
* @param mixed $id
162188
*/
163189
public function __construct($id, $data)
164190
{
@@ -175,3 +201,21 @@ public function getId()
175201
class POPO2 extends POPO
176202
{
177203
}
204+
205+
class IDObject
206+
{
207+
protected $id;
208+
209+
/**
210+
* @param int|string $id
211+
*/
212+
public function __construct($id)
213+
{
214+
$this->id = $id;
215+
}
216+
217+
public function __toString()
218+
{
219+
return (string) $this->id;
220+
}
221+
}

Transformer/AbstractElasticaToModelTransformer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ protected function getSortingClosure(array $idPos, $identifierPath)
4545
$propertyAccessor = $this->propertyAccessor;
4646

4747
return function ($a, $b) use ($idPos, $identifierPath, $propertyAccessor) {
48-
return $idPos[$propertyAccessor->getValue($a, $identifierPath)] > $idPos[$propertyAccessor->getValue($b, $identifierPath)];
48+
return $idPos[(string) $propertyAccessor->getValue($a, $identifierPath)] > $idPos[(string) $propertyAccessor->getValue($b, $identifierPath)];
4949
};
5050
}
5151
}

Transformer/ElasticaToModelTransformerCollection.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ function ($o) use ($identifierGetter) {
6868

6969
$result = array();
7070
foreach ($elasticaObjects as $object) {
71-
if (array_key_exists($object->getId(), $transformed[$object->getType()])) {
72-
$result[] = $transformed[$object->getType()][$object->getId()];
71+
if (array_key_exists((string) $object->getId(), $transformed[$object->getType()])) {
72+
$result[] = $transformed[$object->getType()][(string) $object->getId()];
7373
}
7474
}
7575

0 commit comments

Comments
 (0)