|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace SKien\VCard; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Class representing one address within a contact. |
|
8
|
|
|
* |
|
9
|
|
|
* #### Add an address to a contact for writing: |
|
10
|
|
|
* Create a new instance of a `VCardAdress`, set its property and add the address |
|
11
|
|
|
* to a contact using `VCardContact::addAddress()` |
|
12
|
|
|
* |
|
13
|
|
|
* #### Retrieve an address within a contact: |
|
14
|
|
|
* Use `VCardContact::getAddress()` to retrieve existing address within a given |
|
15
|
|
|
* contact. |
|
16
|
|
|
* |
|
17
|
|
|
* @see VCardContact::addAddress() |
|
18
|
|
|
* @see VCardContact::getAddress() |
|
19
|
|
|
* |
|
20
|
|
|
* @package VCard |
|
21
|
|
|
* @author Stefanius <[email protected]> |
|
22
|
|
|
* @copyright MIT License - see the LICENSE file for details |
|
23
|
|
|
*/ |
|
24
|
|
|
class VCardAddress |
|
25
|
|
|
{ |
|
26
|
|
|
use VCardHelper; |
|
27
|
|
|
|
|
28
|
|
|
/** @var string street (including house number) */ |
|
29
|
|
|
protected string $strStr = ''; |
|
30
|
|
|
/** @var string city */ |
|
31
|
|
|
protected string $strCity = ''; |
|
32
|
|
|
/** @var string postcode */ |
|
33
|
|
|
protected string $strPostcode = ''; |
|
34
|
|
|
/** @var string country */ |
|
35
|
|
|
protected string $strCountry = ''; |
|
36
|
|
|
/** @var string region */ |
|
37
|
|
|
protected string $strRegion = ''; |
|
38
|
|
|
/** @var string post office box */ |
|
39
|
|
|
protected string $strPOBox = ''; |
|
40
|
|
|
/** @var string extended address (e.g. apartment or suite number) */ |
|
41
|
|
|
protected string $strExtAddress = ''; |
|
42
|
|
|
/** @var string type (VCard::HOME, VCard::WORK, VCard::POSTAL, VCard::PARCEL) */ |
|
43
|
|
|
protected string $strType = ''; |
|
44
|
|
|
/** @var bool preferred address */ |
|
45
|
|
|
protected bool $bPreferred = false; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Full address information. |
|
49
|
|
|
* Build semicolon delimitered string containing: |
|
50
|
|
|
* - post office address (postbox) |
|
51
|
|
|
* - extended address (e.g. apartment or suite number) |
|
52
|
|
|
* - street (including house number) |
|
53
|
|
|
* - city |
|
54
|
|
|
* - region |
|
55
|
|
|
* - postal code |
|
56
|
|
|
* - country |
|
57
|
|
|
* @return string |
|
58
|
|
|
* @internal only should be called by the VCardContactWriter |
|
59
|
|
|
*/ |
|
60
|
|
|
public function buildFullAddress() : string |
|
61
|
|
|
{ |
|
62
|
|
|
$strField = 'ADR;TYPE=' . $this->strType; |
|
63
|
|
|
if ($this->bPreferred) { |
|
64
|
|
|
$strField .= ',PREF'; |
|
65
|
|
|
} |
|
66
|
|
|
// postbox |
|
67
|
|
|
// extended address |
|
68
|
|
|
// street (including house number) |
|
69
|
|
|
// city |
|
70
|
|
|
// region |
|
71
|
|
|
// postal code |
|
72
|
|
|
// country |
|
73
|
|
|
// values separated by semikolon |
|
74
|
|
|
$strValue = $this->maskString($this->strPOBox) . ';'; |
|
75
|
|
|
$strValue .= $this->maskString($this->strExtAddress) . ';'; |
|
76
|
|
|
$strValue .= $this->maskString($this->strStr) . ';'; |
|
77
|
|
|
$strValue .= $this->maskString($this->strCity) . ';'; |
|
78
|
|
|
$strValue .= $this->maskString($this->strRegion) . ';'; |
|
79
|
|
|
$strValue .= $this->maskString($this->strPostcode) . ';'; |
|
80
|
|
|
$strValue .= $this->maskString($this->strCountry); |
|
81
|
|
|
|
|
82
|
|
|
return $this->buildProperty($strField, $strValue, false); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Build label for ther address. |
|
87
|
|
|
* @return string |
|
88
|
|
|
* @internal only should be called by the VCardContactWriter |
|
89
|
|
|
*/ |
|
90
|
|
|
public function buildLabel(): string |
|
91
|
|
|
{ |
|
92
|
|
|
$strField = 'LABEL;TYPE=' . $this->strType; |
|
93
|
|
|
if ($this->bPreferred) { |
|
94
|
|
|
$strField .= ',PREF'; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
// values separated by semikolon |
|
98
|
|
|
$strValue = $this->strStr . PHP_EOL; |
|
99
|
|
|
$strValue .= $this->strPostcode . ' ' . $this->strCity . PHP_EOL; |
|
100
|
|
|
if (strlen($this->strRegion) > 0 || strlen($this->strCountry) > 0) { |
|
101
|
|
|
$strSep = (empty($this->strRegion) || empty($this->strCountry)) ? '' : ' - '; |
|
102
|
|
|
$strValue .= $this->strRegion . $strSep . $this->strCountry . PHP_EOL; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
return $this->buildProperty($strField, $strValue); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* explode string into address components: |
|
110
|
|
|
* - post office address (postbox) |
|
111
|
|
|
* - extended address (e.g. apartment or suite number) |
|
112
|
|
|
* - street (including house number) |
|
113
|
|
|
* - city |
|
114
|
|
|
* - region |
|
115
|
|
|
* - postal code |
|
116
|
|
|
* - country |
|
117
|
|
|
* delimitered by semicolon (be aware of masked delimiters) |
|
118
|
|
|
* |
|
119
|
|
|
* @param string $strValue |
|
120
|
|
|
* @param array<string,string> $aParams |
|
121
|
|
|
* @internal only should be called by the VCardContactReader |
|
122
|
|
|
*/ |
|
123
|
|
|
public function parseFullAddress(string $strValue, array $aParams) : void |
|
124
|
|
|
{ |
|
125
|
|
|
$aSplit = $this->explodeMaskedString(';', $strValue); |
|
126
|
|
|
|
|
127
|
|
|
$this->strPOBox = $this->unmaskString($aSplit[0] ?? ''); |
|
128
|
|
|
$this->strExtAddress = $this->unmaskString($aSplit[1] ?? ''); |
|
129
|
|
|
$this->strStr = $this->unmaskString($aSplit[2] ?? ''); |
|
130
|
|
|
$this->strCity = $this->unmaskString($aSplit[3] ?? ''); |
|
131
|
|
|
$this->strRegion = $this->unmaskString($aSplit[4] ?? ''); |
|
132
|
|
|
$this->strPostcode = $this->unmaskString($aSplit[5] ?? ''); |
|
133
|
|
|
$this->strCountry = $this->unmaskString($aSplit[6] ?? ''); |
|
134
|
|
|
$this->strType = $aParams['TYPE'] ?? VCard::HOME; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Set street. |
|
139
|
|
|
* @param string $strStr |
|
140
|
|
|
*/ |
|
141
|
|
|
public function setStr(string $strStr) : void |
|
142
|
|
|
{ |
|
143
|
|
|
$this->strStr = $strStr; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Set city. |
|
148
|
|
|
* @param string $strCity |
|
149
|
|
|
*/ |
|
150
|
|
|
public function setCity(string $strCity) : void |
|
151
|
|
|
{ |
|
152
|
|
|
$this->strCity = $strCity; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Set Postcode |
|
157
|
|
|
* @param string $strPostcode |
|
158
|
|
|
*/ |
|
159
|
|
|
public function setPostcode(string $strPostcode) : void |
|
160
|
|
|
{ |
|
161
|
|
|
$this->strPostcode = $strPostcode; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Set country. |
|
166
|
|
|
* @param string $strCountry |
|
167
|
|
|
*/ |
|
168
|
|
|
public function setCountry(string $strCountry) : void |
|
169
|
|
|
{ |
|
170
|
|
|
$this->strCountry = $strCountry; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Set region. |
|
175
|
|
|
* @param string $strRegion |
|
176
|
|
|
*/ |
|
177
|
|
|
public function setRegion(string $strRegion) : void |
|
178
|
|
|
{ |
|
179
|
|
|
$this->strRegion = $strRegion; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Set post office box. |
|
184
|
|
|
* @param string $strPOBox |
|
185
|
|
|
*/ |
|
186
|
|
|
public function setPOBox(string $strPOBox) : void |
|
187
|
|
|
{ |
|
188
|
|
|
$this->strPOBox = $strPOBox; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* Set extended address (e.g. apartment or suite number). |
|
193
|
|
|
* @param string $strExtAddress |
|
194
|
|
|
*/ |
|
195
|
|
|
public function setExtAddress(string $strExtAddress) : void |
|
196
|
|
|
{ |
|
197
|
|
|
$this->strExtAddress = $strExtAddress; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* Set type. |
|
202
|
|
|
* Any combination of the predefined types VCard::PREF, VCard::WORK, VCard::HOME |
|
203
|
|
|
* VCard::POSTAL, VCard::PARCEL, VCard::INTER or VCard::DOMESTIC can be set. |
|
204
|
|
|
* @param string|array<string> $type one single type or an array of multiple types |
|
205
|
|
|
*/ |
|
206
|
|
|
public function setType($type) : void |
|
207
|
|
|
{ |
|
208
|
|
|
$this->strType = is_array($type) ? implode(',', $type) : $type; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* Set this address as preferred. |
|
213
|
|
|
* @param bool $bPreferred |
|
214
|
|
|
*/ |
|
215
|
|
|
public function setPreferred(bool $bPreferred) : void |
|
216
|
|
|
{ |
|
217
|
|
|
$this->bPreferred = $bPreferred; |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* Get street. |
|
222
|
|
|
* @return string $strStr |
|
223
|
|
|
*/ |
|
224
|
|
|
public function getStr() : string |
|
225
|
|
|
{ |
|
226
|
|
|
return $this->strStr; |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* Get city. |
|
231
|
|
|
* @return string $strCity |
|
232
|
|
|
*/ |
|
233
|
|
|
public function getCity() : string |
|
234
|
|
|
{ |
|
235
|
|
|
return $this->strCity; |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
/** |
|
239
|
|
|
* Get postcode. |
|
240
|
|
|
* @return string $strPostcode |
|
241
|
|
|
*/ |
|
242
|
|
|
public function getPostcode() : string |
|
243
|
|
|
{ |
|
244
|
|
|
return $this->strPostcode; |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
/** |
|
248
|
|
|
* Get country. |
|
249
|
|
|
* @return string $strCountry |
|
250
|
|
|
*/ |
|
251
|
|
|
public function getCountry() : string |
|
252
|
|
|
{ |
|
253
|
|
|
return $this->strCountry; |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
/** |
|
257
|
|
|
* Get region. |
|
258
|
|
|
* @return string $strRegion |
|
259
|
|
|
*/ |
|
260
|
|
|
public function getRegion() : string |
|
261
|
|
|
{ |
|
262
|
|
|
return $this->strRegion; |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
/** |
|
266
|
|
|
* Get post office box. |
|
267
|
|
|
* @return string $strPOBox |
|
268
|
|
|
*/ |
|
269
|
|
|
public function getPOBox() : string |
|
270
|
|
|
{ |
|
271
|
|
|
return $this->strPOBox; |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
/** |
|
275
|
|
|
* Get extended address (e.g. apartment or suite number). |
|
276
|
|
|
* @return string $strRegion |
|
277
|
|
|
*/ |
|
278
|
|
|
public function getExtAddress() : string |
|
279
|
|
|
{ |
|
280
|
|
|
return $this->strExtAddress; |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
/** |
|
284
|
|
|
* Get type. |
|
285
|
|
|
* @return string $strType can b comma separated list of multiple types. |
|
286
|
|
|
*/ |
|
287
|
|
|
public function getType() : string |
|
288
|
|
|
{ |
|
289
|
|
|
return $this->strType; |
|
290
|
|
|
} |
|
291
|
|
|
|
|
292
|
|
|
/** |
|
293
|
|
|
* Get preferred state. |
|
294
|
|
|
* @return bool $bPreferred |
|
295
|
|
|
*/ |
|
296
|
|
|
public function isPreferred() : bool |
|
297
|
|
|
{ |
|
298
|
|
|
return $this->bPreferred; |
|
299
|
|
|
} |
|
300
|
|
|
} |