Passed
Push — master ( 3d777c...464a7d )
by Maxence
02:04
created

WebsiteCore::getOption()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 3
nc 2
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
		$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
		MiscService::endSlash($path);
270
		$this->path = $path;
271
272
		return $this;
273
	}
274
275
	/**
276
	 * @return string
277
	 */
278
	public function getPath() {
279
		return $this->path;
280
	}
281
282
283
	/**
284
	 * @param int $creation
285
	 *
286
	 * @return $this
287
	 */
288
	public function setCreation($creation) {
289
		$this->creation = $creation;
290
291
		return $this;
292
	}
293
294
	/**
295
	 * @return int
296
	 */
297
	public function getCreation() {
298
		return $this->creation;
299
	}
300
301
302
	/**
303
	 * @param string $viewer
304
	 *
305
	 * @return $this
306
	 */
307
	public function setViewer($viewer) {
308
		$this->viewer = $viewer;
309
310
		return $this;
311
	}
312
313
	/**
314
	 * @return string
315
	 */
316
	public function getViewer() {
317
		return $this->viewer;
318
	}
319
320
321
	/**
322
	 * @param string $source
323
	 *
324
	 * @return $this
325
	 */
326
	public function setTemplateSource($source) {
327
		$this->templateSource = $source;
328
329
		return $this;
330
	}
331
332
	/**
333
	 * @return string
334
	 */
335
	public function getTemplateSource() {
336
		return $this->templateSource;
337
	}
338
339
340
	/**
341
	 * @return array
342
	 */
343
	public function jsonSerialize() {
344
		return array(
345
			'id'       => $this->getId(),
346
			'name'     => $this->getName(),
347
			'user_id'  => $this->getUserId(),
348
			'site'     => $this->getSite(),
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(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

369
		$this->setId(/** @scrutinizer ignore-type */ MiscService::get($arr, 'id'))
Loading history...
370
			 ->setName($arr['name'])
371
			 ->setUserId($arr['user_id'])
372
			 ->setSite($arr['site'])
373
			 ->setType($arr['type'])
374
			 ->setOptions(MiscService::get($arr, 'options'))
375
			 ->setPath($arr['path'])
376
			 ->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

376
			 ->setCreation(/** @scrutinizer ignore-type */ MiscService::get($arr, 'creation'));
Loading history...
377
378
		return true;
379
	}
380
381
382
	/**
383
	 * @param string $json
384
	 */
385
	public function fromJSON($json) {
386
		$this->fromArray(json_decode($json, true));
387
	}
388
389
}