Completed
Push — master ( 6ce943...2d2289 )
by Maxence
02:19
created

Share::setSharer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 2
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 Share implements \JsonSerializable {
30
31
	private $source;
32
	private $type;
33
34
	/** @var int */
35
	private $circleId;
36
37
	/** @var string */
38
	private $circleName;
39
40
	/** @var string */
41
	private $author;
42
43
	/** @var string */
44
	private $sharer;
45
46
	/** @var array */
47
	private $item;
48
49
	/** @var int */
50
	private $creation;
51
52
	public function __construct(string $source, string $type) {
53
		$this->source = $source;
54
		$this->type = $type;
55
	}
56
57
58
	/**
59
	 * @return string
60
	 */
61
	public function getSource() {
62
		return $this->source;
63
	}
64
65
	/**
66
	 * @return string
67
	 */
68
	public function getType() {
69
		return $this->type;
70
	}
71
72
	/**
73
	 * @param int $circleId
74
	 */
75
	public function setCircleId(int $circleId) {
76
		$this->circleId = $circleId;
77
	}
78
79
	/**
80
	 * @return int
81
	 */
82
	public function getCircleId() {
83
		return $this->circleId;
84
	}
85
86
87
	/**
88
	 * @param string $circleName
89
	 */
90
	public function setCircleName(string $circleName) {
91
		$this->circleName = $circleName;
92
	}
93
94
	/**
95
	 * @return mixed
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use string.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
96
	 */
97
	public function getCircleName() {
98
		return $this->circleName;
99
	}
100
101
102
	/**
103
	 * @param string $author
104
	 */
105
	public function setAuthor(string $author) {
106
		$this->author = $author;
107
108
		if ($this->getSharer() === null) {
109
			$this->setSharer($author);
110
		}
111
	}
112
113
	/**
114
	 * @return string
115
	 */
116
	public function getAuthor() {
117
		return $this->author;
118
	}
119
120
121
	/**
122
	 * @param string $sharer
123
	 */
124
	public function setSharer(string $sharer) {
125
		$this->sharer = $sharer;
126
	}
127
128
	/**
129
	 * @return string
130
	 */
131
	public function getSharer() {
132
		return $this->sharer;
133
	}
134
135
136
	/**
137
	 * @param array $item
138
	 */
139
	public function setItem(array $item) {
140
		$this->item = $item;
141
	}
142
143
	/**
144
	 * @param bool $asJson
145
	 *
146
	 * @return array|string
147
	 */
148
	public function getItem(bool $asJson = false) {
149
		if ($asJson) {
150
			return json_encode($this->item);
151
		}
152
153
		return $this->item;
154
	}
155
156
157
	/**
158
	 * @param int $creation
159
	 */
160
	public function setCreation($creation) {
161
		if ($creation === null) {
162
			return;
163
		}
164
165
		$this->creation = $creation;
166
	}
167
168
	/**
169
	 * @return int
170
	 */
171
	public function getCreation() {
172
		return $this->creation;
173
	}
174
175
176
	public function jsonSerialize() {
177
		return array(
178
			'circle_id'   => $this->getCircleId(),
179
			'circle_name' => $this->getCircleName(),
180
			'source'      => $this->getSource(),
181
			'type'        => $this->getType(),
182
			'author'      => $this->getAuthor(),
183
			'sharer'      => $this->getSharer(),
184
			'item'        => $this->getItem(),
185
			'creation'    => $this->getCreation(),
186
		);
187
	}
188
189
	public static function fromJSON($json) {
190
191
		$arr = json_decode($json, true);
192
193
		$share = new Share($arr['source'], $arr['type']);
194
		$share->setCircleId($arr['circle_id']);
195
		if (key_exists('circle_name', $arr)) {
196
			$share->setCircleName($arr['circle_name']);
197
		}
198
199
		$share->setAuthor($arr['author']);
200
		$share->setSharer($arr['sharer']);
201
		$share->setItem($arr['item']);
202
		$share->setCreation($arr['creation']);
203
204
		return $share;
205
	}
206
207
}