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

SearchTemplate::setTemplate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
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
use OCP\FullTextSearch\IFullTextSearchProvider;
36
37
38
/**
39
 * Class SearchTemplate
40
 *
41
 * This is a data transfer object that should be created by Content Provider
42
 * when the getSearchTemplate() method is called.
43
 *
44
 * The object will contain templates to be displayed, and the list of the different
45
 * options to be available to the user when he start a new search.
46
 *
47
 * The display of the Options is generated by the FullTextSearch app and Options
48
 * can be displayed in 2 places:
49
 *
50
 * - the navigation page of the app that generate the indexed content.
51
 *   (files, bookmarks, deck, mails, ...)
52
 * - the navigation page of the FullTextSearch app.
53
 *
54
 * Both pages will have different Options, and only the first one can integrate
55
 * a specific template.
56
 *
57
 * @see IFullTextSearchProvider::getSearchTemplate
58
 *
59
 * @since 15.0.0
60
 *
61
 * @package OCP\FullTextSearch\Model
62
 */
63
final class SearchTemplate implements JsonSerializable {
64
65
66
	/** @var string */
67
	private $icon = '';
68
69
	/** @var string */
70
	private $css = '';
71
72
	/** @var string */
73
	private $template = '';
74
75
	/** @var SearchOption[] */
76
	private $panelOptions = [];
77
78
	/** @var SearchOption[] */
79
	private $navigationOptions = [];
80
81
82
	/**
83
	 * SearchTemplate constructor.
84
	 *
85
	 * the class of the icon and the css file to be loaded can be set during the
86
	 * creation of the object.
87
	 *
88
	 * @since 15.0.0
89
	 *
90
	 * @param string $icon
91
	 * @param string $css
92
	 */
93
	public function __construct(string $icon = '', string $css = '') {
94
		$this->icon = $icon;
95
		$this->css = $css;
96
	}
97
98
99
	/**
100
	 * Set the class of the icon to be displayed in the left panel of the
101
	 * FullTextSearch navigation page, in front of the related Content Provider.
102
	 *
103
	 * @since 15.0.0
104
	 *
105
	 * @param string $class
106
	 *
107
	 * @return SearchTemplate
108
	 */
109
	public function setIcon(string $class): SearchTemplate {
110
		$this->icon = $class;
111
112
		return $this;
113
	}
114
115
	/**
116
	 * Get the class of the icon.
117
	 *
118
	 * @since 15.0.0
119
	 *
120
	 * @return string
121
	 */
122
	public function getIcon(): string {
123
		return $this->icon;
124
	}
125
126
127
	/**
128
	 * Set the path of a CSS file that will be loaded when needed.
129
	 *
130
	 * @since 15.0.0
131
	 *
132
	 * @param string $css
133
	 *
134
	 * @return SearchTemplate
135
	 */
136
	public function setCss(string $css): SearchTemplate {
137
		$this->css = $css;
138
139
		return $this;
140
	}
141
142
	/**
143
	 * Get the path of the CSS file.
144
	 *
145
	 * @since 15.0.0
146
	 *
147
	 * @return string
148
	 */
149
	public function getCss(): string {
150
		return $this->css;
151
	}
152
153
154
	/**
155
	 * Set the path of the file of a template that the HTML will be displayed
156
	 * below the Options.
157
	 * This should only be used if your Content Provider needs to set options in
158
	 * a way not generated by FullTextSearch
159
	 *
160
	 * @since 15.0.0
161
	 *
162
	 * @param string $template
163
	 *
164
	 * @return SearchTemplate
165
	 */
166
	public function setTemplate(string $template): SearchTemplate {
167
		$this->template = $template;
168
169
		return $this;
170
	}
171
172
	/**
173
	 * Get the path of the template file.
174
	 *
175
	 * @since 15.0.0
176
	 *
177
	 * @return string
178
	 */
179
	public function getTemplate(): string {
180
		return $this->template;
181
	}
182
183
184
	/**
185
	 * Add an option in the Panel that is displayed when the user start a search
186
	 * within the app that generate the content.
187
	 *
188
	 * @see SearchOption
189
	 *
190
	 * @since 15.0.0
191
	 *
192
	 * @param SearchOption $option
193
	 *
194
	 * @return SearchTemplate
195
	 */
196
	public function addPanelOption(SearchOption $option): SearchTemplate {
197
		$this->panelOptions[] = $option;
198
199
		return $this;
200
	}
201
202
	/**
203
	 * Get all options to be displayed in the Panel.
204
	 *
205
	 * @since 15.0.0
206
	 *
207
	 * @return SearchOption[]
208
	 */
209
	public function getPanelOptions(): array {
210
		return $this->panelOptions;
211
	}
212
213
214
	/**
215
	 * Add an option in the left panel of the FullTextSearch navigation page.
216
	 *
217
	 * @see SearchOption
218
	 *
219
	 * @since 15.0.0
220
	 *
221
	 * @param SearchOption $option
222
	 *
223
	 * @return SearchTemplate
224
	 */
225
	public function addNavigationOption(SearchOption $option): SearchTemplate {
226
		$this->navigationOptions[] = $option;
227
228
		return $this;
229
	}
230
231
	/**
232
	 * Get all options to be displayed in the FullTextSearch navigation page.
233
	 *
234
	 * @since 15.0.0
235
	 *
236
	 * @return array
237
	 */
238
	public function getNavigationOptions(): array {
239
		return $this->navigationOptions;
240
	}
241
242
243
	/**
244
	 * @since 15.0.0
245
	 *
246
	 * @return array
247
	 */
248
	public function jsonSerialize(): array {
249
		return [
250
			'icon' => $this->getIcon(),
251
			'css' => $this->getCss(),
252
			'template' => $this->getTemplate(),
253
			'panel' => $this->getPanelOptions(),
254
			'navigation' => $this->getNavigationOptions()
255
		];
256
	}
257
}
258
259