1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Pagerfanta package. |
5
|
|
|
* |
6
|
|
|
* (c) Pablo Díez <[email protected]> |
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 Pagerfanta\Adapter; |
13
|
|
|
|
14
|
|
|
use Pagerfanta\Exception\InvalidArgumentException; |
15
|
|
|
use Solarium\QueryType\Select\Query\Query; |
16
|
|
|
use Solarium\Core\Client\Client; |
17
|
|
|
use Solarium_Query_Select; |
18
|
|
|
use Solarium_Client; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* SolariumAdapter. |
22
|
|
|
* |
23
|
|
|
* @author Igor Wiedler <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
class SolariumAdapter implements AdapterInterface |
26
|
|
|
{ |
27
|
|
|
private $client; |
28
|
|
|
private $query; |
29
|
|
|
private $resultSet; |
30
|
|
|
private $endPoint; |
31
|
|
|
private $resultSetStart; |
32
|
|
|
private $resultSetRows; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Constructor. |
36
|
|
|
* |
37
|
|
|
* @param Solarium_Client|Client $client A Solarium client. |
38
|
|
|
* @param Solarium_Query_Select|Query $query A Solarium select query. |
39
|
|
|
*/ |
40
|
11 |
|
public function __construct($client, $query) |
41
|
|
|
{ |
42
|
11 |
|
$this->checkClient($client); |
43
|
10 |
|
$this->checkQuery($query); |
44
|
|
|
|
45
|
9 |
|
$this->client = $client; |
46
|
9 |
|
$this->query = $query; |
47
|
9 |
|
} |
48
|
|
|
|
49
|
11 |
|
private function checkClient($client) |
50
|
|
|
{ |
51
|
11 |
|
if ($this->isClientInvalid($client)) { |
52
|
1 |
|
throw new InvalidArgumentException($this->getClientInvalidMessage($client)); |
53
|
|
|
} |
54
|
10 |
|
} |
55
|
|
|
|
56
|
11 |
|
private function isClientInvalid($client) |
57
|
|
|
{ |
58
|
11 |
|
return !($client instanceof Client) && |
59
|
11 |
|
!($client instanceof Solarium_Client); |
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
1 |
|
private function getClientInvalidMessage($client) |
63
|
|
|
{ |
64
|
1 |
|
return sprintf('The client object should be a Solarium_Client or Solarium\Core\Client\Client instance, %s given', |
65
|
1 |
|
get_class($client) |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
|
69
|
10 |
|
private function checkQuery($query) |
70
|
|
|
{ |
71
|
10 |
|
if ($this->isQueryInvalid($query)) { |
72
|
1 |
|
throw new InvalidArgumentException($this->getQueryInvalidMessage($query)); |
73
|
|
|
} |
74
|
9 |
|
} |
75
|
|
|
|
76
|
10 |
|
private function isQueryInvalid($query) |
77
|
|
|
{ |
78
|
10 |
|
return !($query instanceof Query) && |
79
|
10 |
|
!($query instanceof Solarium_Query_Select); |
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
1 |
|
private function getQueryInvalidMessage($query) |
83
|
|
|
{ |
84
|
1 |
|
return sprintf('The query object should be a Solarium_Query_Select or Solarium\QueryType\Select\Query\Query instance, %s given', |
85
|
1 |
|
get_class($query) |
86
|
|
|
); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* {@inheritdoc} |
91
|
|
|
*/ |
92
|
4 |
|
public function getNbResults() |
93
|
|
|
{ |
94
|
4 |
|
return $this->getResultSet()->getNumFound(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* {@inheritdoc} |
99
|
|
|
*/ |
100
|
6 |
|
public function getSlice($offset, $length) |
101
|
|
|
{ |
102
|
6 |
|
return $this->getResultSet($offset, $length); |
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param int $start |
107
|
|
|
* @param int $rows |
108
|
|
|
* |
109
|
|
|
* @return \Solarium_Result_Select|\Solarium\QueryType\Select\Result\Result |
110
|
|
|
**/ |
111
|
9 |
|
public function getResultSet($start = null, $rows = null) |
112
|
|
|
{ |
113
|
9 |
|
if ($this->resultSetStartAndRowsAreNotNullAndChange($start, $rows)) { |
114
|
6 |
|
$this->resultSetStart = $start; |
115
|
6 |
|
$this->resultSetRows = $rows; |
116
|
|
|
|
117
|
6 |
|
$this->modifyQuery(); |
118
|
6 |
|
$this->clearResultSet(); |
119
|
|
|
} |
120
|
|
|
|
121
|
9 |
|
if ($this->resultSetEmpty()) { |
122
|
9 |
|
$this->resultSet = $this->createResultSet(); |
123
|
|
|
} |
124
|
|
|
|
125
|
9 |
|
return $this->resultSet; |
126
|
|
|
} |
127
|
|
|
|
128
|
9 |
|
private function resultSetStartAndRowsAreNotNullAndChange($start, $rows) |
129
|
|
|
{ |
130
|
9 |
|
return $this->resultSetStartAndRowsAreNotNull($start, $rows) && |
131
|
9 |
|
$this->resultSetStartAndRowsChange($start, $rows); |
132
|
|
|
} |
133
|
|
|
|
134
|
9 |
|
private function resultSetStartAndRowsAreNotNull($start, $rows) |
135
|
|
|
{ |
136
|
9 |
|
return $start !== null && $rows !== null; |
137
|
|
|
} |
138
|
|
|
|
139
|
6 |
|
private function resultSetStartAndRowsChange($start, $rows) |
140
|
|
|
{ |
141
|
6 |
|
return $start !== $this->resultSetStart || $rows !== $this->resultSetRows; |
142
|
|
|
} |
143
|
|
|
|
144
|
6 |
|
private function modifyQuery() |
145
|
|
|
{ |
146
|
6 |
|
$this->query |
147
|
6 |
|
->setStart($this->resultSetStart) |
148
|
6 |
|
->setRows($this->resultSetRows); |
149
|
6 |
|
} |
150
|
|
|
|
151
|
9 |
|
private function createResultSet() |
152
|
|
|
{ |
153
|
9 |
|
return $this->client->select($this->query, $this->endPoint); |
154
|
|
|
} |
155
|
|
|
|
156
|
6 |
|
private function clearResultSet() |
157
|
|
|
{ |
158
|
6 |
|
$this->resultSet = null; |
159
|
6 |
|
} |
160
|
|
|
|
161
|
9 |
|
private function resultSetEmpty() |
162
|
|
|
{ |
163
|
9 |
|
return $this->resultSet === null; |
164
|
|
|
} |
165
|
|
|
|
166
|
1 |
|
public function setEndPoint($endPoint) |
167
|
|
|
{ |
168
|
1 |
|
$this->endPoint = $endPoint; |
169
|
|
|
|
170
|
1 |
|
return $this; |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|