Completed
Pull Request — master (#55)
by Maxence
02:47
created

Share::getSource()   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 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 Share implements \JsonSerializable {
30
31
	private $source;
32
	private $type;
33
34
	/** @var int */
35
	private $circleId;
36
37
	/** @var string */
38
	private $author;
39
40
	/** @var string */
41
	private $sharer;
42
43
	/** @var array */
44
	private $item;
45
46
	/** @var int */
47
	private $creation;
48
49
	public function __construct(string $source, string $type) {
50
		$this->source = $source;
51
		$this->type = $type;
52
	}
53
54
55
	/**
56
	 * @return string
57
	 */
58
	public function getSource() {
59
		return $this->source;
60
	}
61
62
	/**
63
	 * @return string
64
	 */
65
	public function getType() {
66
		return $this->type;
67
	}
68
69
	/**
70
	 * @param int $circleId
71
	 */
72
	public function setCircleId(int $circleId) {
73
		$this->circleId = $circleId;
74
	}
75
76
	/**
77
	 * @return int
78
	 */
79
	public function getCircleId() {
80
		return $this->circleId;
81
	}
82
83
84
	/**
85
	 * @param string $author
86
	 */
87
	public function setAuthor(string $author) {
88
		$this->author = $author;
89
90
		if ($this->getSharer() === null) {
91
			$this->setSharer($author);
92
		}
93
	}
94
95
	/**
96
	 * @return string
97
	 */
98
	public function getAuthor() {
99
		return $this->author;
100
	}
101
102
103
	/**
104
	 * @param string $sharer
105
	 */
106
	public function setSharer(string $sharer) {
107
		$this->sharer = $sharer;
108
	}
109
110
	/**
111
	 * @return string
112
	 */
113
	public function getSharer() {
114
		return $this->sharer;
115
	}
116
117
118
	/**
119
	 * @param array $item
120
	 */
121
	public function setItem(array $item) {
122
		$this->item = $item;
123
	}
124
125
	/**
126
	 * @param bool $asJson
127
	 *
128
	 * @return array|string
129
	 */
130
	public function getItem(bool $asJson = false) {
131
		if ($asJson) {
132
			return json_encode($this->item);
133
		}
134
135
		return $this->item;
136
	}
137
138
139
	/**
140
	 * @param int $creation
141
	 */
142
	public function setCreation($creation) {
143
		if ($creation === null) {
144
			return;
145
		}
146
147
		$this->creation = $creation;
148
	}
149
150
	/**
151
	 * @return int
152
	 */
153
	public function getCreation() {
154
		return $this->creation;
155
	}
156
157
158
	public function jsonSerialize() {
159
		return array(
160
			'circle_id' => $this->getCircleId(),
161
			'source'    => $this->getSource(),
162
			'type'      => $this->getType(),
163
			'author'    => $this->getAuthor(),
164
			'sharer'    => $this->getSharer(),
165
			'item'      => $this->getItem(),
166
			'creation'  => $this->getCreation(),
167
		);
168
	}
169
170
	public static function fromJSON($json) {
171
172
		$arr = json_decode($json, true);
173
174
		$share = new Share($arr['source'], $arr['type']);
175
		$share->setCircleId($arr['circle_id']);
176
177
		$share->setAuthor($arr['author']);
178
		$share->setSharer($arr['sharer']);
179
		$share->setItem($arr['item']);
180
		$share->setCreation($arr['creation']);
181
182
		return $share;
183
	}
184
185
}