1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace FOS\ElasticaBundle\Finder; |
4
|
|
|
|
5
|
|
|
use Elastica\Document; |
6
|
|
|
use FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerInterface; |
7
|
|
|
use FOS\ElasticaBundle\Paginator\TransformedPaginatorAdapter; |
8
|
|
|
use FOS\ElasticaBundle\Paginator\FantaPaginatorAdapter; |
9
|
|
|
use FOS\ElasticaBundle\Paginator\RawPaginatorAdapter; |
10
|
|
|
use Pagerfanta\Pagerfanta; |
11
|
|
|
use Elastica\SearchableInterface; |
12
|
|
|
use Elastica\Query; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Finds elastica documents and map them to persisted objects. |
16
|
|
|
*/ |
17
|
|
|
class TransformedFinder implements PaginatedFinderInterface |
18
|
|
|
{ |
19
|
|
|
protected $searchable; |
20
|
|
|
protected $transformer; |
21
|
|
|
|
22
|
|
|
public function __construct(SearchableInterface $searchable, ElasticaToModelTransformerInterface $transformer) |
23
|
|
|
{ |
24
|
|
|
$this->searchable = $searchable; |
25
|
|
|
$this->transformer = $transformer; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Search for a query string. |
30
|
|
|
* |
31
|
|
|
* @param string $query |
32
|
|
|
* @param integer $limit |
33
|
|
|
* @param array $options |
34
|
|
|
* |
35
|
|
|
* @return array of model objects |
36
|
|
|
**/ |
37
|
|
|
public function find($query, $limit = null, $options = array()) |
38
|
|
|
{ |
39
|
|
|
$results = $this->search($query, $limit, $options); |
40
|
|
|
|
41
|
|
|
return $this->transformer->transform($results); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function findHybrid($query, $limit = null, $options = array()) |
45
|
|
|
{ |
46
|
|
|
$results = $this->search($query, $limit, $options); |
47
|
|
|
|
48
|
|
|
return $this->transformer->hybridTransform($results); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function findRawResult($query, $limit = null, $options = array()) |
52
|
|
|
{ |
53
|
|
|
$results = $this->search($query, $limit, $options); |
54
|
|
|
|
55
|
|
|
return $results; |
56
|
|
|
} |
57
|
|
|
/** |
58
|
|
|
* Find documents similar to one with passed id. |
59
|
|
|
* |
60
|
|
|
* @param integer $id |
61
|
|
|
* @param array $params |
62
|
|
|
* @param array $query |
63
|
|
|
* |
64
|
|
|
* @return array of model objects |
65
|
|
|
**/ |
66
|
|
|
public function moreLikeThis($id, $params = array(), $query = array()) |
67
|
|
|
{ |
68
|
|
|
$doc = new Document($id); |
69
|
|
|
$results = $this->searchable->moreLikeThis($doc, $params, $query)->getResults(); |
|
|
|
|
70
|
|
|
|
71
|
|
|
return $this->transformer->transform($results); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param $query |
76
|
|
|
* @param null|int $limit |
77
|
|
|
* @param array $options |
78
|
|
|
* |
79
|
|
|
* @return array |
80
|
|
|
*/ |
81
|
|
|
protected function search($query, $limit = null, $options = array()) |
82
|
|
|
{ |
83
|
|
|
$queryObject = Query::create($query); |
84
|
|
|
if (null !== $limit) { |
85
|
|
|
$queryObject->setSize($limit); |
86
|
|
|
} |
87
|
|
|
$results = $this->searchable->search($queryObject, $options)->getResults(); |
88
|
|
|
|
89
|
|
|
return $results; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Gets a paginator wrapping the result of a search. |
94
|
|
|
* |
95
|
|
|
* @param string $query |
96
|
|
|
* @param array $options |
97
|
|
|
* |
98
|
|
|
* @return Pagerfanta |
99
|
|
|
*/ |
100
|
|
View Code Duplication |
public function findPaginated($query, $options = array()) |
|
|
|
|
101
|
|
|
{ |
102
|
|
|
$queryObject = Query::create($query); |
103
|
|
|
$paginatorAdapter = $this->createPaginatorAdapter($queryObject, $options); |
104
|
|
|
|
105
|
|
|
return new Pagerfanta(new FantaPaginatorAdapter($paginatorAdapter)); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* {@inheritdoc} |
110
|
|
|
*/ |
111
|
|
View Code Duplication |
public function findRawPaginated($query, $options = array()) |
|
|
|
|
112
|
|
|
{ |
113
|
|
|
$queryObject = Query::create($query); |
114
|
|
|
$paginatorAdapter = $this->createRawPaginatorAdapter($queryObject, $options); |
115
|
|
|
|
116
|
|
|
return new Pagerfanta(new FantaPaginatorAdapter($paginatorAdapter)); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* {@inheritdoc} |
121
|
|
|
*/ |
122
|
|
|
public function createPaginatorAdapter($query, $options = array()) |
123
|
|
|
{ |
124
|
|
|
$query = Query::create($query); |
125
|
|
|
|
126
|
|
|
return new TransformedPaginatorAdapter($this->searchable, $query, $options, $this->transformer); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* {@inheritdoc} |
131
|
|
|
*/ |
132
|
|
|
public function createRawPaginatorAdapter($query, $options = array()) |
133
|
|
|
{ |
134
|
|
|
$query = Query::create($query); |
135
|
|
|
return new RawPaginatorAdapter($this->searchable, $query, $options); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: