Completed
Push — master ( 2566be...2cb116 )
by Maxence
01:40 queued 10s
created

QueryContent::setMatch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
declare(strict_types=1);
3
4
5
/**
6
 * FullTextSearch_Elasticsearch - Use Elasticsearch to index the content of your nextcloud
7
 *
8
 * This file is licensed under the Affero General Public License version 3 or
9
 * later. See the COPYING file.
10
 *
11
 * @author Maxence Lange <[email protected]>
12
 * @copyright 2018
13
 * @license GNU AGPL version 3 or any later version
14
 *
15
 * This program is free software: you can redistribute it and/or modify
16
 * it under the terms of the GNU Affero General Public License as
17
 * published by the Free Software Foundation, either version 3 of the
18
 * License, or (at your option) any later version.
19
 *
20
 * This program is distributed in the hope that it will be useful,
21
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 * GNU Affero General Public License for more details.
24
 *
25
 * You should have received a copy of the GNU Affero General Public License
26
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27
 *
28
 */
29
30
31
namespace OCA\FullTextSearch_Elasticsearch\Model;
32
33
34
use JsonSerializable;
35
36
37
/**
38
 * Class QueryContent
39
 *
40
 * @package OCA\FullTextSearch_Elasticsearch\Model
41
 */
42
class QueryContent implements JsonSerializable {
43
44
45
	const OPTION_MUST = 1;
46
	const OPTION_MUST_NOT = 2;
47
48
49
	/** @var string */
50
	private $word;
51
52
	/** @var string */
53
	private $should;
54
55
	/** @var string */
56
	private $match;
57
58
	/** @var int */
59
	private $option = 0;
60
61
62
	/** @var array */
63
	private $options = [
64
		'+' => [self::OPTION_MUST, 'must', 'match_phrase_prefix'],
65
		'-' => [self::OPTION_MUST_NOT, 'must_not', 'match_phrase_prefix']
66
	];
67
68
69
	/**
70
	 * QueryContent constructor.
71
	 *
72
	 * @param string $word
73
	 */
74
	function __construct(string $word) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
75
		$this->word = $word;
76
77
		$this->init();
78
	}
79
80
81
	/**
82
	 *
83
	 */
84
	private function init() {
85
		$this->setShould('should');
86
		$this->setMatch('match_phrase_prefix');
87
88
		$curr = substr($this->getWord(), 0, 1);
89
90
		if (array_key_exists($curr, $this->options)) {
91
			$this->setOption($this->options[$curr][0])
92
				 ->setShould($this->options[$curr][1])
93
				 ->setMatch($this->options[$curr][2])
94
				 ->setWord(substr($this->getWord(), 1));
95
		}
96
97
		if (substr($this->getWord(), 0, 1) === '"') {
98
			$this->setMatch('match');
99
			if (strpos($this->getWord(), " ") > -1) {
100
				$this->setMatch('match_phrase_prefix');
101
			}
102
		}
103
104
		$this->setWord(str_replace('"', '', $this->getWord()));
105
	}
106
107
108
	/**
109
	 * @return string
110
	 */
111
	public function getWord(): string {
112
		return $this->word;
113
	}
114
115
	/**
116
	 * @param string $word
117
	 *
118
	 * @return $this
119
	 */
120
	public function setWord(string $word): QueryContent {
121
		$this->word = $word;
122
123
		return $this;
124
	}
125
126
127
	/**
128
	 * @return string
129
	 */
130
	public function getShould(): string {
131
		return $this->should;
132
	}
133
134
	/**
135
	 * @param string $should
136
	 *
137
	 * @return QueryContent
138
	 */
139
	public function setShould(string $should): QueryContent {
140
		$this->should = $should;
141
142
		return $this;
143
	}
144
145
146
	/**
147
	 * @return string
148
	 */
149
	public function getMatch(): string {
150
		return $this->match;
151
	}
152
153
	/**
154
	 * @param string $match
155
	 *
156
	 * @return QueryContent
157
	 */
158
	public function setMatch(string $match): QueryContent {
159
		$this->match = $match;
160
161
		return $this;
162
	}
163
164
165
	/**
166
	 * @return int
167
	 */
168
	public function getOption(): int {
169
		return $this->option;
170
	}
171
172
	/**
173
	 * @param int $option
174
	 *
175
	 * @return QueryContent
176
	 */
177
	public function setOption(int $option): QueryContent {
178
		$this->option = $option;
179
180
		return $this;
181
	}
182
183
184
	/**
185
	 * @return array
186
	 */
187
	public function jsonSerialize(): array {
188
		return [
189
			'word'   => $this->getWord(),
190
			'should' => $this->getShould(),
191
			'match'  => $this->getMatch(),
192
			'option' => $this->getOption()
193
		];
194
	}
195
196
}
197
198