Skip to content

Commit 4af9f44

Browse files
committed
Move common sorting code to base Transformer class
1 parent 925410a commit 4af9f44

File tree

3 files changed

+57
-46
lines changed

3 files changed

+57
-46
lines changed

Doctrine/AbstractElasticaToModelTransformer.php

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace FOS\ElasticaBundle\Doctrine;
44

55
use FOS\ElasticaBundle\HybridResult;
6-
use FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerInterface;
6+
use FOS\ElasticaBundle\Transformer\AbstractElasticaToModelTransformer as BaseTransformer;
77
use FOS\ElasticaBundle\Transformer\HighlightableModelInterface;
88
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
99

@@ -12,7 +12,7 @@
1212
* This mapper assumes an exact match between
1313
* elastica documents ids and doctrine object ids.
1414
*/
15-
abstract class AbstractElasticaToModelTransformer implements ElasticaToModelTransformerInterface
15+
abstract class AbstractElasticaToModelTransformer extends BaseTransformer
1616
{
1717
/**
1818
* Manager registry.
@@ -38,13 +38,6 @@ abstract class AbstractElasticaToModelTransformer implements ElasticaToModelTran
3838
'query_builder_method' => 'createQueryBuilder',
3939
);
4040

41-
/**
42-
* PropertyAccessor instance.
43-
*
44-
* @var PropertyAccessorInterface
45-
*/
46-
protected $propertyAccessor;
47-
4841
/**
4942
* Instantiates a new Mapper.
5043
*
@@ -69,16 +62,6 @@ public function getObjectClass()
6962
return $this->objectClass;
7063
}
7164

72-
/**
73-
* Set the PropertyAccessor.
74-
*
75-
* @param PropertyAccessorInterface $propertyAccessor
76-
*/
77-
public function setPropertyAccessor(PropertyAccessorInterface $propertyAccessor)
78-
{
79-
$this->propertyAccessor = $propertyAccessor;
80-
}
81-
8265
/**
8366
* Transforms an array of elastica objects into an array of
8467
* model objects fetched from the doctrine repository.
@@ -111,10 +94,7 @@ public function transform(array $elasticaObjects)
11194
// sort objects in the order of ids
11295
$idPos = array_flip($ids);
11396
$identifier = $this->options['identifier'];
114-
$propertyAccessor = $this->propertyAccessor;
115-
usort($objects, function ($a, $b) use ($idPos, $identifier, $propertyAccessor) {
116-
return $idPos[$propertyAccessor->getValue($a, $identifier)] > $idPos[$propertyAccessor->getValue($b, $identifier)];
117-
});
97+
usort($objects, $this->getSortingClosure($idPos, $identifier));
11898

11999
return $objects;
120100
}

Propel/ElasticaToModelTransformer.php

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace FOS\ElasticaBundle\Propel;
44

55
use FOS\ElasticaBundle\HybridResult;
6+
use FOS\ElasticaBundle\Transformer\AbstractElasticaToModelTransformer;
67
use FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerInterface;
78
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
89

@@ -14,7 +15,7 @@
1415
*
1516
* @author William Durand <[email protected]>
1617
*/
17-
class ElasticaToModelTransformer implements ElasticaToModelTransformerInterface
18+
class ElasticaToModelTransformer extends AbstractElasticaToModelTransformer
1819
{
1920
/**
2021
* Propel model class to map to Elastica documents.
@@ -33,13 +34,6 @@ class ElasticaToModelTransformer implements ElasticaToModelTransformerInterface
3334
'identifier' => 'id',
3435
);
3536

36-
/**
37-
* PropertyAccessor instance.
38-
*
39-
* @var PropertyAccessorInterface
40-
*/
41-
protected $propertyAccessor;
42-
4337
/**
4438
* Constructor.
4539
*
@@ -52,16 +46,6 @@ public function __construct($objectClass, array $options = array())
5246
$this->options = array_merge($this->options, $options);
5347
}
5448

55-
/**
56-
* Set the PropertyAccessor instance.
57-
*
58-
* @param PropertyAccessorInterface $propertyAccessor
59-
*/
60-
public function setPropertyAccessor(PropertyAccessorInterface $propertyAccessor)
61-
{
62-
$this->propertyAccessor = $propertyAccessor;
63-
}
64-
6549
/**
6650
* Transforms an array of Elastica document into an array of Propel entities
6751
* fetched from the database.
@@ -82,11 +66,7 @@ public function transform(array $elasticaObjects)
8266
// Sort objects in the order of their IDs
8367
$idPos = array_flip($ids);
8468
$identifier = $this->options['identifier'];
85-
$propertyAccessor = $this->propertyAccessor;
86-
87-
$sortCallback = function ($a, $b) use ($idPos, $identifier, $propertyAccessor) {
88-
return $idPos[$propertyAccessor->getValue($a, $identifier)] > $idPos[$propertyAccessor->getValue($b, $identifier)];
89-
};
69+
$sortCallback = $this->getSortingClosure($idPos, $identifier);
9070

9171
if (is_object($objects)) {
9272
$objects->uasort($sortCallback);
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the FOSElasticaBundle project.
5+
*
6+
* (c) FriendsOfSymfony <https://github.com/FriendsOfSymfony/FOSElasticaBundle/graphs/contributors>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace FOS\ElasticaBundle\Transformer;
13+
14+
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
15+
16+
abstract class AbstractElasticaToModelTransformer implements ElasticaToModelTransformerInterface
17+
{
18+
/**
19+
* PropertyAccessor instance.
20+
*
21+
* @var PropertyAccessorInterface
22+
*/
23+
protected $propertyAccessor;
24+
25+
/**
26+
* Set the PropertyAccessor instance.
27+
*
28+
* @param PropertyAccessorInterface $propertyAccessor
29+
*/
30+
public function setPropertyAccessor(PropertyAccessorInterface $propertyAccessor)
31+
{
32+
$this->propertyAccessor = $propertyAccessor;
33+
}
34+
35+
/**
36+
* Returns a sorting closure to be used with usort() to put retrieved objects
37+
* back in the order that they were returned by ElasticSearch.
38+
*
39+
* @param array $idPos
40+
* @param string $identifierPath
41+
* @return callable
42+
*/
43+
protected function getSortingClosure(array $idPos, $identifierPath)
44+
{
45+
$propertyAccessor = $this->propertyAccessor;
46+
47+
return function ($a, $b) use ($idPos, $identifierPath, $propertyAccessor) {
48+
return $idPos[$propertyAccessor->getValue($a, $identifierPath)] > $idPos[$propertyAccessor->getValue($b, $identifierPath)];
49+
};
50+
}
51+
}

0 commit comments

Comments
 (0)