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
|
|
|
/** |
22
|
|
|
* @param SearchableInterface $searchable |
23
|
|
|
* @param ElasticaToModelTransformerInterface $transformer |
24
|
|
|
*/ |
25
|
5 |
|
public function __construct(SearchableInterface $searchable, ElasticaToModelTransformerInterface $transformer) |
26
|
|
|
{ |
27
|
5 |
|
$this->searchable = $searchable; |
28
|
5 |
|
$this->transformer = $transformer; |
29
|
5 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* {@inheritdoc} |
33
|
|
|
*/ |
34
|
1 |
|
public function find($query, $limit = null, $options = array()) |
35
|
|
|
{ |
36
|
1 |
|
$results = $this->search($query, $limit, $options); |
37
|
|
|
|
38
|
1 |
|
return $this->transformer->transform($results); |
39
|
|
|
} |
40
|
|
|
|
41
|
1 |
|
public function findHybrid($query, $limit = null, $options = array()) |
42
|
|
|
{ |
43
|
1 |
|
$results = $this->search($query, $limit, $options); |
44
|
|
|
|
45
|
1 |
|
return $this->transformer->hybridTransform($results); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Find documents similar to one with passed id. |
50
|
|
|
* |
51
|
|
|
* @param integer $id |
52
|
|
|
* @param array $params |
53
|
|
|
* @param array $query |
54
|
|
|
* |
55
|
|
|
* @return array of model objects |
56
|
|
|
**/ |
57
|
1 |
|
public function moreLikeThis($id, $params = array(), $query = array()) |
58
|
|
|
{ |
59
|
1 |
|
$doc = new Document($id); |
60
|
1 |
|
$results = $this->searchable->moreLikeThis($doc, $params, $query)->getResults(); |
|
|
|
|
61
|
|
|
|
62
|
1 |
|
return $this->transformer->transform($results); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param $query |
67
|
|
|
* @param null|int $limit |
68
|
|
|
* @param array $options |
69
|
|
|
* |
70
|
|
|
* @return array |
71
|
|
|
*/ |
72
|
1 |
|
protected function search($query, $limit = null, $options = array()) |
73
|
|
|
{ |
74
|
1 |
|
$queryObject = Query::create($query); |
75
|
1 |
|
if (null !== $limit) { |
76
|
1 |
|
$queryObject->setSize($limit); |
77
|
|
|
} |
78
|
1 |
|
$results = $this->searchable->search($queryObject, $options)->getResults(); |
79
|
|
|
|
80
|
1 |
|
return $results; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* {@inheritdoc} |
85
|
|
|
*/ |
86
|
1 |
|
public function findPaginated($query, $options = array()) |
87
|
|
|
{ |
88
|
1 |
|
$queryObject = Query::create($query); |
89
|
1 |
|
$paginatorAdapter = $this->createPaginatorAdapter($queryObject, $options); |
90
|
|
|
|
91
|
1 |
|
return new Pagerfanta(new FantaPaginatorAdapter($paginatorAdapter)); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* {@inheritdoc} |
96
|
|
|
*/ |
97
|
1 |
|
public function createPaginatorAdapter($query, $options = array()) |
98
|
|
|
{ |
99
|
1 |
|
$query = Query::create($query); |
100
|
|
|
|
101
|
1 |
|
return new TransformedPaginatorAdapter($this->searchable, $query, $options, $this->transformer); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
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: