Completed
Pull Request — master (#71)
by Maxence
09:26 queued 05:54
created

Circle::jsonSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 13
nc 1
nop 0
1
<?php
2
/**
3
 * Circles - Bring cloud-users closer together.
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\Circles\Model;
28
29
use OCA\Circles\Exceptions\CircleTypeNotValid;
30
31
class Circle extends BaseCircle implements \JsonSerializable {
32
33
	const CIRCLES_PERSONAL = 1;
34
	const CIRCLES_HIDDEN = 2;
35
	const CIRCLES_PRIVATE = 4;
36
	const CIRCLES_PUBLIC = 8;
37
38
	const CIRCLES_ALL = 15;
39
40
	public function getTypeString() {
41
		switch ($this->getType()) {
42
			case self::CIRCLES_PERSONAL:
43
				return 'Personal';
44
			case self::CIRCLES_HIDDEN:
45
				return 'Hidden';
46
			case self::CIRCLES_PRIVATE:
47
				return 'Private';
48
			case self::CIRCLES_PUBLIC:
49
				return 'Public';
50
			case self::CIRCLES_ALL:
51
				return 'All';
52
		}
53
54
		return 'none';
55
	}
56
57
	public function getTypeLongString() {
58
		return self::typeLongString($this->getType());
59
	}
60
61
62
	public function getInfo() {
63
		return $this->getTypeLongString();
64
	}
65
66
67
	public function jsonSerialize() {
68
		return array(
69
			'id'             => $this->getId(),
70
			'name'           => $this->getName(),
71
			'owner'          => $this->getOwner(),
72
			'user'           => $this->getUser(),
73
			'description'    => $this->getDescription(),
74
			'type'           => $this->getTypeString(),
75
			'creation'       => $this->getCreation(),
76
			'typeString'     => $this->getTypeString(),
77
			'typeLongString' => $this->getTypeLongString(),
78
			'members'        => $this->getMembers(),
79
			'links'          => $this->getRemote()
80
		);
81
	}
82
83
	/**
84
	 * set all infos from an Array.
85
	 *
86
	 * @param $arr
87
	 *
88
	 * @return $this
89
	 */
90
	public function fromArray($arr) {
91
		$this->setId($arr['id']);
92
		$this->setName($arr['name']);
93
		$this->setUniqueId($arr['unique_id']);
94
		$this->setDescription($arr['description']);
95
		$this->setType($arr['type']);
96
		$this->setCreation($arr['creation']);
97
98
		$this->setOwnerMemberFromArray($arr);
99
		$this->setUserMemberFromArray($arr);
100
101
		return $this;
102
	}
103
104
105
	/**
106
	 * set Owner Infos from Array
107
	 *
108
	 * @param $array
109
	 */
110
	private function setOwnerMemberFromArray(& $array) {
111
		if (key_exists('owner', $array)) {
112
			$owner = new Member($this->l10n);
113
			$owner->setUserId($array['owner']);
114
			$this->setOwner($owner);
115
		}
116
	}
117
118
119
	/**
120
	 * set User Infos from Array
121
	 *
122
	 * @param $array
123
	 */
124
	private function setUserMemberFromArray(& $array) {
125
		if (key_exists('status', $array)
126
			&& key_exists('level', $array)
127
			&& key_exists('joined', $array)
128
		) {
129
			$user = new Member($this->l10n);
130
			$user->setStatus($array['status']);
131
			$user->setLevel($array['level']);
132
			$user->setJoined($array['joined']);
133
			$this->setUser($user);
134
		}
135
	}
136
137
138
	/**
139
	 * @throws CircleTypeNotValid
140
	 */
141
	public function cantBePersonal() {
142
		if ($this->getType() === self::CIRCLES_PERSONAL) {
143
			throw new CircleTypeNotValid(
144
				$this->l10n->t("This option is not available to personal circles")
145
			);
146
		}
147
	}
148
149
150
	/**
151
	 * @param $type
152
	 *
153
	 * @return string
154
	 */
155
	public static function typeLongString($type) {
156
		switch ($type) {
157
			case self::CIRCLES_PERSONAL:
158
				return 'Personal circle';
159
			case self::CIRCLES_HIDDEN:
160
				return 'Hidden circle';
161
			case self::CIRCLES_PRIVATE:
162
				return 'Private circle';
163
			case self::CIRCLES_PUBLIC:
164
				return 'Public circle';
165
			case self::CIRCLES_ALL:
166
				return 'All circles';
167
		}
168
169
		return 'none';
170
	}
171
172
}
173
174
175