1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of gpupo/search |
5
|
|
|
* |
6
|
|
|
* (c) Gilmar Pupo <[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 Gpupo\Search\Query; |
13
|
|
|
|
14
|
|
|
use Gpupo\Common\Entity\CollectionAbstract; |
15
|
|
|
use Gpupo\Search\Paginator\PaginableInterface; |
16
|
|
|
use Gpupo\Search\Paginator\PaginatorInterface; |
17
|
|
|
|
18
|
|
|
abstract class QueryAbstract extends CollectionAbstract implements PaginableInterface |
19
|
|
|
{ |
20
|
|
|
public function set($key, $value) |
21
|
|
|
{ |
22
|
|
|
parent::set($key, $value); |
23
|
|
|
|
24
|
|
|
return $this; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param \Gpupo\Search\Query\KeywordsInterface $keywords |
29
|
|
|
*/ |
30
|
|
|
public function __construct(KeywordsInterface $keywords = null) |
31
|
|
|
{ |
32
|
|
|
$data = [ |
33
|
|
|
'index' => null, |
34
|
|
|
'filters' => null, |
35
|
|
|
'keywords' => ['primary' => $keywords], |
36
|
|
|
'fieldWeights' => [], |
37
|
|
|
'limit' => 20, |
38
|
|
|
'offset' => null, |
39
|
|
|
'countableAttributes' => [], |
40
|
|
|
]; |
41
|
|
|
|
42
|
|
|
parent::__construct($data); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Objeto de pesquisa. |
47
|
|
|
* |
48
|
|
|
* @param \Gpupo\Search\Query\KeywordsInterface $keyword |
49
|
|
|
* |
50
|
|
|
* @return type |
51
|
|
|
*/ |
52
|
|
|
public function setKeyword(KeywordsInterface $keyword) |
53
|
|
|
{ |
54
|
|
|
return $this->set('keywords', ['primary' => $keyword]); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function setKeywords(array $keywordsList) |
58
|
|
|
{ |
59
|
|
|
foreach ($keywordsList as $keywords) { |
60
|
|
|
if (!$keywords instanceof KeywordsInterface) { |
61
|
|
|
throw new \InvalidArgumentException('Cada item de $keywordsList deve implementar KeywordsInterface'); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $this->set('keywords', $keywordsList); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Acesso a um conjunto de KeywordsInterface. |
70
|
|
|
* |
71
|
|
|
* @todo Evoluir o uso da busca facetada para mais de uma Keyword |
72
|
|
|
*/ |
73
|
|
|
public function getKeywords() |
74
|
|
|
{ |
75
|
|
|
$keywords = $this->get('keywords'); |
76
|
|
|
|
77
|
|
|
return $keywords; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function getFilters() |
81
|
|
|
{ |
82
|
|
|
if ($this->get('filters') instanceof FiltersInterface) { |
83
|
|
|
return $this->get('filters')->toArray(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* SphinxSearch Queries Array. |
91
|
|
|
* |
92
|
|
|
* <code> |
93
|
|
|
* |
94
|
|
|
* //Search single query sintaxe: |
95
|
|
|
* array( |
96
|
|
|
* array( |
97
|
|
|
* 'key' => 'search_key', // The key to search on |
98
|
|
|
* 'values' => array( // The values to match with |
99
|
|
|
* 'value_one', |
100
|
|
|
* 'value_two', |
101
|
|
|
* ), |
102
|
|
|
* ) |
103
|
|
|
* ); |
104
|
|
|
* |
105
|
|
|
* //Search Multi query sintaxe: |
106
|
|
|
* array( |
107
|
|
|
* array( |
108
|
|
|
* 'key' => 'search_key', // The key to search on |
109
|
|
|
* 'values' => array( // The values to match with |
110
|
|
|
* 'value_one', |
111
|
|
|
* 'value_two', |
112
|
|
|
* ), |
113
|
|
|
* ), |
114
|
|
|
* array( |
115
|
|
|
* 'key' => 'another_key', // The key to search on |
116
|
|
|
* 'values' => array( // The values to match with |
117
|
|
|
* 'some_value', |
118
|
|
|
* ), |
119
|
|
|
* countableAttributes => array(), // For Faceted search |
120
|
|
|
* ), |
121
|
|
|
* ... |
122
|
|
|
* ); |
123
|
|
|
* |
124
|
|
|
* </code> |
125
|
|
|
*/ |
126
|
|
|
public function getQueries() |
127
|
|
|
{ |
128
|
|
|
$array = []; |
129
|
|
|
foreach ($this->getKeywords() as $keyword) { |
130
|
|
|
$value = $keyword->getData(); |
131
|
|
|
if ($this->getCountableAttributes()) { |
132
|
|
|
$value['countableAttributes'] = $this->getCountableAttributes(); |
133
|
|
|
} |
134
|
|
|
$array[] = $value; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return $array; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Field weights sintaxe:. |
142
|
|
|
* |
143
|
|
|
* <code> |
144
|
|
|
* |
145
|
|
|
* array( |
146
|
|
|
* 'field_one' => 5, |
147
|
|
|
* 'field_two' => 3, |
148
|
|
|
* ... |
149
|
|
|
* ); |
150
|
|
|
* </code> |
151
|
|
|
* |
152
|
|
|
* @todo Implementar Query Field weights |
153
|
|
|
*/ |
154
|
|
|
public function getFieldWeights() |
155
|
|
|
{ |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Acesso a quantidade de itens por pagina. |
160
|
|
|
* |
161
|
|
|
* @return int |
162
|
|
|
*/ |
163
|
|
|
public function getLimit() |
164
|
|
|
{ |
165
|
|
|
if ($this->getPaginator()) { |
166
|
|
|
return $this->getPaginator()->getItemNumberPerPage(); |
167
|
|
|
} else { |
168
|
|
|
return $this->get('limit'); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Offsets the result list by the number of places set by the count;. |
174
|
|
|
* |
175
|
|
|
* This would be used for pagination through results, where if you have 20 |
176
|
|
|
* results per 'page', the second page would begin at offset 20, the third |
177
|
|
|
* page at offset 40, etc. |
178
|
|
|
* |
179
|
|
|
* @return int |
180
|
|
|
*/ |
181
|
|
|
public function getOffSet() |
182
|
|
|
{ |
183
|
|
|
if ($this->getPaginator()) { |
184
|
|
|
return $this->getPaginator()->getOffset(); |
185
|
|
|
} else { |
186
|
|
|
$offset = $this->get('offset'); |
187
|
|
|
|
188
|
|
|
if (!$offset) { |
189
|
|
|
$offset = 0; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
return $offset; |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Mapeamento de index SphinxSearch. |
198
|
|
|
* |
199
|
|
|
* @throws \InvalidArgumentException |
200
|
|
|
* |
201
|
|
|
* @return string |
202
|
|
|
*/ |
203
|
|
|
public function getIndex() |
204
|
|
|
{ |
205
|
|
|
$index = $this->get('index'); |
206
|
|
|
|
207
|
|
|
if (empty($index)) { |
208
|
|
|
throw new \InvalidArgumentException('index nao informado'); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
return $index; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Mapeamento de index SphinxSearch. |
216
|
|
|
* |
217
|
|
|
* @param string $index |
218
|
|
|
* |
219
|
|
|
* @throws \InvalidArgumentException |
220
|
|
|
* |
221
|
|
|
* @return bool |
222
|
|
|
*/ |
223
|
|
|
public function setIndex($index) |
224
|
|
|
{ |
225
|
|
|
if (empty($index)) { |
226
|
|
|
throw new \InvalidArgumentException('index nao pode ser vazio'); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
return $this->set('index', $index); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
public function setFilters(FiltersInterface $filters) |
233
|
|
|
{ |
234
|
|
|
if (!$filters instanceof FiltersInterface) { |
235
|
|
|
throw new \Exception('Filtro invalido'); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
return $this->set('filters', $filters); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
public function setFieldWeights() |
242
|
|
|
{ |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
public function setLimit($limit) |
246
|
|
|
{ |
247
|
|
|
$validator = new \Zend\Validator\Digits(); |
248
|
|
|
if (!$validator->isValid($limit)) { |
249
|
|
|
throw new \InvalidArgumentException('Limit deve ser numerico'); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
return $this->set('limit', $limit); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
public function setOffSet($offset) |
256
|
|
|
{ |
257
|
|
|
$validator = new \Zend\Validator\Digits(); |
258
|
|
|
if (!$validator->isValid($offset)) { |
259
|
|
|
throw new \InvalidArgumentException('Offset deve ser numerico'); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
return $this->set('offset', $offset); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* @return \Gpupo\Search\Paginator\PaginatorInterface|bool |
267
|
|
|
*/ |
268
|
|
|
public function getPaginator() |
269
|
|
|
{ |
270
|
|
|
$paginator = $this->get('paginator'); |
271
|
|
|
|
272
|
|
|
if ($paginator instanceof PaginatorInterface) { |
273
|
|
|
return $paginator; |
274
|
|
|
} else { |
275
|
|
|
return false; |
276
|
|
|
} |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
public function setPaginator(PaginatorInterface $paginator) |
280
|
|
|
{ |
281
|
|
|
return $this->set('paginator', $paginator); |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
public function getCountableAttributes() |
285
|
|
|
{ |
286
|
|
|
return $this->get('countableAttributes'); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* Adiciona um atributo para contagem de resultados. |
291
|
|
|
* |
292
|
|
|
* Usado na busca facetada |
293
|
|
|
* |
294
|
|
|
* @param string $attribute |
295
|
|
|
* |
296
|
|
|
* @return bool |
297
|
|
|
*/ |
298
|
|
|
public function addCountableAttribute($attribute) |
299
|
|
|
{ |
300
|
|
|
if (empty($attribute)) { |
301
|
|
|
return false; |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
if (in_array($attribute, $this->getCountableAttributes(), true)) { |
305
|
|
|
return false; |
306
|
|
|
} |
307
|
|
|
$this->addToArrayValue('countableAttributes', $attribute); |
308
|
|
|
|
309
|
|
|
return $this; |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* Adiciona muitos atributos para contagem de resultados. |
314
|
|
|
* |
315
|
|
|
* @param array $attributes |
316
|
|
|
*/ |
317
|
|
|
public function setCountableAttributes(array $attributes) |
318
|
|
|
{ |
319
|
|
|
foreach ($attributes as $attribute) { |
320
|
|
|
$this->addCountableAttribute($attribute); |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
return $this; |
324
|
|
|
} |
325
|
|
|
} |
326
|
|
|
|