Completed
Push — master ( 2e50fa...e8ee15 )
by Maxence
04:11 queued 02:15
created

QueryContent::setMatch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
4
namespace OCA\FullTextSearch_ElasticSearch\Model;
5
6
7
class QueryContent {
8
9
10
	const OPTION_MUST = 1;
11
	const OPTION_MUST_NOT = 2;
12
13
14
	/** @var string */
15
	private $word;
16
17
	/** @var string */
18
	private $should;
19
20
	/** @var string */
21
	private $match;
22
23
	/** @var int */
24
	private $option;
25
26
27
	/** @var array */
28
	private $options = [
29
		'+' => [self::OPTION_MUST, 'must', 'prefix'],
30
		'-' => [self::OPTION_MUST_NOT, 'must_not', 'prefix']
31
	];
32
33
34
	/**
35
	 * SearchQueryContent constructor.
36
	 *
37
	 * @param string $word
38
	 */
39
	function __construct($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...
40
		$this->word = $word;
41
42
		$this->init();
43
	}
44
45
46
	private function init() {
47
		$this->setShould('should');
48
		$this->setMatch('match_phrase_prefix');
49
50
		$curr = substr($this->getWord(), 0, 1);
51
52
		if (array_key_exists($curr, $this->options)) {
53
			$this->setOption($this->options[$curr][0])
54
				 ->setShould($this->options[$curr][1])
55
				 ->setMatch($this->options[$curr][2])
56
				 ->setWord(substr($this->getWord(), 1));
57
		}
58
59
		if (substr($this->getWord(), 0, 1) === '"') {
60
			$this->setMatch('match');
61
			if (strpos($this->getWord(), " ") > -1) {
62
				$this->setMatch('match_phrase_prefix');
63
			}
64
		}
65
66
		$this->setWord(str_replace('"', '', $this->getWord()));
67
	}
68
69
70
	/**
71
	 * @return string
72
	 */
73
	public function getWord() {
74
		return $this->word;
75
	}
76
77
	/**
78
	 * @param string $word
79
	 */
80
	public function setWord($word) {
81
		$this->word = $word;
82
	}
83
84
85
	/**
86
	 * @return string
87
	 */
88
	public function getShould() {
89
		return $this->should;
90
	}
91
92
	/**
93
	 * @param string $should
94
	 *
95
	 * @return QueryContent
96
	 */
97
	public function setShould($should) {
98
		$this->should = $should;
99
100
		return $this;
101
	}
102
103
104
	/**
105
	 * @return string
106
	 */
107
	public function getMatch() {
108
		return $this->match;
109
	}
110
111
	/**
112
	 * @param string $match
113
	 *
114
	 * @return QueryContent
115
	 */
116
	public function setMatch($match) {
117
		$this->match = $match;
118
119
		return $this;
120
	}
121
122
123
	/**
124
	 * @return int
125
	 */
126
	public function getOption() {
127
		return $this->option;
128
	}
129
130
	/**
131
	 * @param int $option
132
	 *
133
	 * @return QueryContent
134
	 */
135
	public function setOption($option) {
136
		$this->option = $option;
137
138
		return $this;
139
	}
140
141
142
}