Completed
Push — master ( 09a846...387856 )
by Maxence
02:20
created

SearchRequest::getWildcardQueries()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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