Passed
Push — master ( 47b269...8df878 )
by Maxence
04:35
created

WebsiteCore::setSite()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

388
			 ->setPage(/** @scrutinizer ignore-type */ MiscService::get($arr, 'page'))
Loading history...
389
			 ->setTheme(MiscService::get($arr, 'theme', 'default'))
390
			 ->setType($arr['type'])
391
			 ->setOptions(MiscService::get($arr, 'options'))
392
			 ->setPath($arr['path'])
393
			 ->setCreation((int)MiscService::get($arr, 'creation'));
394
395
		return true;
396
	}
397
398
399
	/**
400
	 * @param string $json
401
	 */
402
	public function fromJSON($json) {
403
		$this->fromArray(json_decode($json, true));
404
	}
405
406
}