Completed
Pull Request — master (#1411)
by Floran
09:07
created

setPropertyAccessor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
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
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 10
    public function setPropertyAccessor(PropertyAccessorInterface $propertyAccessor)
31
    {
32 10
        $this->propertyAccessor = $propertyAccessor;
33 10
    }
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
     *
42
     * @return callable
43
     */
44
    protected function getSortingClosure(array $idPos, $identifierPath)
45
    {
46
        $propertyAccessor = $this->propertyAccessor;
47
48
        return function ($a, $b) use ($idPos, $identifierPath, $propertyAccessor) {
49
            return $idPos[(string) $propertyAccessor->getValue($a, $identifierPath)] > $idPos[(string) $propertyAccessor->getValue($b, $identifierPath)];
50
        };
51
    }
52
}
53