Passed
Push — master ( 8460a0...9ca045 )
by Maxence
02:13
created

WebsiteCore::getOptions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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