Completed
Push — master ( f0c529...f57333 )
by Maxence
01:58
created

SearchRequest::addRegexFilter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
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 $wildcardQueries = [];
56
57
	/** @var array */
58
	private $wildcardFilters = [];
59
60
	/** @var array */
61
	private $regexFilters = [];
62
63
64
	/**
65
	 * SearchRequest constructor.
66
	 */
67
	public function __construct() {
68
	}
69
70
71
	/**
72
	 * @return array
73
	 */
74
	public function getProviders() {
75
		return $this->providers;
76
	}
77
78
	/**
79
	 * @param string|array $providers
80
	 */
81
	public function setProviders($providers) {
82
		if (!is_array($providers)) {
83
			$providers = [$providers];
84
		}
85
86
		$this->providers = $providers;
87
	}
88
89
90
	/**
91
	 * @return string
92
	 */
93
	public function getAuthor() {
94
		return $this->author;
95
	}
96
97
	/**
98
	 * @param string $author
99
	 */
100
	public function setAuthor($author) {
101
		$this->author = $author;
102
	}
103
104
105
	/**
106
	 * @return string
107
	 */
108
	public function getSearch() {
109
		return $this->search;
110
	}
111
112
	/**
113
	 * @param string $search
114
	 */
115
	public function setSearch($search) {
116
		$this->search = $search;
117
	}
118
119
	public function cleanSearch() {
120
		$this->search = trim(str_replace('  ', ' ', $this->search));
121
	}
122
123
124
	/**
125
	 * @return int
126
	 */
127
	public function getPage() {
128
		return $this->page;
129
	}
130
131
	/**
132
	 * @param int $page
133
	 */
134
	public function setPage($page) {
135
		if ($page < 1) {
136
			$page = 1;
137
		}
138
139
		$this->page = $page;
140
	}
141
142
143
	/**
144
	 * @return int
145
	 */
146
	public function getSize() {
147
		return $this->size;
148
	}
149
150
	/**
151
	 * @param int $size
152
	 */
153
	public function setSize($size) {
154
		$this->size = $size;
155
	}
156
157
158
	/**
159
	 * @return array
160
	 */
161
	public function getOptions() {
162
		return $this->options;
163
	}
164
165
	/**
166
	 * @param array $options
167
	 */
168
	public function setOptions($options) {
169
		$this->options = $options;
170
	}
171
172
	/**
173
	 * @param string $option
174
	 *
175
	 * @return mixed|string
176
	 */
177
	public function getOption($option) {
178
		if (array_key_exists($option, $this->options)) {
179
			return $this->options[$option];
180
		}
181
182
		return '';
183
	}
184
185
186
	/**
187
	 * @param string $tag
188
	 */
189
	public function addTag($tag) {
190
		$this->tags[] = $tag;
191
	}
192
193
	/**
194
	 * @return array
195
	 */
196
	public function getTags() {
197
		return $this->tags;
198
	}
199
200
	/**
201
	 * @param array $tags
202
	 */
203
	public function setTags($tags) {
204
		$this->tags = $tags;
205
	}
206
207
208
	/**
209
	 * @param array $query
210
	 *
211
	 * @return $this
212
	 */
213
	public function addWildcardQuery($query) {
214
		$this->addWildcardQueries([$query]);
215
216
		return $this;
217
	}
218
219
	/**
220
	 * @param array $query
221
	 *
222
	 * @return $this
223
	 */
224
	public function addWildcardQueries($query) {
225
		array_push($this->wildcardQueries, $query);
226
227
		return $this;
228
	}
229
230
	/**
231
	 * @return array
232
	 */
233
	public function getWildcardQueries() {
234
		return $this->wildcardQueries;
235
	}
236
237
238
	/**
239
	 * @param array $filter
240
	 *
241
	 * @return $this
242
	 */
243
	public function addWildcardFilter($filter) {
244
		$this->addWildcardFilters([$filter]);
245
246
		return $this;
247
	}
248
249
	/**
250
	 * @param array $filters
251
	 *
252
	 * @return $this
253
	 */
254
	public function addWildcardFilters($filters) {
255
		array_push($this->wildcardFilters, $filters);
256
257
		return $this;
258
	}
259
260
	/**
261
	 * @return array
262
	 */
263
	public function getWildcardFilters() {
264
		return $this->wildcardFilters;
265
	}
266
267
268
	/**
269
	 * @param array $filter
270
	 *
271
	 * @return $this
272
	 */
273
	public function addRegexFilter($filter) {
274
		$this->addRegexFilters([$filter]);
275
276
		return $this;
277
	}
278
279
	/**
280
	 * @param array $filters
281
	 *
282
	 * @return $this
283
	 */
284
	public function addRegexFilters($filters) {
285
		array_push($this->regexFilters, $filters);
286
287
		return $this;
288
	}
289
290
	/**
291
	 * @return array
292
	 */
293
	public function getRegexFilters() {
294
		return $this->regexFilters;
295
	}
296
297
298
	/**
299
	 * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,array|string|integer>.

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.

Loading history...
300
	 */
301
	public function jsonSerialize() {
302
		return [
303
			'providers' => $this->getProviders(),
304
			'author'    => $this->getAuthor(),
305
			'search'    => $this->getSearch(),
306
			'page'      => $this->getPage(),
307
			'size'      => $this->getSize(),
308
			'options'   => $this->getOptions(),
309
			'tags'      => $this->getTags()
310
		];
311
	}
312
313
314
	/**
315
	 * @param string $json
316
	 *
317
	 * @return SearchRequest
318
	 */
319
	public static function fromJSON($json) {
320
		return self::fromArray(json_decode($json, true));
321
	}
322
323
	/**
324
	 * @param array $arr
325
	 *
326
	 * @return SearchRequest
327
	 */
328
	public static function fromArray($arr) {
329
		$request = new SearchRequest();
330
		$request->setProviders($arr['providers']);
331
		$request->setAuthor(MiscService::get($arr, 'author', ''));
332
		$request->setSearch(MiscService::get($arr, 'search', ''));
333
		$request->setPage(MiscService::get($arr, 'page', 0));
334
		$request->setSize(MiscService::get($arr, 'size', 10));
335
		$request->setOptions(MiscService::get($arr, 'options', []));
0 ignored issues
show
Documentation introduced by
array() is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
336
		$request->setTags(MiscService::get($arr, 'tags', []));
0 ignored issues
show
Documentation introduced by
array() is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
337
338
		return $request;
339
	}
340
341
342
}