Completed
Push — testdrone ( ce59c4...d70b76 )
by Maxence
62:33
created

Circle::fromArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 9

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 9
nc 1
nop 1
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
	/** @var string */
39
	private $typeString;
40
41
	/** @var string */
42
	private $typeLongString;
43
44
	public function setType($type) {
45
		parent::setType($type);
46
		$this->setTypeString(self::TypeString($type));
47
		$this->setTypeLongString(self::TypeLongString($type));
48
		$this->setInfo($this->getTypeLongString());
49
50
		return $this;
51
	}
52
53
	public function setTypeString($str) {
54
		$this->typeString = $str;
55
56
		return $this;
57
	}
58
59
	public function getTypeString() {
60
		return $this->typeString;
61
	}
62
63
	public function setTypeLongString($str) {
64
		$this->typeLongString = $str;
65
	}
66
67
	public function getTypeLongString() {
68
		return $this->typeLongString;
69
	}
70
71
72
	public function jsonSerialize() {
73
		return array(
74
			'id'          => $this->getId(),
75
			'name'        => $this->getName(),
76
			'owner'       => $this->getOwner(),
77
			'user'        => $this->getUser(),
78
			'description' => $this->getDescription(),
79
			'type'        => $this->getTypeString(),
80
			'creation'    => $this->getCreation(),
81
			'members'     => $this->getMembers()
82
		);
83
	}
84
85
	/**
86
	 * set all infos from an Array.
87
	 *
88
	 * @param $arr
89
	 *
90
	 * @return $this
91
	 */
92
	public function fromArray($arr) {
93
		$this->setId($arr['id']);
94
		$this->setName($arr['name']);
95
		$this->setDescription($arr['description']);
96
		$this->setType($arr['type']);
97
		$this->setCreation($arr['creation']);
98
99
		$this->setOwnerMemberFromArray($arr);
100
		$this->setUserMemberFromArray($arr);
101
102
		return $this;
103
	}
104
105
106
	/**
107
	 * set Owner Infos from Array
108
	 *
109
	 * @param $array
110
	 */
111
	private function setOwnerMemberFromArray($array) {
112
		if (key_exists('owner', $array)) {
113
			$owner = new Member();
114
			$owner->setUserId($array['owner']);
115
			$this->setOwner($owner);
116
		}
117
	}
118
119
120
	/**
121
	 * set User Infos from Array
122
	 *
123
	 * @param $array
124
	 */
125
	private function setUserMemberFromArray($array) {
126
		if (key_exists('status', $array)
127
			&& key_exists('level', $array)
128
			&& key_exists('joined', $array)
129
		) {
130
			$user = new Member();
131
			$user->setStatus($array['status']);
132
			$user->setLevel($array['level']);
133
			$user->setJoined($array['joined']);
134
			$this->setUser($user);
135
		}
136
	}
137
138
139
	public static function TypeString($type) {
0 ignored issues
show
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
140
		switch ($type) {
141
			case self::CIRCLES_PERSONAL:
142
				return 'Personal';
143
			case self::CIRCLES_HIDDEN:
144
				return 'Hidden';
145
			case self::CIRCLES_PRIVATE:
146
				return 'Private';
147
			case self::CIRCLES_PUBLIC:
148
				return 'Public';
149
			case self::CIRCLES_ALL:
150
				return 'All';
151
		}
152
153
		return 'none';
154
	}
155
156
	/**
157
	 * @param $type
158
	 *
159
	 * @return string
160
	 */
161
	public static function TypeLongString($type) {
0 ignored issues
show
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
162
		switch ($type) {
163
			case self::CIRCLES_PERSONAL:
164
				return 'Personal Circle';
165
			case self::CIRCLES_HIDDEN:
166
				return 'Hidden Circle';
167
			case self::CIRCLES_PRIVATE:
168
				return 'Private Circle';
169
			case self::CIRCLES_PUBLIC:
170
				return 'Public Circle';
171
			case self::CIRCLES_ALL:
172
				return 'All Circles';
173
		}
174
175
		return 'none';
176
	}
177
178
}
179
180
181