|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
4
|
|
|
* file that was distributed with this source code. |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace Pagerfanta\Adapter; |
|
8
|
|
|
|
|
9
|
|
|
use SphinxClient; |
|
10
|
|
|
|
|
11
|
|
|
class SphinxAdapter implements AdapterInterface |
|
12
|
|
|
{ |
|
13
|
|
|
private $client; |
|
14
|
|
|
private $query; |
|
15
|
|
|
private $index; |
|
16
|
|
|
private $comment; |
|
17
|
|
|
private $results; |
|
18
|
|
|
private $maxMatches = 0; |
|
19
|
|
|
private $cutoff = 0; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Constructor. |
|
23
|
|
|
* |
|
24
|
|
|
* @param SphinxClient $client A Sphinx client. |
|
25
|
|
|
* @param string $query A Sphinx query. |
|
26
|
|
|
* @param string $index A Sphinx index. |
|
27
|
|
|
* @param string $comment A Sphinx comment. |
|
28
|
|
|
*/ |
|
29
|
|
|
public function __construct(SphinxClient $client, $query, $index = "*", $comment = "") |
|
30
|
|
|
{ |
|
31
|
|
|
$this->client = $client; |
|
32
|
|
|
$this->query = $query; |
|
33
|
|
|
$this->index = $index; |
|
34
|
|
|
$this->comment = $comment; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* {@inheritdoc} |
|
39
|
|
|
*/ |
|
40
|
|
|
public function getNbResults() |
|
41
|
|
|
{ |
|
42
|
|
|
if (!$this->results) { |
|
43
|
|
|
return $this->client->query($this->query, $this->index, $this->comment)['total']; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
return $this->results['total']; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/* |
|
50
|
|
|
* setMaxMatches |
|
51
|
|
|
* |
|
52
|
|
|
* @param int $maxMatches Controls how much matches searchd will keep in RAM while searching. |
|
53
|
|
|
* |
|
54
|
|
|
* @return SphinxAdapter |
|
55
|
|
|
*/ |
|
56
|
|
|
public function setMaxMatches($maxMatches) |
|
57
|
|
|
{ |
|
58
|
|
|
$this->maxMatches = $maxMatches; |
|
59
|
|
|
|
|
60
|
|
|
return $this; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/* |
|
64
|
|
|
* getMaxMatches |
|
65
|
|
|
* |
|
66
|
|
|
* @param void |
|
67
|
|
|
* |
|
68
|
|
|
* @return int |
|
69
|
|
|
*/ |
|
70
|
|
|
public function getMaxMatches() |
|
71
|
|
|
{ |
|
72
|
|
|
return $this->maxMatches; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/* |
|
76
|
|
|
* setCutoff |
|
77
|
|
|
* |
|
78
|
|
|
* @param $cutoff Used for advanced performance control. It tells searchd to forcibly stop search query once cutoff matches have been found and processed. |
|
79
|
|
|
* |
|
80
|
|
|
* @return SphinxAdapter |
|
81
|
|
|
*/ |
|
82
|
|
|
public function setCutoff($cutoff) |
|
83
|
|
|
{ |
|
84
|
|
|
$this->cutoff = $cutoff; |
|
85
|
|
|
|
|
86
|
|
|
return $this; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/* |
|
90
|
|
|
* getCutoff |
|
91
|
|
|
* |
|
92
|
|
|
* @param void |
|
93
|
|
|
* |
|
94
|
|
|
* @return int |
|
95
|
|
|
*/ |
|
96
|
|
|
public function getCutoff() |
|
97
|
|
|
{ |
|
98
|
|
|
return $this->cutoff; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* {@inheritdoc} |
|
103
|
|
|
*/ |
|
104
|
|
|
public function getSlice($offset, $limit) |
|
105
|
|
|
{ |
|
106
|
|
|
// Set limit |
|
107
|
|
|
$this->client->setLimits($offset, $limit, $this->maxMatches, $this->cutoff); |
|
108
|
|
|
|
|
109
|
|
|
return $this->results = $this->client |
|
110
|
|
|
->query($this->query, $this->index, $this->comment); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|