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