1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the FOSElasticaBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> |
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
|
|
|
/** |
13
|
|
|
* This file is part of the FOSElasticaBundle project. |
14
|
|
|
* |
15
|
|
|
* (c) FriendsOfSymfony <https://github.com/FriendsOfSymfony/FOSElasticaBundle/graphs/contributors> |
16
|
|
|
* |
17
|
|
|
* For the full copyright and license information, please view the LICENSE |
18
|
|
|
* file that was distributed with this source code. |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace FOS\ElasticaBundle\Transformer; |
22
|
|
|
|
23
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccessorInterface; |
24
|
|
|
|
25
|
|
|
abstract class AbstractElasticaToModelTransformer implements ElasticaToModelTransformerInterface |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* PropertyAccessor instance. |
29
|
|
|
* |
30
|
|
|
* @var PropertyAccessorInterface |
31
|
|
|
*/ |
32
|
|
|
protected $propertyAccessor; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Set the PropertyAccessor instance. |
36
|
|
|
* |
37
|
|
|
* @param PropertyAccessorInterface $propertyAccessor |
38
|
|
|
*/ |
39
|
10 |
|
public function setPropertyAccessor(PropertyAccessorInterface $propertyAccessor) |
40
|
|
|
{ |
41
|
10 |
|
$this->propertyAccessor = $propertyAccessor; |
42
|
10 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Returns a sorting closure to be used with usort() to put retrieved objects |
46
|
|
|
* back in the order that they were returned by ElasticSearch. |
47
|
|
|
* |
48
|
|
|
* @param array $idPos |
49
|
|
|
* @param string $identifierPath |
50
|
|
|
* |
51
|
|
|
* @return callable |
52
|
|
|
*/ |
53
|
|
|
protected function getSortingClosure(array $idPos, $identifierPath) |
54
|
|
|
{ |
55
|
|
|
$propertyAccessor = $this->propertyAccessor; |
56
|
|
|
|
57
|
|
|
return function ($a, $b) use ($idPos, $identifierPath, $propertyAccessor) { |
58
|
|
|
return $idPos[(string) $propertyAccessor->getValue($a, $identifierPath)] > $idPos[(string) $propertyAccessor->getValue($b, $identifierPath)]; |
59
|
|
|
}; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|