Passed
Push — master ( f7c149...eb684e )
by Maxence
02:42
created

WebsiteCore::getTemplateSource()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 2
rs 10
cc 1
eloc 1
nc 1
nop 0
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 (is_array($data)) {
77
			$this->fromArray($data);
78
79
			return;
80
		}
81
82
		$this->fromJSON($data);
83
	}
84
85
86
	/**
87
	 * @param int $id
88
	 *
89
	 * @return $this
90
	 */
91
	public function setId($id) {
92
		$this->id = (int)$id;
93
94
		return $this;
95
	}
96
97
	/**
98
	 * @return int
99
	 */
100
	public function getId() {
101
		return $this->id;
102
	}
103
104
105
	/**
106
	 * @param string $name
107
	 *
108
	 * @return $this
109
	 */
110
	public function setName($name) {
111
		$this->name = $name;
112
113
		return $this;
114
	}
115
116
	/**
117
	 * @return string
118
	 */
119
	public function getName() {
120
		return $this->name;
121
	}
122
123
124
	/**
125
	 * @param $theme
126
	 *
127
	 * @return $this
128
	 */
129
	public function setTheme($theme) {
130
		if ($theme === '') {
131
			return $this;
132
		}
133
134
		$this->theme = $theme;
135
136
		return $this;
137
	}
138
139
	/**
140
	 * @return string
141
	 */
142
	public function getTheme() {
143
		return $this->theme;
144
	}
145
146
	/**
147
	 * @param string $userId
148
	 *
149
	 * @return $this
150
	 */
151
	public function setUserId($userId) {
152
		$this->userId = $userId;
153
154
		return $this;
155
	}
156
157
	/**
158
	 * @return string
159
	 */
160
	public function getUserId() {
161
		return $this->userId;
162
	}
163
164
165
	/**
166
	 * @param string $site
167
	 *
168
	 * @return $this
169
	 */
170
	public function setSite($site) {
171
		$this->site = $site;
172
173
		return $this;
174
	}
175
176
	/**
177
	 * @return string
178
	 */
179
	public function getSite() {
180
		return $this->site;
181
	}
182
183
184
	/**
185
	 * @param int $type
186
	 *
187
	 * @return $this
188
	 */
189
	public function setType($type) {
190
		$this->type = $type;
191
192
		return $this;
193
	}
194
195
	/**
196
	 * @return int
197
	 */
198
	public function getType() {
199
		return $this->type;
200
	}
201
202
203
	/**
204
	 * @param string $key
205
	 * @param string $value
206
	 *
207
	 * @return $this
208
	 */
209
	public function setOption($key, $value) {
210
		$this->options[$key] = $value;
211
212
		return $this;
213
	}
214
215
	/**
216
	 * @param string $key
217
	 *
218
	 * @return string
219
	 */
220
	public function getOption($key) {
221
		if (!key_exists($key, $this->options)) {
222
			return '';
223
		}
224
225
		return (string)$this->options[$key];
226
	}
227
228
229
	/**
230
	 * @param array|string $options
231
	 *
232
	 * @return $this
233
	 */
234
	public function setOptions($options) {
235
		if (!is_array($options)) {
236
			$options = json_decode($options, true);
237
		}
238
239
		if ($options === null) {
240
			return $this;
241
		}
242
243
		$this->options = $options;
244
245
		return $this;
246
	}
247
248
249
	/**
250
	 * @param bool $json
251
	 *
252
	 * @return array
253
	 */
254
	public function getOptions($json = false) {
255
		if ($json === true) {
256
			return json_encode($this->options);
257
		}
258
259
		return $this->options;
260
	}
261
262
263
	/**
264
	 * @param string $path
265
	 *
266
	 * @return $this
267
	 */
268
	public function setPath($path) {
269
		$this->path = MiscService::endSlash($path);
270
271
		return $this;
272
	}
273
274
	/**
275
	 * @return string
276
	 */
277
	public function getPath() {
278
		return $this->path;
279
	}
280
281
282
	/**
283
	 * @param int $creation
284
	 *
285
	 * @return $this
286
	 */
287
	public function setCreation($creation) {
288
		$this->creation = $creation;
289
290
		return $this;
291
	}
292
293
	/**
294
	 * @return int
295
	 */
296
	public function getCreation() {
297
		return $this->creation;
298
	}
299
300
301
	/**
302
	 * @param string $viewer
303
	 *
304
	 * @return $this
305
	 */
306
	public function setViewer($viewer) {
307
		$this->viewer = $viewer;
308
309
		return $this;
310
	}
311
312
	/**
313
	 * @return string
314
	 */
315
	public function getViewer() {
316
		return $this->viewer;
317
	}
318
319
320
	/**
321
	 * @param string $source
322
	 *
323
	 * @return $this
324
	 */
325
	public function setTemplateSource($source) {
326
		$this->templateSource = $source;
327
328
		return $this;
329
	}
330
331
	/**
332
	 * @return string
333
	 */
334
	public function getTemplateSource() {
335
		return $this->templateSource;
336
	}
337
338
339
	/**
340
	 * @return array
341
	 */
342
	public function jsonSerialize() {
343
		return array(
344
			'id'       => $this->getId(),
345
			'name'     => $this->getName(),
346
			'user_id'  => $this->getUserId(),
347
			'site'     => $this->getSite(),
348
			'theme'    => $this->getTheme(),
349
			'type'     => $this->getType(),
350
			'options'  => $this->getOptions(),
351
			'path'     => $this->getPath(),
352
			'creation' => $this->getCreation()
353
		);
354
	}
355
356
357
	/**
358
	 * @param array $arr
359
	 *
360
	 * @return bool
361
	 */
362
	public function fromArray($arr) {
363
		if (!is_array($arr)) {
364
			return false;
365
		}
366
367
		MiscService::mustContains($arr, ['name', 'user_id', 'site', 'type', 'path']);
368
369
		$this->setId((int)MiscService::get($arr, 'id'))
370
			 ->setName($arr['name'])
371
			 ->setUserId($arr['user_id'])
372
			 ->setSite($arr['site'])
373
			 ->setTheme(MiscService::get($arr, 'theme'))
374
			 ->setType($arr['type'])
375
			 ->setOptions(MiscService::get($arr, 'options'))
376
			 ->setPath($arr['path'])
377
			 ->setCreation((int)MiscService::get($arr, 'creation'));
378
379
		return true;
380
	}
381
382
383
	/**
384
	 * @param string $json
385
	 */
386
	public function fromJSON($json) {
387
		$this->fromArray(json_decode($json, true));
388
	}
389
390
}