Completed
Pull Request — master (#347)
by Maxence
01:49
created

DavCard::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 2
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
	const TYPE_CONTACT = 1;
49
	const TYPE_LOCAL = 2;
50
//	const TYPE_FEDERATED = 3;
51
52
53
	/** @var int */
54
	private $addressBookId = 0;
55
56
	/** @var string */
57
	private $owner = '';
58
59
	/** @var string */
60
	private $cardUri = '';
61
62
	/** @var string */
63
	private $contactId = '';
64
65
	/** @var string */
66
	private $fn = '';
67
68
	/** @var array */
69
	private $emails = [];
70
71
	/** @var array */
72
	private $clouds = [];
73
74
	/** @var array */
75
	private $groups = [];
76
77
	/** @var Circle[] */
78
	private $circles = [];
79
80
	/** @var string */
81
	private $userId = '';
82
83
84
	public function __construct() {
85
	}
86
87
88
	/**
89
	 * @return int
90
	 */
91
	public function getAddressBookId(): int {
92
		return $this->addressBookId;
93
	}
94
95
	/**
96
	 * @param int $addressBookId
97
	 *
98
	 * @return DavCard
99
	 */
100
	public function setAddressBookId(int $addressBookId): self {
101
		$this->addressBookId = $addressBookId;
102
103
		return $this;
104
	}
105
106
107
	/**
108
	 * @return string
109
	 */
110
	public function getOwner(): string {
111
		return $this->owner;
112
	}
113
114
	/**
115
	 * @param string $owner
116
	 *
117
	 * @return DavCard
118
	 */
119
	public function setOwner(string $owner): self {
120
		$this->owner = $owner;
121
122
		return $this;
123
	}
124
125
126
	/**
127
	 * @return string
128
	 */
129
	public function getCardUri(): string {
130
		return $this->cardUri;
131
	}
132
133
	/**
134
	 * @param string $cardUri
135
	 *
136
	 * @return DavCard
137
	 */
138
	public function setCardUri(string $cardUri): self {
139
		$this->cardUri = $cardUri;
140
141
		return $this;
142
	}
143
144
145
	/**
146
	 * @return string
147
	 */
148
	public function getContactId(): string {
149
		return $this->contactId;
150
	}
151
152
	/**
153
	 * @param string $contactId
154
	 *
155
	 * @return DavCard
156
	 */
157
	public function setContactId(string $contactId): self {
158
		$this->contactId = $contactId;
159
160
		return $this;
161
	}
162
163
164
	/**
165
	 * @return string
166
	 */
167
	public function getFn(): string {
168
		return $this->fn;
169
	}
170
171
	/**
172
	 * @param string $fn
173
	 *
174
	 * @return DavCard
175
	 */
176
	public function setFn(string $fn): self {
177
		$this->fn = $fn;
178
179
		return $this;
180
	}
181
182
183
	/**
184
	 * @return array
185
	 */
186
	public function getEmails(): array {
187
		return $this->emails;
188
	}
189
190
	/**
191
	 * @param array $emails
192
	 *
193
	 * @return DavCard
194
	 */
195
	public function setEmails(array $emails): self {
196
		$this->emails = $emails;
197
198
		return $this;
199
	}
200
201
202
	/**
203
	 * @return array
204
	 */
205
	public function getClouds(): array {
206
		return $this->clouds;
207
	}
208
209
	/**
210
	 * @param array $clouds
211
	 *
212
	 * @return DavCard
213
	 */
214
	public function setClouds(array $clouds): self {
215
		$this->clouds = $clouds;
216
217
		return $this;
218
	}
219
220
221
	/**
222
	 * @return array
223
	 */
224
	public function getGroups(): array {
225
		return $this->groups;
226
	}
227
228
	/**
229
	 * @param array $groups
230
	 *
231
	 * @return DavCard
232
	 */
233
	public function setGroups(array $groups): self {
234
		$this->groups = $groups;
235
236
		return $this;
237
	}
238
239
240
	/**
241
	 * @return Circle[]
242
	 */
243
	public function getCircles(): array {
244
		return $this->circles;
245
	}
246
247
	/**
248
	 * @param array $circles
249
	 *
250
	 * @return DavCard
251
	 */
252
	public function setCircles(array $circles): self {
253
		$this->circles = $circles;
254
255
		return $this;
256
	}
257
258
259
	/**
260
	 * @return string
261
	 */
262
	public function getUserId(): string {
263
		return $this->userId;
264
	}
265
266
	/**
267
	 * @param string $userId
268
	 *
269
	 * @return DavCard
270
	 */
271
	public function setUserId(string $userId): self {
272
		$this->userId = $userId;
273
274
		return $this;
275
	}
276
277
278
	/**
279
	 * @param Circle $circle
280
	 *
281
	 * @return $this
282
	 */
283
	public function addCircle(Circle $circle): self {
284
		$this->circles[] = $circle;
285
286
		return $this;
287
	}
288
289
290
	/**
291
	 * @param array $data
292
	 */
293
	public function import(array $data) {
294
		$this->setAddressBookId($this->get('addressBookId', $data));
295
		$this->setOwner($this->get('owner', $data));
296
		$this->setCardUri($this->get('cardUri', $data));
297
		$this->setContactId($this->get('contactId', $data));
298
		$this->setFn($this->get('fn', $data));
299
		$this->setEmails($this->getArray('emails', $data));
300
		$this->setClouds($this->getArray('clouds', $data));
301
		$this->setGroups($this->getArray('groups', $data));
302
		$this->setUserId($this->get('userId', $data));
303
	}
304
305
306
	/**
307
	 * @param string $dav
308
	 */
309
	public function importFromDav(string $dav) {
310
		$data = $this->parseDav($dav);
311
312
		$this->setContactId($this->get('UID', $data));
313
		$this->setFn($this->get('FN', $data));
314
		$this->setEmails($this->getArray('EMAILS', $data));
315
		$this->setClouds($this->getArray('CLOUDS', $data));
316
		$this->setGroups($this->getArray('CATEGORIES', $data));
317
	}
318
319
320
	/**
321
	 * get essential data from the dav content
322
	 * (also don't think we need regex)
323
	 *
324
	 * @param string $dav
325
	 *
326
	 * @return array
327
	 */
328
	private function parseDav(string $dav): array {
329
		$result = [
330
			'UID'        => '',
331
			'FN'         => '',
332
			'EMAILS'     => [],
333
			'CLOUDS'     => [],
334
			'CATEGORIES' => []
335
		];
336
337
		$data = preg_split('/\R/', $dav);
338
		foreach ($data as $entry) {
339
			if (trim($entry) === '' || strpos($entry, ':') === false) {
340
				continue;
341
			}
342
			list($k, $v) = explode(':', $entry, 2);
343
344
			$k = strtoupper($k);
345
			if (strpos($entry, ';') !== false) {
346
				list($k) = explode(';', $entry, 2);
347
			}
348
349
			switch ($k) {
350
				case 'UID':
351
				case 'FN':
352
					$result[$k] = $v;
353
					break;
354
355
				case 'EMAIL':
356
					if ($v !== '') {
357
						$result['EMAILS'][] = $v;
358
					}
359
					break;
360
361
				case 'CLOUD':
362
					if ($v !== '') {
363
						$result['CLOUDS'][] = $v;
364
					}
365
					break;
366
367
				case 'CATEGORIES':
368
					if (strpos($v, ',') === false) {
369
						$result['CATEGORIES'] = [$v];
370
					} else {
371
						$result['CATEGORIES'] = explode(',', $v);
372
					}
373
					break;
374
			}
375
		}
376
377
		return $result;
378
379
	}
380
381
382
	/**
383
	 * @return array
384
	 */
385
	public function jsonSerialize(): array {
386
		return [
387
			'addressBookId' => $this->getAddressBookId(),
388
			'owner'         => $this->getOwner(),
389
			'cardUri'       => $this->getCardUri(),
390
			'contactId'     => $this->getContactId(),
391
			'fn'            => $this->getFn(),
392
			'emails'        => $this->getEmails(),
393
			'clouds'        => $this->getClouds(),
394
			'groups'        => $this->getGroups(),
395
			'userId'        => $this->getUserId()
396
		];
397
	}
398
399
}
400