1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* FullTextSearch - Full text search framework for Nextcloud |
4
|
|
|
* |
5
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
6
|
|
|
* later. See the COPYING file. |
7
|
|
|
* |
8
|
|
|
* @author Maxence Lange <[email protected]> |
9
|
|
|
* @copyright 2018 |
10
|
|
|
* @license GNU AGPL version 3 or any later version |
11
|
|
|
* |
12
|
|
|
* This program is free software: you can redistribute it and/or modify |
13
|
|
|
* it under the terms of the GNU Affero General Public License as |
14
|
|
|
* published by the Free Software Foundation, either version 3 of the |
15
|
|
|
* License, or (at your option) any later version. |
16
|
|
|
* |
17
|
|
|
* This program is distributed in the hope that it will be useful, |
18
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
19
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
20
|
|
|
* GNU Affero General Public License for more details. |
21
|
|
|
* |
22
|
|
|
* You should have received a copy of the GNU Affero General Public License |
23
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
24
|
|
|
* |
25
|
|
|
*/ |
26
|
|
|
|
27
|
|
|
namespace OCA\FullTextSearch\Model; |
28
|
|
|
|
29
|
|
|
use OCA\FullTextSearch\Service\MiscService; |
30
|
|
|
|
31
|
|
|
class SearchRequest implements \JsonSerializable { |
32
|
|
|
|
33
|
|
|
/** @var array */ |
34
|
|
|
private $providers; |
35
|
|
|
|
36
|
|
|
/** @var string */ |
37
|
|
|
private $search; |
38
|
|
|
|
39
|
|
|
/** @var int */ |
40
|
|
|
private $page = 1; |
41
|
|
|
|
42
|
|
|
/** @var int */ |
43
|
|
|
private $size = 10; |
44
|
|
|
|
45
|
|
|
/** @var string */ |
46
|
|
|
private $author; |
47
|
|
|
|
48
|
|
|
/** @var array */ |
49
|
|
|
private $tags; |
50
|
|
|
|
51
|
|
|
/** @var array */ |
52
|
|
|
private $options; |
53
|
|
|
|
54
|
|
|
/** @var array */ |
55
|
|
|
private $parts = []; |
56
|
|
|
|
57
|
|
|
/** @var array */ |
58
|
|
|
private $fields = []; |
59
|
|
|
|
60
|
|
|
/** @var array */ |
61
|
|
|
private $wildcardQueries = []; |
62
|
|
|
|
63
|
|
|
/** @var array */ |
64
|
|
|
private $wildcardFilters = []; |
65
|
|
|
|
66
|
|
|
/** @var array */ |
67
|
|
|
private $regexFilters = []; |
68
|
|
|
|
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* SearchRequest constructor. |
72
|
|
|
*/ |
73
|
|
|
public function __construct() { |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return array |
79
|
|
|
*/ |
80
|
|
|
public function getProviders() { |
81
|
|
|
return $this->providers; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param string|array $providers |
86
|
|
|
*/ |
87
|
|
|
public function setProviders($providers) { |
88
|
|
|
if (!is_array($providers)) { |
89
|
|
|
$providers = [$providers]; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$this->providers = $providers; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
|
|
public function getAuthor() { |
100
|
|
|
return $this->author; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param string $author |
105
|
|
|
*/ |
106
|
|
|
public function setAuthor($author) { |
107
|
|
|
$this->author = $author; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return string |
113
|
|
|
*/ |
114
|
|
|
public function getSearch() { |
115
|
|
|
return $this->search; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param string $search |
120
|
|
|
*/ |
121
|
|
|
public function setSearch($search) { |
122
|
|
|
$this->search = $search; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function cleanSearch() { |
126
|
|
|
$this->search = trim(str_replace(' ', ' ', $this->search)); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @return int |
132
|
|
|
*/ |
133
|
|
|
public function getPage() { |
134
|
|
|
return $this->page; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param int $page |
139
|
|
|
*/ |
140
|
|
|
public function setPage($page) { |
141
|
|
|
if ($page < 1) { |
142
|
|
|
$page = 1; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
$this->page = $page; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @return int |
151
|
|
|
*/ |
152
|
|
|
public function getSize() { |
153
|
|
|
return $this->size; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param int $size |
158
|
|
|
*/ |
159
|
|
|
public function setSize($size) { |
160
|
|
|
$this->size = $size; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @return array |
166
|
|
|
*/ |
167
|
|
|
public function getOptions() { |
168
|
|
|
return $this->options; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @param array $options |
173
|
|
|
*/ |
174
|
|
|
public function setOptions($options) { |
175
|
|
|
$this->options = $options; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @param string $option |
180
|
|
|
* |
181
|
|
|
* @return mixed|string |
182
|
|
|
*/ |
183
|
|
|
public function getOption($option) { |
184
|
|
|
if (array_key_exists($option, $this->options)) { |
185
|
|
|
return $this->options[$option]; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
return ''; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @param array $parts |
194
|
|
|
* |
195
|
|
|
* @return $this |
196
|
|
|
*/ |
197
|
|
|
public function setParts($parts) { |
198
|
|
|
$this->parts = $parts; |
199
|
|
|
|
200
|
|
|
return $this; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @return array |
205
|
|
|
*/ |
206
|
|
|
public function getParts() { |
207
|
|
|
return $this->parts; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* @return array |
213
|
|
|
*/ |
214
|
|
|
public function getFields() { |
215
|
|
|
return $this->fields; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @param array $fields |
220
|
|
|
* |
221
|
|
|
* @return $this |
222
|
|
|
*/ |
223
|
|
|
public function setFields($fields) { |
224
|
|
|
$this->fields = $fields; |
225
|
|
|
|
226
|
|
|
return $this; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* @param $field |
231
|
|
|
* |
232
|
|
|
* @return $this |
233
|
|
|
*/ |
234
|
|
|
public function addField($field) { |
235
|
|
|
$this->fields[] = $field; |
236
|
|
|
|
237
|
|
|
return $this; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* @param string $tag |
243
|
|
|
*/ |
244
|
|
|
public function addTag($tag) { |
245
|
|
|
$this->tags[] = $tag; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* @return array |
250
|
|
|
*/ |
251
|
|
|
public function getTags() { |
252
|
|
|
return $this->tags; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* @param array $tags |
257
|
|
|
*/ |
258
|
|
|
public function setTags($tags) { |
259
|
|
|
$this->tags = $tags; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* @param array $query |
265
|
|
|
* |
266
|
|
|
* @return $this |
267
|
|
|
*/ |
268
|
|
|
public function addWildcardQuery($query) { |
269
|
|
|
$this->addWildcardQueries([$query]); |
270
|
|
|
|
271
|
|
|
return $this; |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* @param array $query |
276
|
|
|
* |
277
|
|
|
* @return $this |
278
|
|
|
*/ |
279
|
|
|
public function addWildcardQueries($query) { |
280
|
|
|
array_push($this->wildcardQueries, $query); |
281
|
|
|
|
282
|
|
|
return $this; |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* @return array |
287
|
|
|
*/ |
288
|
|
|
public function getWildcardQueries() { |
289
|
|
|
return $this->wildcardQueries; |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* @param array $filter |
295
|
|
|
* |
296
|
|
|
* @return $this |
297
|
|
|
*/ |
298
|
|
|
public function addWildcardFilter($filter) { |
299
|
|
|
$this->addWildcardFilters([$filter]); |
300
|
|
|
|
301
|
|
|
return $this; |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* @param array $filters |
306
|
|
|
* |
307
|
|
|
* @return $this |
308
|
|
|
*/ |
309
|
|
|
public function addWildcardFilters($filters) { |
310
|
|
|
array_push($this->wildcardFilters, $filters); |
311
|
|
|
|
312
|
|
|
return $this; |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* @return array |
317
|
|
|
*/ |
318
|
|
|
public function getWildcardFilters() { |
319
|
|
|
return $this->wildcardFilters; |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* @param array $filter |
325
|
|
|
* |
326
|
|
|
* @return $this |
327
|
|
|
*/ |
328
|
|
|
public function addRegexFilter($filter) { |
329
|
|
|
$this->addRegexFilters([$filter]); |
330
|
|
|
|
331
|
|
|
return $this; |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
/** |
335
|
|
|
* @param array $filters |
336
|
|
|
* |
337
|
|
|
* @return $this |
338
|
|
|
*/ |
339
|
|
|
public function addRegexFilters($filters) { |
340
|
|
|
array_push($this->regexFilters, $filters); |
341
|
|
|
|
342
|
|
|
return $this; |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
/** |
346
|
|
|
* @return array |
347
|
|
|
*/ |
348
|
|
|
public function getRegexFilters() { |
349
|
|
|
return $this->regexFilters; |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
|
353
|
|
|
/** |
354
|
|
|
* @return array |
|
|
|
|
355
|
|
|
*/ |
356
|
|
|
public function jsonSerialize() { |
357
|
|
|
return [ |
358
|
|
|
'providers' => $this->getProviders(), |
359
|
|
|
'author' => $this->getAuthor(), |
360
|
|
|
'search' => $this->getSearch(), |
361
|
|
|
'page' => $this->getPage(), |
362
|
|
|
'size' => $this->getSize(), |
363
|
|
|
'parts' => $this->getParts(), |
364
|
|
|
'options' => $this->getOptions(), |
365
|
|
|
'tags' => $this->getTags() |
366
|
|
|
]; |
367
|
|
|
} |
368
|
|
|
|
369
|
|
|
|
370
|
|
|
/** |
371
|
|
|
* @param string $json |
372
|
|
|
* |
373
|
|
|
* @return SearchRequest |
374
|
|
|
*/ |
375
|
|
|
public static function fromJSON($json) { |
376
|
|
|
return self::fromArray(json_decode($json, true)); |
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
/** |
380
|
|
|
* @param array $arr |
381
|
|
|
* |
382
|
|
|
* @return SearchRequest |
383
|
|
|
*/ |
384
|
|
|
public static function fromArray($arr) { |
385
|
|
|
$request = new SearchRequest(); |
386
|
|
|
$request->setProviders($arr['providers']); |
387
|
|
|
$request->setAuthor(MiscService::get($arr, 'author', '')); |
388
|
|
|
$request->setSearch(MiscService::get($arr, 'search', '')); |
389
|
|
|
$request->setPage(MiscService::get($arr, 'page', 0)); |
390
|
|
|
$request->setParts(MiscService::get($arr, 'parts', [])); |
|
|
|
|
391
|
|
|
$request->setSize(MiscService::get($arr, 'size', 10)); |
392
|
|
|
$request->setOptions(MiscService::get($arr, 'options', [])); |
|
|
|
|
393
|
|
|
$request->setTags(MiscService::get($arr, 'tags', [])); |
|
|
|
|
394
|
|
|
|
395
|
|
|
return $request; |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
|
399
|
|
|
} |
This check looks for the generic type
array
as a return type and suggests a more specific type. This type is inferred from the actual code.