Completed
Push — master ( affabd...c701bc )
by Maxence
02:37
created

SearchRequest::addSubTag()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 2
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
	public $metaTags = [];
53
54
	/** @var array */
55
	public $subTags = [];
56
57
	/** @var array */
58
	private $options;
59
60
	/** @var array */
61
	private $parts = [];
62
63
	/** @var array */
64
	private $fields = [];
65
66
	/** @var array */
67
	private $limitFields = [];
68
69
	/** @var array */
70
	private $wildcardFields = [];
71
72
//	/** @var array */
0 ignored issues
show
Unused Code Comprehensibility introduced by
36% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
73
//	private $wildcardQueries = [];
74
75
	/** @var array */
76
	private $wildcardFilters = [];
77
78
	/** @var array */
79
	private $regexFilters = [];
80
81
82
	/**
83
	 * SearchRequest constructor.
84
	 */
85
	public function __construct() {
86
	}
87
88
89
	/**
90
	 * @return array
91
	 */
92
	public function getProviders() {
93
		return $this->providers;
94
	}
95
96
	/**
97
	 * @param string|array $providers
98
	 */
99
	public function setProviders($providers) {
100
		if (!is_array($providers)) {
101
			$providers = [$providers];
102
		}
103
104
		$this->providers = $providers;
105
	}
106
107
108
	/**
109
	 * @return string
110
	 */
111
	public function getAuthor() {
112
		return $this->author;
113
	}
114
115
	/**
116
	 * @param string $author
117
	 */
118
	public function setAuthor($author) {
119
		$this->author = $author;
120
	}
121
122
123
	/**
124
	 * @return string
125
	 */
126
	public function getSearch() {
127
		return $this->search;
128
	}
129
130
	/**
131
	 * @param string $search
132
	 */
133
	public function setSearch($search) {
134
		$this->search = $search;
135
	}
136
137
138
	/**
139
	 *
140
	 */
141
	public function cleanSearch() {
142
		$search = trim(str_replace('  ', ' ', $this->getSearch()));
143
144
		preg_match_all('/[^?]"(?:\\\\.|[^\\\\"])*"|\S+/', " $search ", $words);
145
		$searchItems = [];
146
		foreach ($words[0] as $word) {
147
			if ($this->searchQueryOptions($word)) {
148
				continue;
149
			}
150
151
			$searchItems[] = $word;
152
		}
153
154
		$this->setSearch(implode(" ", $searchItems));
155
	}
156
157
158
	/**
159
	 * @param $word
160
	 *
161
	 * @return bool
162
	 */
163
	private function searchQueryOptions($word) {
164
		if (($pos = strpos($word, ':')) === false) {
165
			return false;
166
		}
167
168
		list($kw, $value) = explode(':', $word, 2);
169
170
		$options = ['is', 'show'];
171
		if (in_array($kw, $options)) {
172
			$this->addOption($kw . '_' . $value, '1');
173
174
			return true;
175
		}
176
177
		$valuedOptions = ['in'];
178
		if (in_array($kw, $valuedOptions)) {
179
			$this->addMultipleOption($kw, $value);
180
181
			return true;
182
		}
183
184
		return false;
185
	}
186
187
188
	/**
189
	 * @return int
190
	 */
191
	public function getPage() {
192
		return $this->page;
193
	}
194
195
	/**
196
	 * @param int $page
197
	 */
198
	public function setPage($page) {
199
		if ($page < 1) {
200
			$page = 1;
201
		}
202
203
		$this->page = $page;
204
	}
205
206
207
	/**
208
	 * @return int
209
	 */
210
	public function getSize() {
211
		return $this->size;
212
	}
213
214
	/**
215
	 * @param int $size
216
	 */
217
	public function setSize($size) {
218
		$this->size = $size;
219
	}
220
221
222
	/**
223
	 * @return array
224
	 */
225
	public function getOptions() {
226
		return $this->options;
227
	}
228
229
	/**
230
	 * @param array $options
231
	 */
232
	public function setOptions($options) {
233
		$this->options = $options;
234
	}
235
236
	/**
237
	 * @param $key
238
	 * @param $value
239
	 *
240
	 * @return $this
241
	 */
242
	public function addOption($key, $value) {
243
		$this->options[$key] = $value;
244
245
		return $this;
246
	}
247
248
	/**
249
	 * @param $key
250
	 * @param $value
251
	 *
252
	 * @return $this
253
	 */
254
	public function addMultipleOption($key, $value) {
255
		if (!array_key_exists($key, $this->options)) {
256
			$this->options[$key] = [];
257
		}
258
259
		$this->options[$key][] = $value;
260
261
		return $this;
262
	}
263
264
	/**
265
	 * @param string $option
266
	 *
267
	 * @return mixed|string
268
	 */
269
	public function getOption($option) {
270
		if (array_key_exists($option, $this->options)) {
271
			return $this->options[$option];
272
		}
273
274
		return '';
275
	}
276
277
278
	/**
279
	 * @param array $parts
280
	 *
281
	 * @return $this
282
	 */
283
	public function setParts($parts) {
284
		$this->parts = $parts;
285
286
		return $this;
287
	}
288
289
	/**
290
	 * @return array
291
	 */
292
	public function getParts() {
293
		return $this->parts;
294
	}
295
296
297
	/**
298
	 * @return array
299
	 */
300
	public function getFields() {
301
		return $this->fields;
302
	}
303
304
	/**
305
	 * @param array $fields
306
	 *
307
	 * @return $this
308
	 */
309
	public function setFields($fields) {
310
		$this->fields = $fields;
311
312
		return $this;
313
	}
314
315
316
	/**
317
	 * @param $field
318
	 *
319
	 * @return $this
320
	 */
321
	public function limitToField($field) {
322
		array_push($this->limitFields, $field);
323
324
		return $this;
325
	}
326
327
	/**
328
	 * @return array
329
	 */
330
	public function getLimitFields() {
331
		return $this->limitFields;
332
	}
333
334
335
	/**
336
	 * @param $field
337
	 *
338
	 * @return $this
339
	 */
340
	public function addField($field) {
341
		$this->fields[] = $field;
342
343
		return $this;
344
	}
345
346
347
	/**
348
	 * @param string $tag
349
	 */
350
	public function addTag($tag) {
351
		$this->tags[] = $tag;
352
	}
353
354
	/**
355
	 * @return array
356
	 */
357
	public function getTags() {
358
		return $this->tags;
359
	}
360
361
	/**
362
	 * @param array $tags
363
	 */
364
	public function setTags($tags) {
365
		$this->tags = $tags;
366
	}
367
368
369
	/**
370
	 * @param array $tags
371
	 *
372
	 * @return $this
373
	 */
374
	public function setMetaTags($tags) {
375
		$this->metaTags = $tags;
376
377
		return $this;
378
	}
379
380
	/**
381
	 * @return array
382
	 */
383
	public function getMetaTags() {
384
		return $this->metaTags;
385
	}
386
387
	/**
388
	 * @param string $tags
389
	 *
390
	 * @return $this
391
	 */
392
	public function addMetaTag($tags) {
393
		$this->metaTags[] = $tags;
394
395
		return $this;
396
	}
397
398
399
	/**
400
	 * @param array $tags
401
	 *
402
	 * @return $this
403
	 */
404
	public function setSubTags($tags) {
405
		$this->subTags = $tags;
406
407
		return $this;
408
	}
409
410
	/**
411
	 * @param bool $formatted
412
	 *
413
	 * @return array
414
	 */
415 View Code Duplication
	public function getSubTags($formatted = false) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
416
		if ($formatted === false) {
417
			return $this->subTags;
418
		}
419
420
		$subTags = [];
421
		$ak = array_keys($this->subTags);
422
		foreach ($ak as $source) {
423
			$tags = $this->subTags[$source];
424
			foreach ($tags as $tag) {
425
				$subTags[] = $source . '_' . $tag;
426
			}
427
		}
428
429
		return $subTags;
430
	}
431
432
	/**
433
	 * @param string $source
434
	 * @param string $tag
435
	 *
436
	 * @return $this
437
	 */
438
	public function addSubTag($source, $tag) {
439
		if (!array_key_exists($source, $this->subTags)) {
440
			$this->subTags[$source] = [];
441
		}
442
443
		$this->subTags[$source][] = $tag;
444
445
		return $this;
446
	}
447
448
449
	/**
450
	 * @param string $field
451
	 *
452
	 * @return $this
453
	 */
454
	public function addWildcardField($field) {
455
		$this->wildcardFields[] = $field;
456
457
		return $this;
458
	}
459
460
	/**
461
	 * @return array
462
	 */
463
	public function getWildcardFields() {
464
		return $this->wildcardFields;
465
	}
466
467
//
0 ignored issues
show
Unused Code Comprehensibility introduced by
37% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
468
//	/**
469
//	 * @param array $query
470
//	 *
471
//	 * @return $this
472
//	 */
473
//	public function addWildcardQuery($query) {
474
//		$this->addWildcardQueries([$query]);
475
//
476
//		return $this;
477
//	}
478
//
479
//	/**
480
//	 * @param array $query
481
//	 *
482
//	 * @return $this
483
//	 */
484
//	public function addWildcardQueries($query) {
485
//		array_push($this->wildcardQueries, $query);
486
//
487
//		return $this;
488
//	}
489
//
490
//	/**
491
//	 * @return array
492
//	 */
493
//	public function getWildcardQueries() {
494
//		return $this->wildcardQueries;
495
//	}
496
497
498
	/**
499
	 * @param array $filter
500
	 *
501
	 * @return $this
502
	 */
503
	public function addWildcardFilter($filter) {
504
		$this->addWildcardFilters([$filter]);
505
506
		return $this;
507
	}
508
509
	/**
510
	 * @param array $filters
511
	 *
512
	 * @return $this
513
	 */
514
	public function addWildcardFilters($filters) {
515
		array_push($this->wildcardFilters, $filters);
516
517
		return $this;
518
	}
519
520
	/**
521
	 * @return array
522
	 */
523
	public function getWildcardFilters() {
524
		return $this->wildcardFilters;
525
	}
526
527
528
	/**
529
	 * @param array $filter
530
	 *
531
	 * @return $this
532
	 */
533
	public function addRegexFilter($filter) {
534
		$this->addRegexFilters([$filter]);
535
536
		return $this;
537
	}
538
539
	/**
540
	 * @param array $filters
541
	 *
542
	 * @return $this
543
	 */
544
	public function addRegexFilters($filters) {
545
		array_push($this->regexFilters, $filters);
546
547
		return $this;
548
	}
549
550
	/**
551
	 * @return array
552
	 */
553
	public function getRegexFilters() {
554
		return $this->regexFilters;
555
	}
556
557
558
	/**
559
	 * @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...
560
	 */
561
	public function jsonSerialize() {
562
		return [
563
			'providers' => $this->getProviders(),
564
			'author'    => $this->getAuthor(),
565
			'search'    => $this->getSearch(),
566
			'page'      => $this->getPage(),
567
			'size'      => $this->getSize(),
568
			'parts'     => $this->getParts(),
569
			'options'   => $this->getOptions(),
570
			'metatags'  => $this->getMetaTags(),
571
			'subtags'   => $this->getSubTags(),
572
			'tags'      => $this->getTags()
573
		];
574
	}
575
576
577
	/**
578
	 * @param string $json
579
	 *
580
	 * @return SearchRequest
581
	 */
582
	public static function fromJSON($json) {
583
		return self::fromArray(json_decode($json, true));
584
	}
585
586
	/**
587
	 * @param array $arr
588
	 *
589
	 * @return SearchRequest
590
	 */
591
	public static function fromArray($arr) {
592
		$request = new SearchRequest();
593
		$request->setProviders($arr['providers']);
594
		$request->setAuthor(MiscService::get('author', $arr, ''));
595
		$request->setSearch(MiscService::get('search', $arr, ''));
596
		$request->setPage(MiscService::get('page', $arr, 0));
597
		$request->setParts(MiscService::get('parts', $arr, []));
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...
598
		$request->setSize(MiscService::get('size', $arr, 10));
599
		$request->setOptions(MiscService::get('options', $arr, []));
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...
600
		$request->setMetaTags(MiscService::get('metatags', $arr, []));
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...
601
		$request->setSubTags(MiscService::get('subtags', $arr, []));
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...
602
		$request->setTags(MiscService::get('tags', $arr, []));
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...
603
604
		return $request;
605
	}
606
607
608
}