|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* YAWIK |
|
4
|
|
|
* |
|
5
|
|
|
* @filesource |
|
6
|
|
|
* @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de) |
|
7
|
|
|
* @license MIT |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Solr\Bridge; |
|
11
|
|
|
|
|
12
|
|
|
use Core\Repository\RepositoryService; |
|
13
|
|
|
use Solr\Filter\AbstractPaginationQuery; |
|
14
|
|
|
use Zend\ServiceManager\ServiceLocatorInterface; |
|
15
|
|
|
use ArrayAccess; |
|
16
|
|
|
use InvalidArgumentException; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Class ResultConverter |
|
20
|
|
|
* |
|
21
|
|
|
* Convert SOLR query result into Doctrine ODM Entity |
|
22
|
|
|
* |
|
23
|
|
|
* @author Anthonius Munthi <[email protected]> |
|
24
|
|
|
* @author Miroslav Fedeleš <[email protected]> |
|
25
|
|
|
* @package Solr\Bridge |
|
26
|
|
|
* @since 0.26 |
|
27
|
|
|
*/ |
|
28
|
|
|
class ResultConverter |
|
29
|
|
|
{ |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var RepositoryService |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $repositories; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param RepositoryService $repositories |
|
38
|
|
|
*/ |
|
39
|
|
|
public function __construct(RepositoryService $repositories) |
|
40
|
|
|
{ |
|
41
|
|
|
$this->repositories = $repositories; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Convert result into entities |
|
46
|
|
|
* |
|
47
|
|
|
* @param AbstractPaginationQuery $filter |
|
48
|
|
|
* @param ArrayAccess $response |
|
49
|
|
|
* @return array Array of entities |
|
50
|
|
|
* @throws InvalidArgumentException |
|
51
|
|
|
*/ |
|
52
|
|
|
public function convert(AbstractPaginationQuery $filter, ArrayAccess $response) |
|
53
|
|
|
{ |
|
54
|
|
|
$entities = []; |
|
55
|
|
|
$ids = []; |
|
56
|
|
|
$return = []; |
|
57
|
|
|
|
|
58
|
|
|
if (!isset($response['response']) |
|
59
|
|
|
|| !isset($response['response']['docs']) |
|
60
|
|
|
|| !is_array($response['response']['docs'])) |
|
61
|
|
|
{ |
|
62
|
|
|
throw new InvalidArgumentException('invalid response'); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
foreach ($response['response']['docs'] as $doc) { |
|
66
|
|
|
$ids[] = $doc->id; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
// fetch entities with given IDs |
|
70
|
|
|
$repository = $this->repositories->get($filter->getRepositoryName()); |
|
71
|
|
|
$qb = $repository->createQueryBuilder() /* @var $repository \Core\Repository\AbstractRepository */ |
|
72
|
|
|
->field('id') |
|
73
|
|
|
->in($ids); |
|
74
|
|
|
$result = $qb->getQuery()->execute(); |
|
75
|
|
|
|
|
76
|
|
|
foreach ($result as $document) { |
|
77
|
|
|
$entities[$document->getId()] = $document; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
// iterate over Solr response to preserve sorting |
|
81
|
|
|
foreach ($response['response']['docs'] as $doc) { |
|
82
|
|
|
$entity = isset($entities[$doc->id]) ? $entities[$doc->id] : $repository->create(); |
|
|
|
|
|
|
83
|
|
|
$return[] = $filter->proxyFactory($entity, $doc); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return $return; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Create a new instance of ResultConverter |
|
91
|
|
|
* @param ServiceLocatorInterface $sl |
|
92
|
|
|
* @return ResultConverter |
|
93
|
|
|
*/ |
|
94
|
|
|
static public function factory(ServiceLocatorInterface $sl) |
|
|
|
|
|
|
95
|
|
|
{ |
|
96
|
|
|
return new static($sl->get('repositories')); |
|
|
|
|
|
|
97
|
|
|
} |
|
98
|
|
|
} |
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.