Completed
Push — master ( c218d3...33ae5d )
by Maxence
02:12
created

SearchRequest::getOption()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 2
eloc 4
nc 2
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 $parts = [];
56
57
	/** @var array */
58
	private $wildcardQueries = [];
59
60
	/** @var array */
61
	private $wildcardFilters = [];
62
63
	/** @var array */
64
	private $regexFilters = [];
65
66
67
	/**
68
	 * SearchRequest constructor.
69
	 */
70
	public function __construct() {
71
	}
72
73
74
	/**
75
	 * @return array
76
	 */
77
	public function getProviders() {
78
		return $this->providers;
79
	}
80
81
	/**
82
	 * @param string|array $providers
83
	 */
84
	public function setProviders($providers) {
85
		if (!is_array($providers)) {
86
			$providers = [$providers];
87
		}
88
89
		$this->providers = $providers;
90
	}
91
92
93
	/**
94
	 * @return string
95
	 */
96
	public function getAuthor() {
97
		return $this->author;
98
	}
99
100
	/**
101
	 * @param string $author
102
	 */
103
	public function setAuthor($author) {
104
		$this->author = $author;
105
	}
106
107
108
	/**
109
	 * @return string
110
	 */
111
	public function getSearch() {
112
		return $this->search;
113
	}
114
115
	/**
116
	 * @param string $search
117
	 */
118
	public function setSearch($search) {
119
		$this->search = $search;
120
	}
121
122
	public function cleanSearch() {
123
		$this->search = trim(str_replace('  ', ' ', $this->search));
124
	}
125
126
127
	/**
128
	 * @return int
129
	 */
130
	public function getPage() {
131
		return $this->page;
132
	}
133
134
	/**
135
	 * @param int $page
136
	 */
137
	public function setPage($page) {
138
		if ($page < 1) {
139
			$page = 1;
140
		}
141
142
		$this->page = $page;
143
	}
144
145
146
	/**
147
	 * @return int
148
	 */
149
	public function getSize() {
150
		return $this->size;
151
	}
152
153
	/**
154
	 * @param int $size
155
	 */
156
	public function setSize($size) {
157
		$this->size = $size;
158
	}
159
160
161
	/**
162
	 * @return array
163
	 */
164
	public function getOptions() {
165
		return $this->options;
166
	}
167
168
	/**
169
	 * @param array $options
170
	 */
171
	public function setOptions($options) {
172
		$this->options = $options;
173
	}
174
175
	/**
176
	 * @param string $option
177
	 *
178
	 * @return mixed|string
179
	 */
180
	public function getOption($option) {
181
		if (array_key_exists($option, $this->options)) {
182
			return $this->options[$option];
183
		}
184
185
		return '';
186
	}
187
188
189
	/**
190
	 * @param array $parts
191
	 *
192
	 * @return $this
193
	 */
194
	public function setParts($parts) {
195
		$this->parts = $parts;
196
197
		return $this;
198
	}
199
200
	/**
201
	 * @return array
202
	 */
203
	public function getParts() {
204
		return $this->parts;
205
	}
206
207
208
	/**
209
	 * @param string $tag
210
	 */
211
	public function addTag($tag) {
212
		$this->tags[] = $tag;
213
	}
214
215
	/**
216
	 * @return array
217
	 */
218
	public function getTags() {
219
		return $this->tags;
220
	}
221
222
	/**
223
	 * @param array $tags
224
	 */
225
	public function setTags($tags) {
226
		$this->tags = $tags;
227
	}
228
229
230
	/**
231
	 * @param array $query
232
	 *
233
	 * @return $this
234
	 */
235
	public function addWildcardQuery($query) {
236
		$this->addWildcardQueries([$query]);
237
238
		return $this;
239
	}
240
241
	/**
242
	 * @param array $query
243
	 *
244
	 * @return $this
245
	 */
246
	public function addWildcardQueries($query) {
247
		array_push($this->wildcardQueries, $query);
248
249
		return $this;
250
	}
251
252
	/**
253
	 * @return array
254
	 */
255
	public function getWildcardQueries() {
256
		return $this->wildcardQueries;
257
	}
258
259
260
	/**
261
	 * @param array $filter
262
	 *
263
	 * @return $this
264
	 */
265
	public function addWildcardFilter($filter) {
266
		$this->addWildcardFilters([$filter]);
267
268
		return $this;
269
	}
270
271
	/**
272
	 * @param array $filters
273
	 *
274
	 * @return $this
275
	 */
276
	public function addWildcardFilters($filters) {
277
		array_push($this->wildcardFilters, $filters);
278
279
		return $this;
280
	}
281
282
	/**
283
	 * @return array
284
	 */
285
	public function getWildcardFilters() {
286
		return $this->wildcardFilters;
287
	}
288
289
290
	/**
291
	 * @param array $filter
292
	 *
293
	 * @return $this
294
	 */
295
	public function addRegexFilter($filter) {
296
		$this->addRegexFilters([$filter]);
297
298
		return $this;
299
	}
300
301
	/**
302
	 * @param array $filters
303
	 *
304
	 * @return $this
305
	 */
306
	public function addRegexFilters($filters) {
307
		array_push($this->regexFilters, $filters);
308
309
		return $this;
310
	}
311
312
	/**
313
	 * @return array
314
	 */
315
	public function getRegexFilters() {
316
		return $this->regexFilters;
317
	}
318
319
320
	/**
321
	 * @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...
322
	 */
323
	public function jsonSerialize() {
324
		return [
325
			'providers' => $this->getProviders(),
326
			'author'    => $this->getAuthor(),
327
			'search'    => $this->getSearch(),
328
			'page'      => $this->getPage(),
329
			'size'      => $this->getSize(),
330
			'parts'     => $this->getParts(),
331
			'options'   => $this->getOptions(),
332
			'tags'      => $this->getTags()
333
		];
334
	}
335
336
337
	/**
338
	 * @param string $json
339
	 *
340
	 * @return SearchRequest
341
	 */
342
	public static function fromJSON($json) {
343
		return self::fromArray(json_decode($json, true));
344
	}
345
346
	/**
347
	 * @param array $arr
348
	 *
349
	 * @return SearchRequest
350
	 */
351
	public static function fromArray($arr) {
352
		$request = new SearchRequest();
353
		$request->setProviders($arr['providers']);
354
		$request->setAuthor(MiscService::get($arr, 'author', ''));
355
		$request->setSearch(MiscService::get($arr, 'search', ''));
356
		$request->setPage(MiscService::get($arr, 'page', 0));
357
		$request->setParts(MiscService::get($arr, 'parts', []));
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...
358
		$request->setSize(MiscService::get($arr, 'size', 10));
359
		$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...
360
		$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...
361
362
		return $request;
363
	}
364
365
366
}