Completed
Push — master ( 221ac4...66e177 )
by Maxence
9s
created

Circle::jsonSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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