Passed
Push — master ( 2cfe21...f7c149 )
by Maxence
02:23
created

WebsiteCore::setOptions()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 3
eloc 6
nc 4
nop 1
1
<?php
2
/**
3
 * CMS Pico - Integration of Pico within your files to create websites.
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Maxence Lange <[email protected]>
9
 * @copyright 2017
10
 * @license GNU AGPL version 3 or any later version
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License as
14
 * published by the Free Software Foundation, either version 3 of the
15
 * License, or (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24
 *
25
 */
26
27
namespace OCA\CMSPico\Model;
28
29
use OCA\CMSPico\Service\MiscService;
30
31
class WebsiteCore implements \JsonSerializable {
32
33
	const TYPE_PUBLIC = 1;
34
	const TYPE_PRIVATE = 2;
35
36
	const SITE_LENGTH_MIN = 3;
37
	const NAME_LENGTH_MIN = 5;
38
39
40
	/** @var int */
41
	private $id;
42
43
	/** @var string */
44
	private $userId;
45
46
	/** @var string */
47
	private $name;
48
49
	/** @var string */
50
	private $theme = 'default';
51
52
	/** @var string */
53
	private $site;
54
55
	/** @var int */
56
	private $type = self::TYPE_PUBLIC;
57
58
	/** @var array */
59
	private $options = [];
60
61
	/** @var string */
62
	private $path;
63
64
	/** @var int */
65
	private $creation;
66
67
	/** @var string */
68
	private $viewer;
69
70
	/** @var string */
71
	private $templateSource;
72
73
74
	public function __construct($data = '') {
75
76
		if ($data === '') {
77
			return;
78
		}
79
80
		if (is_array($data)) {
81
			$this->fromArray($data);
82
83
			return;
84
		}
85
86
		$this->fromJSON($data);
87
	}
88
89
90
	/**
91
	 * @param int $id
92
	 *
93
	 * @return $this
94
	 */
95
	public function setId($id) {
96
		$this->id = (int)$id;
97
98
		return $this;
99
	}
100
101
	/**
102
	 * @return int
103
	 */
104
	public function getId() {
105
		return $this->id;
106
	}
107
108
109
	/**
110
	 * @param string $name
111
	 *
112
	 * @return $this
113
	 */
114
	public function setName($name) {
115
		$this->name = $name;
116
117
		return $this;
118
	}
119
120
	/**
121
	 * @return string
122
	 */
123
	public function getName() {
124
		return $this->name;
125
	}
126
127
128
	/**
129
	 * @param $theme
130
	 *
131
	 * @return $this
132
	 */
133
	public function setTheme($theme) {
134
		if ($theme === '') {
135
			return $this;
136
		}
137
138
		$this->theme = $theme;
139
140
		return $this;
141
	}
142
143
	/**
144
	 * @return string
145
	 */
146
	public function getTheme() {
147
		return $this->theme;
148
	}
149
150
	/**
151
	 * @param string $userId
152
	 *
153
	 * @return $this
154
	 */
155
	public function setUserId($userId) {
156
		$this->userId = $userId;
157
158
		return $this;
159
	}
160
161
	/**
162
	 * @return string
163
	 */
164
	public function getUserId() {
165
		return $this->userId;
166
	}
167
168
169
	/**
170
	 * @param string $site
171
	 *
172
	 * @return $this
173
	 */
174
	public function setSite($site) {
175
		$this->site = $site;
176
177
		return $this;
178
	}
179
180
	/**
181
	 * @return string
182
	 */
183
	public function getSite() {
184
		return $this->site;
185
	}
186
187
188
	/**
189
	 * @param int $type
190
	 *
191
	 * @return $this
192
	 */
193
	public function setType($type) {
194
		$this->type = $type;
195
196
		return $this;
197
	}
198
199
	/**
200
	 * @return int
201
	 */
202
	public function getType() {
203
		return $this->type;
204
	}
205
206
207
	/**
208
	 * @param string $key
209
	 * @param string $value
210
	 *
211
	 * @return $this
212
	 */
213
	public function setOption($key, $value) {
214
		$this->options[$key] = $value;
215
216
		return $this;
217
	}
218
219
	/**
220
	 * @param string $key
221
	 *
222
	 * @return string
223
	 */
224
	public function getOption($key) {
225
		if (!key_exists($key, $this->options)) {
226
			return '';
227
		}
228
229
		return (string)$this->options[$key];
230
	}
231
232
233
	/**
234
	 * @param array|string $options
235
	 *
236
	 * @return $this
237
	 */
238
	public function setOptions($options) {
239
		if (!is_array($options)) {
240
			$options = json_decode($options, true);
241
		}
242
243
		if ($options === null) {
244
			return $this;
245
		}
246
247
		$this->options = $options;
248
249
		return $this;
250
	}
251
252
253
	/**
254
	 * @param bool $json
255
	 *
256
	 * @return array
257
	 */
258
	public function getOptions($json = false) {
259
		if ($json === true) {
260
			return json_encode($this->options);
261
		}
262
263
		return $this->options;
264
	}
265
266
267
	/**
268
	 * @param string $path
269
	 *
270
	 * @return $this
271
	 */
272
	public function setPath($path) {
273
		$this->path = MiscService::endSlash($path);
274
275
		return $this;
276
	}
277
278
	/**
279
	 * @return string
280
	 */
281
	public function getPath() {
282
		return $this->path;
283
	}
284
285
286
	/**
287
	 * @param int $creation
288
	 *
289
	 * @return $this
290
	 */
291
	public function setCreation($creation) {
292
		$this->creation = $creation;
293
294
		return $this;
295
	}
296
297
	/**
298
	 * @return int
299
	 */
300
	public function getCreation() {
301
		return $this->creation;
302
	}
303
304
305
	/**
306
	 * @param string $viewer
307
	 *
308
	 * @return $this
309
	 */
310
	public function setViewer($viewer) {
311
		$this->viewer = $viewer;
312
313
		return $this;
314
	}
315
316
	/**
317
	 * @return string
318
	 */
319
	public function getViewer() {
320
		return $this->viewer;
321
	}
322
323
324
	/**
325
	 * @param string $source
326
	 *
327
	 * @return $this
328
	 */
329
	public function setTemplateSource($source) {
330
		$this->templateSource = $source;
331
332
		return $this;
333
	}
334
335
	/**
336
	 * @return string
337
	 */
338
	public function getTemplateSource() {
339
		return $this->templateSource;
340
	}
341
342
343
	/**
344
	 * @return array
345
	 */
346
	public function jsonSerialize() {
347
		return array(
348
			'id'       => $this->getId(),
349
			'name'     => $this->getName(),
350
			'user_id'  => $this->getUserId(),
351
			'site'     => $this->getSite(),
352
			'theme'    => $this->getTheme(),
353
			'type'     => $this->getType(),
354
			'options'  => $this->getOptions(),
355
			'path'     => $this->getPath(),
356
			'creation' => $this->getCreation()
357
		);
358
	}
359
360
361
	/**
362
	 * @param array $arr
363
	 *
364
	 * @return bool
365
	 */
366
	public function fromArray($arr) {
367
		if (!is_array($arr)) {
368
			return false;
369
		}
370
371
		MiscService::mustContains($arr, ['name', 'user_id', 'site', 'type', 'path']);
372
373
		$this->setId((int)MiscService::get($arr, 'id'))
374
			 ->setName($arr['name'])
375
			 ->setUserId($arr['user_id'])
376
			 ->setSite($arr['site'])
377
			 ->setTheme(MiscService::get($arr, 'theme'))
378
			 ->setType($arr['type'])
379
			 ->setOptions(MiscService::get($arr, 'options'))
380
			 ->setPath($arr['path'])
381
			 ->setCreation((int)MiscService::get($arr, 'creation'));
382
383
		return true;
384
	}
385
386
387
	/**
388
	 * @param string $json
389
	 */
390
	public function fromJSON($json) {
391
		$this->fromArray(json_decode($json, true));
392
	}
393
394
}