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\Finder; |
13
|
|
|
|
14
|
|
|
use Elastica\Query; |
15
|
|
|
use Elastica\SearchableInterface; |
16
|
|
|
use FOS\ElasticaBundle\Paginator\FantaPaginatorAdapter; |
17
|
|
|
use FOS\ElasticaBundle\Paginator\HybridPaginatorAdapter; |
18
|
|
|
use FOS\ElasticaBundle\Paginator\RawPaginatorAdapter; |
19
|
|
|
use FOS\ElasticaBundle\Paginator\TransformedPaginatorAdapter; |
20
|
|
|
use FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerInterface; |
21
|
|
|
use Pagerfanta\Pagerfanta; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Finds elastica documents and map them to persisted objects. |
25
|
|
|
*/ |
26
|
|
|
class TransformedFinder implements PaginatedFinderInterface |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var SearchableInterface |
30
|
|
|
*/ |
31
|
|
|
protected $searchable; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var ElasticaToModelTransformerInterface |
35
|
|
|
*/ |
36
|
|
|
protected $transformer; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param SearchableInterface $searchable |
40
|
|
|
* @param ElasticaToModelTransformerInterface $transformer |
41
|
|
|
*/ |
42
|
7 |
|
public function __construct(SearchableInterface $searchable, ElasticaToModelTransformerInterface $transformer) |
43
|
|
|
{ |
44
|
7 |
|
$this->searchable = $searchable; |
45
|
7 |
|
$this->transformer = $transformer; |
46
|
7 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritdoc} |
50
|
|
|
*/ |
51
|
1 |
|
public function find($query, $limit = null, $options = []) |
52
|
|
|
{ |
53
|
1 |
|
$results = $this->search($query, $limit, $options); |
54
|
|
|
|
55
|
1 |
|
return $this->transformer->transform($results); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param $query |
60
|
|
|
* @param null|int $limit |
61
|
|
|
* @param array $options |
62
|
|
|
* |
63
|
|
|
* @return array |
64
|
|
|
*/ |
65
|
1 |
|
public function findHybrid($query, $limit = null, $options = []) |
66
|
|
|
{ |
67
|
1 |
|
$results = $this->search($query, $limit, $options); |
68
|
|
|
|
69
|
1 |
|
return $this->transformer->hybridTransform($results); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritdoc} |
74
|
|
|
*/ |
75
|
1 |
|
public function findPaginated($query, $options = []) |
76
|
|
|
{ |
77
|
1 |
|
$queryObject = Query::create($query); |
78
|
1 |
|
$paginatorAdapter = $this->createPaginatorAdapter($queryObject, $options); |
79
|
|
|
|
80
|
1 |
|
return new Pagerfanta(new FantaPaginatorAdapter($paginatorAdapter)); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* {@inheritdoc} |
85
|
|
|
*/ |
86
|
|
|
public function findHybridPaginated($query) |
87
|
|
|
{ |
88
|
|
|
$paginatorAdapter = $this->createHybridPaginatorAdapter($query); |
89
|
|
|
|
90
|
|
|
return new Pagerfanta(new FantaPaginatorAdapter($paginatorAdapter)); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* {@inheritdoc} |
95
|
|
|
*/ |
96
|
2 |
|
public function createPaginatorAdapter($query, $options = []) |
97
|
|
|
{ |
98
|
2 |
|
$query = Query::create($query); |
99
|
|
|
|
100
|
2 |
|
return new TransformedPaginatorAdapter($this->searchable, $query, $options, $this->transformer); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* {@inheritdoc} |
105
|
|
|
*/ |
106
|
1 |
|
public function createHybridPaginatorAdapter($query) |
107
|
|
|
{ |
108
|
1 |
|
$query = Query::create($query); |
109
|
|
|
|
110
|
1 |
|
return new HybridPaginatorAdapter($this->searchable, $query, $this->transformer); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* {@inheritdoc} |
115
|
|
|
*/ |
116
|
|
|
public function createRawPaginatorAdapter($query, $options = []) |
117
|
|
|
{ |
118
|
|
|
$query = Query::create($query); |
119
|
|
|
|
120
|
|
|
return new RawPaginatorAdapter($this->searchable, $query, $options); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param $query |
125
|
|
|
* @param null|int $limit |
126
|
|
|
* @param array $options |
127
|
|
|
* |
128
|
|
|
* @return array |
129
|
|
|
*/ |
130
|
1 |
|
protected function search($query, $limit = null, $options = []) |
131
|
|
|
{ |
132
|
1 |
|
$queryObject = Query::create($query); |
133
|
1 |
|
if (null !== $limit) { |
134
|
1 |
|
$queryObject->setSize($limit); |
135
|
|
|
} |
136
|
1 |
|
$results = $this->searchable->search($queryObject, $options)->getResults(); |
137
|
|
|
|
138
|
1 |
|
return $results; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|