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

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