Passed
Push — master ( 64ec5e...ddd39f )
by Roeland
28:49 queued 15:55
created

SearchOption   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 250
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 35
dl 0
loc 250
rs 10
c 0
b 0
f 0
wmc 12

12 Methods

Rating   Name   Duplication   Size   Complexity  
A setSize() 0 4 1
A jsonSerialize() 0 7 1
A getTitle() 0 2 1
A getPlaceholder() 0 2 1
A getType() 0 2 1
A setName() 0 4 1
A setTitle() 0 4 1
A __construct() 0 6 1
A setType() 0 4 1
A getSize() 0 2 1
A setPlaceholder() 0 4 1
A getName() 0 2 1
1
<?php
2
declare(strict_types=1);
3
4
5
/**
6
 * FullTextSearch - Full text search framework for 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 OCP\FullTextSearch\Model;
32
33
34
use JsonSerializable;
35
36
37
/**
38
 * @since 15.0.0
39
 *
40
 * Class SearchOption
41
 *
42
 * @package OCP\FullTextSearch\Model
43
 */
44
final class SearchOption implements JsonSerializable {
45
46
47
	const CHECKBOX = 'checkbox';
48
	const INPUT = 'input';
49
50
	const INPUT_SMALL = 'small';
51
52
53
	/** @var string */
54
	private $name = '';
55
56
	/** @var string */
57
	private $title = '';
58
59
	/** @var string */
60
	private $type = '';
61
62
	/** @var string */
63
	private $size = '';
64
65
	/** @var string */
66
	private $placeholder = '';
67
68
69
	/**
70
	 *     *
71
	 *
72
	 * The array can be empty in case no search options are available.
73
	 * The format of the array must be like this:
74
	 *
75
	 * [
76
	 *   'panel' => [
77
	 *     'options' => [
78
	 *         OPTION1,
79
	 *         OPTION2,
80
	 *         OPTION3
81
	 *     ]
82
	 *   ],
83
	 *   'navigation' => [
84
	 *     'icon'    => 'css-class-of-the-icon',
85
	 *     'options' => [
86
	 *         OPTION1,
87
	 *         OPTION2,
88
	 *         OPTION3
89
	 *     ]
90
	 *   ]
91
	 * ]
92
	 *
93
	 * - PANEL contains entries that will be displayed in the app itself, when
94
	 *   a search is initiated.
95
	 * - NAVIGATION contains entries that will be available when using the
96
	 *   FullTextSearch navigation page
97
	 * - OPTION is an element that define each option available to the user.
98
	 *
99
	 * The format for the options must be like this:
100
	 *
101
	 * [
102
	 *   'name'        => 'name_of_the_option',
103
	 *   'title'       => 'Name displayed in the panel',
104
	 *   'type'        => '',
105
	 *   'size'        => ''   (optional),
106
	 *   'placeholder' => ''   (optional)
107
	 * ]
108
	 *
109
	 * - NAME is the variable name that is sent to the IFullTextSearchProvider
110
	 *   when a ISearchRequest is requested. (keys in the array returned by the
111
	 *   ISearchRequest->getOptions())
112
	 * - TYPE can be 'input' or 'checkbox'
113
	 * - SIZE is only used in case TYPE='input', default is 'large' but can be
114
	 *   'small'
115
	 * - PLACEHOLDER is only used in case TYPE='input', default is empty.
116
	 */
117
118
	/**
119
	 * SearchOption constructor.
120
	 *
121
	 * Some value can be setduring the creation of the object.
122
	 *
123
	 * @since 15.0.0
124
	 *
125
	 * @param string $name
126
	 * @param string $title
127
	 * @param string $type
128
	 * @param string $size
129
	 * @param string $placeholder
130
	 */
131
	public function __construct(string $name = '', string $title = '', string $type = '', string $size = '', string $placeholder = '') {
132
		$this->name = $name;
133
		$this->title = $title;
134
		$this->type = $type;
135
		$this->size = $size;
136
		$this->placeholder = $placeholder;
137
	}
138
139
140
	/**
141
	 * Set the name/key of the option.
142
	 * The string should only contains alphanumerical chars and underscore.
143
	 * The key can be retrieve when using ISearchRequest::getOption
144
	 *
145
	 * @see ISearchRequest::getOption
146
	 *
147
	 * @since 15.0.0
148
	 *
149
	 * @param string $name
150
	 *
151
	 * @return SearchOption
152
	 */
153
	public function setName(string $name): SearchOption {
154
		$this->name = $name;
155
156
		return $this;
157
	}
158
159
	/**
160
	 * Get the name/key of the option.
161
	 *
162
	 * @since 15.0.0
163
	 *
164
	 * @return string
165
	 */
166
	public function getName(): string {
167
		return $this->name;
168
	}
169
170
171
	/**
172
	 * Set the title/display name of the option.
173
	 *
174
	 * @since 15.0.0
175
	 *
176
	 * @param string $title
177
	 *
178
	 * @return SearchOption
179
	 */
180
	public function setTitle(string $title): SearchOption {
181
		$this->title = $title;
182
183
		return $this;
184
	}
185
186
	/**
187
	 * Get the title of the option.
188
	 *
189
	 * @since 15.0.0
190
	 *
191
	 * @return string
192
	 */
193
	public function getTitle(): string {
194
		return $this->title;
195
	}
196
197
198
	/**
199
	 * Set the type of the option.
200
	 * $type can be SearchOption::CHECKBOX or SearchOption::INPUT
201
	 *
202
	 * @since 15.0.0
203
	 *
204
	 * @param string $type
205
	 *
206
	 * @return SearchOption
207
	 */
208
	public function setType(string $type): SearchOption {
209
		$this->type = $type;
210
211
		return $this;
212
	}
213
214
	/**
215
	 * Get the type of the option.
216
	 *
217
	 * @since 15.0.0
218
	 *
219
	 * @return string
220
	 */
221
	public function getType(): string {
222
		return $this->type;
223
	}
224
225
226
	/**
227
	 * In case of Type is INPUT, set the size of the input field.
228
	 * Value can be SearchOption::INPUT_SMALL or not defined.
229
	 *
230
	 * @since 15.0.0
231
	 *
232
	 * @param string $size
233
	 *
234
	 * @return SearchOption
235
	 */
236
	public function setSize(string $size): SearchOption {
237
		$this->size = $size;
238
239
		return $this;
240
	}
241
242
	/**
243
	 * Get the size of the INPUT.
244
	 *
245
	 * @since 15.0.0
246
	 *
247
	 * @return string
248
	 */
249
	public function getSize(): string {
250
		return $this->size;
251
	}
252
253
254
	/**
255
	 * In case of Type is , set the placeholder to be displayed in the input
256
	 * field.
257
	 *
258
	 * @since 15.0.0
259
	 *
260
	 * @param string $placeholder
261
	 *
262
	 * @return SearchOption
263
	 */
264
	public function setPlaceholder(string $placeholder): SearchOption {
265
		$this->placeholder = $placeholder;
266
267
		return $this;
268
	}
269
270
	/**
271
	 * Get the placeholder.
272
	 *
273
	 * @since 15.0.0
274
	 *
275
	 * @return string
276
	 */
277
	public function getPlaceholder(): string {
278
		return $this->placeholder;
279
	}
280
281
282
	/**
283
	 * @since 15.0.0
284
	 *
285
	 * @return array
286
	 */
287
	public function jsonSerialize(): array {
288
		return [
289
			'name' => $this->getName(),
290
			'title' => $this->getTitle(),
291
			'type' => $this->getType(),
292
			'size' => $this->getSize(),
293
			'placeholder' => $this->getPlaceholder()
294
		];
295
	}
296
}
297