Passed
Push — master ( 275bab...ff10b0 )
by Maxence
03:14
created

WebsiteCore::getSite()   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 ($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
	public function setTheme($theme) {
132
		$this->theme = $theme;
133
	}
134
135
	/**
136
	 * @return string
137
	 */
138
	public function getTheme() {
139
		return $this->theme;
140
	}
141
142
	/**
143
	 * @param string $userId
144
	 *
145
	 * @return $this
146
	 */
147
	public function setUserId($userId) {
148
		$this->userId = $userId;
149
150
		return $this;
151
	}
152
153
	/**
154
	 * @return string
155
	 */
156
	public function getUserId() {
157
		return $this->userId;
158
	}
159
160
161
	/**
162
	 * @param string $site
163
	 *
164
	 * @return $this
165
	 */
166
	public function setSite($site) {
167
		$this->site = $site;
168
169
		return $this;
170
	}
171
172
	/**
173
	 * @return string
174
	 */
175
	public function getSite() {
176
		return $this->site;
177
	}
178
179
180
	/**
181
	 * @param int $type
182
	 *
183
	 * @return $this
184
	 */
185
	public function setType($type) {
186
		$this->type = $type;
187
188
		return $this;
189
	}
190
191
	/**
192
	 * @return int
193
	 */
194
	public function getType() {
195
		return $this->type;
196
	}
197
198
199
	/**
200
	 * @param string $key
201
	 * @param string $value
202
	 */
203
	public function setOption($key, $value) {
204
		$this->options[$key] = $value;
205
	}
206
207
208
	/**
209
	 * @param string $key
210
	 *
211
	 * @return string
212
	 */
213
	public function getOption($key) {
214
		if (!key_exists($key, $this->options)) {
215
			return '';
216
		}
217
218
		return (string)$this->options[$key];
219
	}
220
221
222
	/**
223
	 * @param array|string $options
224
	 *
225
	 * @return $this
226
	 */
227
	public function setOptions($options) {
228
		if (!is_array($options)) {
229
			$options = json_decode($options, true);
230
		}
231
232
		if ($options === null) {
233
			return $this;
234
		}
235
236
		$this->options = $options;
237
238
		return $this;
239
	}
240
241
242
	/**
243
	 * @param bool $json
244
	 *
245
	 * @return array
246
	 */
247
	public function getOptions($json = false) {
248
		if ($json === true) {
249
			return json_encode($this->options);
250
		}
251
252
		return $this->options;
253
	}
254
255
256
	/**
257
	 * @param string $path
258
	 *
259
	 * @return $this
260
	 */
261
	public function setPath($path) {
262
		MiscService::endSlash($path);
263
		$this->path = $path;
264
265
		return $this;
266
	}
267
268
	/**
269
	 * @return string
270
	 */
271
	public function getPath() {
272
		return $this->path;
273
	}
274
275
276
	/**
277
	 * @param int $creation
278
	 *
279
	 * @return $this
280
	 */
281
	public function setCreation($creation) {
282
		if ($creation === null) {
283
			return $this;
284
		}
285
286
		$this->creation = $creation;
287
288
		return $this;
289
	}
290
291
	/**
292
	 * @return int
293
	 */
294
	public function getCreation() {
295
		return $this->creation;
296
	}
297
298
299
	/**
300
	 * @param string $viewer
301
	 */
302
	public function setViewer($viewer) {
303
		$this->viewer = $viewer;
304
	}
305
306
	/**
307
	 * @return string
308
	 */
309
	public function getViewer() {
310
		return $this->viewer;
311
	}
312
313
314
	/**
315
	 * @param string $source
316
	 *
317
	 * @return $this
318
	 */
319
	public function setTemplateSource($source) {
320
		$this->templateSource = $source;
321
322
		return $this;
323
	}
324
325
	/**
326
	 * @return string
327
	 */
328
	public function getTemplateSource() {
329
		return $this->templateSource;
330
	}
331
332
333
	/**
334
	 * @return array
335
	 */
336
	public function jsonSerialize() {
337
		return array(
338
			'id'       => $this->getId(),
339
			'name'     => $this->getName(),
340
			'user_id'  => $this->getUserId(),
341
			'site'     => $this->getSite(),
342
			'type'     => $this->getType(),
343
			'options'  => $this->getOptions(),
344
			'path'     => $this->getPath(),
345
			'creation' => $this->getCreation()
346
		);
347
	}
348
349
350
	/**
351
	 * @param array $arr
352
	 */
353
	public function fromArray($arr) {
354
		if (!is_array($arr)) {
355
			return;
356
		}
357
358
		MiscService::mustContains($arr, ['name', 'user_id', 'site', 'type', 'path']);
359
360
		$this->setId(MiscService::get($arr, 'id'))
0 ignored issues
show
Bug introduced by
OCA\CMSPico\Service\MiscService::get($arr, 'id') of type string|array is incompatible with the type integer expected by parameter $id of OCA\CMSPico\Model\WebsiteCore::setId(). ( Ignorable by Annotation )

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

360
		$this->setId(/** @scrutinizer ignore-type */ MiscService::get($arr, 'id'))
Loading history...
361
			 ->setName($arr['name'])
362
			 ->setUserId($arr['user_id'])
363
			 ->setSite($arr['site'])
364
			 ->setType($arr['type'])
365
			 ->setOptions(MiscService::get($arr, 'options'))
366
			 ->setPath($arr['path'])
367
			 ->setCreation(MiscService::get($arr, 'creation'));
0 ignored issues
show
Bug introduced by
OCA\CMSPico\Service\Misc...::get($arr, 'creation') of type string|array is incompatible with the type integer expected by parameter $creation of OCA\CMSPico\Model\WebsiteCore::setCreation(). ( Ignorable by Annotation )

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

367
			 ->setCreation(/** @scrutinizer ignore-type */ MiscService::get($arr, 'creation'));
Loading history...
368
	}
369
370
371
	/**
372
	 * @param string $json
373
	 */
374
	public function fromJSON($json) {
375
		if (!is_string($json)) {
376
			return;
377
		}
378
		$this->fromArray(json_decode($json, true));
379
	}
380
381
}