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