Completed
Pull Request — master (#347)
by Maxence
02:22
created

DavCard::getCircles()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
4
/**
5
 * Circles - Bring cloud-users closer together.
6
 *
7
 * This file is licensed under the Affero General Public License version 3 or
8
 * later. See the COPYING file.
9
 *
10
 * @author Maxence Lange <[email protected]>
11
 * @copyright 2017
12
 * @license GNU AGPL version 3 or any later version
13
 *
14
 * This program is free software: you can redistribute it and/or modify
15
 * it under the terms of the GNU Affero General Public License as
16
 * published by the Free Software Foundation, either version 3 of the
17
 * License, or (at your option) any later version.
18
 *
19
 * This program is distributed in the hope that it will be useful,
20
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 * GNU Affero General Public License for more details.
23
 *
24
 * You should have received a copy of the GNU Affero General Public License
25
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
26
 *
27
 */
28
29
30
namespace OCA\Circles\Model;
31
32
33
use daita\MySmallPhpTools\Traits\TArrayTools;
34
use JsonSerializable;
35
36
37
/**
38
 * Class DavCard
39
 *
40
 * @package OCA\Circles\Model
41
 */
42
class DavCard implements JsonSerializable {
43
44
45
	use TArrayTools;
46
47
48
	/** @var int */
49
	private $addressBookId = 0;
50
51
	/** @var string */
52
	private $cardUri = '';
53
54
	/** @var string */
55
	private $contactId = '';
56
57
	/** @var string */
58
	private $fn = '';
59
60
	/** @var array */
61
	private $emails = [];
62
63
	/** @var array */
64
	private $groups = [];
65
66
	/** @var Circle[] */
67
	private $circles = [];
68
69
70
	public function __construct() {
71
	}
72
73
74
	/**
75
	 * @return int
76
	 */
77
	public function getAddressBookId(): int {
78
		return $this->addressBookId;
79
	}
80
81
	/**
82
	 * @param int $addressBookId
83
	 *
84
	 * @return DavCard
85
	 */
86
	public function setAddressBookId(int $addressBookId): self {
87
		$this->addressBookId = $addressBookId;
88
89
		return $this;
90
	}
91
92
93
	/**
94
	 * @return string
95
	 */
96
	public function getCardUri(): string {
97
		return $this->cardUri;
98
	}
99
100
	/**
101
	 * @param string $cardUri
102
	 *
103
	 * @return DavCard
104
	 */
105
	public function setCardUri(string $cardUri): self {
106
		$this->cardUri = $cardUri;
107
108
		return $this;
109
	}
110
111
112
	/**
113
	 * @return string
114
	 */
115
	public function getContactId(): string {
116
		return $this->contactId;
117
	}
118
119
	/**
120
	 * @param string $contactId
121
	 *
122
	 * @return DavCard
123
	 */
124
	public function setContactId(string $contactId): self {
125
		$this->contactId = $contactId;
126
127
		return $this;
128
	}
129
130
131
	/**
132
	 * @return string
133
	 */
134
	public function getFn(): string {
135
		return $this->fn;
136
	}
137
138
	/**
139
	 * @param string $fn
140
	 *
141
	 * @return DavCard
142
	 */
143
	public function setFn(string $fn): self {
144
		$this->fn = $fn;
145
146
		return $this;
147
	}
148
149
150
	/**
151
	 * @return array
152
	 */
153
	public function getEmails(): array {
154
		return $this->emails;
155
	}
156
157
	/**
158
	 * @param array $emails
159
	 *
160
	 * @return DavCard
161
	 */
162
	public function setEmails(array $emails): self {
163
		$this->emails = $emails;
164
165
		return $this;
166
	}
167
168
169
	/**
170
	 * @return array
171
	 */
172
	public function getGroups(): array {
173
		return $this->groups;
174
	}
175
176
	/**
177
	 * @param array $groups
178
	 *
179
	 * @return DavCard
180
	 */
181
	public function setGroups(array $groups): self {
182
		$this->groups = $groups;
183
184
		return $this;
185
	}
186
187
188
	/**
189
	 * @return Circle[]
190
	 */
191
	public function getCircles(): array {
192
		return $this->circles;
193
	}
194
195
	/**
196
	 * @param array $circles
197
	 *
198
	 * @return DavCard
199
	 */
200
	public function setCircles(array $circles): self {
201
		$this->circles = $circles;
202
203
		return $this;
204
	}
205
206
	/**
207
	 * @param Circle $circle
208
	 *
209
	 * @return $this
210
	 */
211
	public function addCircle(Circle $circle): self {
212
		$this->circles[] = $circle;
213
214
		return $this;
215
	}
216
217
218
	/**
219
	 * @param array $data
220
	 */
221
	public function import(array $data) {
222
		$this->setAddressBookId($this->get('addressBookId', $data));
223
		$this->setCardUri($this->get('cardUri', $data));
224
		$this->setContactId($this->get('uid', $data));
225
		$this->setFn($this->get('fn', $data));
226
		$this->setEmails($this->getArray('emails', $data));
227
		$this->setGroups($this->getArray('groups', $data));
228
	}
229
230
231
	/**
232
	 * @param string $dav
233
	 */
234
	public function importFromDav(string $dav) {
235
		$data = $this->parseDav($dav);
236
237
		$this->setContactId($this->get('UID', $data));
238
		$this->setFn($this->get('FN', $data));
239
		$this->setEmails($this->getArray('EMAILS', $data));
240
		$this->setGroups($this->getArray('CATEGORIES', $data));
241
	}
242
243
244
	/**
245
	 * get essential data from the dav content
246
	 * (also don't think we need regex)
247
	 *
248
	 * @param string $dav
249
	 *
250
	 * @return array
251
	 */
252
	private function parseDav(string $dav): array {
253
		$result = [
254
			'UID'        => '',
255
			'FN'         => '',
256
			'EMAILS'     => [],
257
			'CATEGORIES' => []
258
		];
259
260
		$data = preg_split('/\R/', $dav);
261
		foreach ($data as $entry) {
262
			if (trim($entry) === '' || strpos($entry, ':') === false) {
263
				continue;
264
			}
265
			list($k, $v) = explode(':', $entry, 2);
266
267
			$k = strtoupper($k);
268
			if (strpos($entry, ';') !== false) {
269
				list($k) = explode(';', $entry, 2);
270
			}
271
272
			switch ($k) {
273
				case 'UID':
274
				case 'FN':
275
					$result[$k] = $v;
276
					break;
277
278
				case 'EMAIL':
279
					$result['EMAILS'][] = $v;
280
					break;
281
282
				case 'CATEGORIES':
283
					if (strpos($v, ',') === false) {
284
						$result['CATEGORIES'] = $v;
285
					} else {
286
						$result['CATEGORIES'] = explode(',', $v);
287
					}
288
					break;
289
			}
290
		}
291
292
		return $result;
293
294
	}
295
296
297
	/**
298
	 * @return array
299
	 */
300
	public function jsonSerialize(): array {
301
		return [
302
			'addressBookId' => $this->getAddressBookId(),
303
			'cardUri'       => $this->getCardUri(),
304
			'uid'           => $this->getContactId(),
305
			'fn'            => $this->getFn(),
306
			'emails'        => $this->getEmails(),
307
			'groups'        => $this->getGroups()
308
		];
309
	}
310
311
}
312