Completed
Push — master ( 11144a...f5a495 )
by Maxence
12:24 queued 10s
created

WidgetTemplate::jsonSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
5
/**
6
 * Nextcloud - Dashboard App
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, Maxence Lange <[email protected]>
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
namespace OCP\Dashboard\Model;
31
32
33
use JsonSerializable;
34
35
36
/**
37
 * Interface WidgetTemplate
38
 *
39
 * A widget must create an WidgetTemplate object and returns it in the
40
 * IDashboardWidget::getWidgetTemplate method.
41
 *
42
 * @see IDashboardWidget::getWidgetTemplate
43
 *
44
 * @since 15.0.0
45
 *
46
 * @package OCP\Dashboard\Model
47
 */
48
final class WidgetTemplate implements JsonSerializable {
49
50
51
	/** @var string */
52
	private $icon = '';
53
54
	/** @var array */
55
	private $css = [];
56
57
	/** @var array */
58
	private $js = [];
59
60
	/** @var string */
61
	private $content = '';
62
63
	/** @var string */
64
	private $function = '';
65
66
	/** @var WidgetSetting[] */
67
	private $settings = [];
68
69
70
	/**
71
	 * Get the icon class of the widget.
72
	 *
73
	 * @since 15.0.0
74
	 *
75
	 * @return string
76
	 */
77
	public function getIcon(): string {
78
		return $this->icon;
79
	}
80
81
	/**
82
	 * Set the icon class of the widget.
83
	 * This class must be defined in one of the CSS file used by the widget.
84
	 *
85
	 * @see addCss
86
	 *
87
	 * @since 15.0.0
88
	 *
89
	 * @param string $icon
90
	 *
91
	 * @return WidgetTemplate
92
	 */
93
	public function setIcon(string $icon): WidgetTemplate {
94
		$this->icon = $icon;
95
96
		return $this;
97
	}
98
99
	/**
100
	 * Get CSS files to be included when displaying a widget
101
	 *
102
	 * @since 15.0.0
103
	 *
104
	 * @return array
105
	 */
106
	public function getCss(): array {
107
		return $this->css;
108
	}
109
110
	/**
111
	 * path and name of CSS files
112
	 *
113
	 * @since 15.0.0
114
	 *
115
	 * @param array $css
116
	 *
117
	 * @return WidgetTemplate
118
	 */
119
	public function setCss(array $css): WidgetTemplate {
120
		$this->css = $css;
121
122
		return $this;
123
	}
124
125
	/**
126
	 * Add a CSS file to be included when displaying a widget.
127
	 *
128
	 * @since 15.0.0
129
	 *
130
	 * @param string $css
131
	 *
132
	 * @return WidgetTemplate
133
	 */
134
	public function addCss(string $css): WidgetTemplate {
135
		$this->css[] = $css;
136
137
		return $this;
138
	}
139
140
	/**
141
	 * Get JS files to be included when loading a widget
142
	 *
143
	 * @since 15.0.0
144
	 *
145
	 * @return array
146
	 */
147
	public function getJs(): array {
148
		return $this->js;
149
	}
150
151
	/**
152
	 * Set an array of JS files to be included when loading a widget.
153
	 *
154
	 * @since 15.0.0
155
	 *
156
	 * @param array $js
157
	 *
158
	 * @return WidgetTemplate
159
	 */
160
	public function setJs(array $js): WidgetTemplate {
161
		$this->js = $js;
162
163
		return $this;
164
	}
165
166
	/**
167
	 * Add a JS file to be included when loading a widget.
168
	 *
169
	 * @since 15.0.0
170
	 *
171
	 * @param string $js
172
	 *
173
	 * @return WidgetTemplate
174
	 */
175
	public function addJs(string $js): WidgetTemplate {
176
		$this->js[] = $js;
177
178
		return $this;
179
	}
180
181
	/**
182
	 * Get the HTML file that contains the content of the widget.
183
	 *
184
	 * @since 15.0.0
185
	 *
186
	 * @return string
187
	 */
188
	public function getContent(): string {
189
		return $this->content;
190
	}
191
192
	/**
193
	 * Set the HTML file that contains the content of the widget.
194
	 *
195
	 * @since 15.0.0
196
	 *
197
	 * @param string $content
198
	 *
199
	 * @return WidgetTemplate
200
	 */
201
	public function setContent(string $content): WidgetTemplate {
202
		$this->content = $content;
203
204
		return $this;
205
	}
206
207
	/**
208
	 * Get the JS function to be called when loading the widget.
209
	 *
210
	 * @since 15.0.0
211
	 *
212
	 * @return string
213
	 */
214
	public function getInitFunction(): string {
215
		return $this->function;
216
	}
217
218
	/**
219
	 * JavaScript function to be called when loading the widget on the
220
	 * dashboard
221
	 *
222
	 * @since 15.0.0
223
	 *
224
	 * @param string $function
225
	 *
226
	 * @return WidgetTemplate
227
	 */
228
	public function setInitFunction(string $function): WidgetTemplate {
229
		$this->function = $function;
230
231
		return $this;
232
	}
233
234
	/**
235
	 * Get all WidgetSetting defined for the widget.
236
	 *
237
	 * @see WidgetSetting
238
	 *
239
	 * @since 15.0.0
240
	 *
241
	 * @return WidgetSetting[]
242
	 */
243
	public function getSettings(): array {
244
		return $this->settings;
245
	}
246
247
	/**
248
	 * Define all WidgetSetting for the widget.
249
	 *
250
	 * @since 15.0.0
251
	 *
252
	 * @see WidgetSetting
253
	 *
254
	 * @param WidgetSetting[] $settings
255
	 *
256
	 * @return WidgetTemplate
257
	 */
258
	public function setSettings(array $settings): WidgetTemplate {
259
		$this->settings = $settings;
260
261
		return $this;
262
	}
263
264
	/**
265
	 * Add a WidgetSetting.
266
	 *
267
	 * @see WidgetSetting
268
	 *
269
	 * @since 15.0.0
270
	 *
271
	 * @param WidgetSetting $setting
272
	 *
273
	 * @return WidgetTemplate
274
	 */
275
	public function addSetting(WidgetSetting $setting): WidgetTemplate {
276
		$this->settings[] = $setting;
277
278
		return $this;
279
	}
280
281
	/**
282
	 * Get a WidgetSetting by its name
283
	 *
284
	 * @see WidgetSetting::setName
285
	 *
286
	 * @since 15.0.0
287
	 *
288
	 * @param string $key
289
	 *
290
	 * @return WidgetSetting
291
	 */
292
	public function getSetting(string $key): WidgetSetting {
293
		if (!array_key_exists($key, $this->settings)) {
294
			return null;
295
		}
296
297
		return $this->settings[$key];
298
	}
299
300
301
	/**
302
	 * @since 15.0.0
303
	 *
304
	 * @return array
305
	 */
306
	public function jsonSerialize() {
307
		return [
308
			'icon' => $this->getIcon(),
309
			'css' => $this->getCss(),
310
			'js' => $this->getJs(),
311
			'content' => $this->getContent(),
312
			'function' => $this->getInitFunction(),
313
			'settings' => $this->getSettings()
314
		];
315
	}
316
317
318
}
319
320