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 from a read 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 type (VCard::HOME, VCard::WORK, VCard::POSTAL, VCard::PARCEL) */
|
39
|
|
|
protected string $strType = '';
|
40
|
|
|
/** @var bool preferred address */
|
41
|
|
|
protected bool $bPreferred = false;
|
42
|
|
|
|
43
|
|
|
/**
|
44
|
|
|
* Full address information.
|
45
|
|
|
* Build semicolon delimitered string containing:
|
46
|
|
|
* - post office address (not supported)
|
47
|
|
|
* - extended address (not supported)
|
48
|
|
|
* - street (including house number)
|
49
|
|
|
* - city
|
50
|
|
|
* - region
|
51
|
|
|
* - postal code
|
52
|
|
|
* - country
|
53
|
|
|
* @return string
|
54
|
|
|
* @internal only should be called by the VCardContactWriter
|
55
|
|
|
*/
|
56
|
|
|
public function buildFullAddress() : string
|
57
|
|
|
{
|
58
|
|
|
$strField = 'ADR;TYPE=' . $this->strType;
|
59
|
|
|
if ($this->bPreferred) {
|
60
|
|
|
$strField .= ',PREF';
|
61
|
|
|
}
|
62
|
|
|
|
63
|
|
|
// values separated by semikolon
|
64
|
|
|
$strValue = ';'; // post office address (not supported)
|
65
|
|
|
$strValue .= ';'; // extended address (not supported)
|
66
|
|
|
$strValue .= $this->maskString($this->strStr) . ';'; // street (including house number)
|
67
|
|
|
$strValue .= $this->maskString($this->strCity) . ';'; // city
|
68
|
|
|
$strValue .= $this->maskString($this->strRegion) . ';'; // region
|
69
|
|
|
$strValue .= $this->maskString($this->strPostcode) . ';'; // postal code
|
70
|
|
|
$strValue .= $this->maskString($this->strCountry); // country
|
71
|
|
|
|
72
|
|
|
return $this->buildProperty($strField, $strValue, false);
|
73
|
|
|
}
|
74
|
|
|
|
75
|
|
|
/**
|
76
|
|
|
* Build label for ther address.
|
77
|
|
|
* @return string
|
78
|
|
|
* @internal only should be called by the VCardContactWriter
|
79
|
|
|
*/
|
80
|
|
|
public function buildLabel(): string
|
81
|
|
|
{
|
82
|
|
|
$strField = 'LABEL;TYPE=' . $this->strType;
|
83
|
|
|
if ($this->bPreferred) {
|
84
|
|
|
$strField .= ',PREF';
|
85
|
|
|
}
|
86
|
|
|
|
87
|
|
|
// values separated by semikolon
|
88
|
|
|
$strValue = $this->strStr . PHP_EOL;
|
89
|
|
|
$strValue .= $this->strPostcode . ' ' . $this->strCity . PHP_EOL;
|
90
|
|
|
if (strlen($this->strRegion) > 0 || strlen($this->strCountry) > 0 ) {
|
91
|
|
|
$strSep = (strlen($this->strRegion) > 0 && strlen($this->strCountry) > 0 ) ? ' - ' : '';
|
92
|
|
|
$strValue .= $this->strRegion . $strSep . $this->strCountry . PHP_EOL;
|
93
|
|
|
}
|
94
|
|
|
|
95
|
|
|
return $this->buildProperty($strField, $strValue);
|
96
|
|
|
}
|
97
|
|
|
|
98
|
|
|
/**
|
99
|
|
|
* explode string into address components:
|
100
|
|
|
* - post office address (not supported)
|
101
|
|
|
* - extended address (not supported)
|
102
|
|
|
* - street (including house number)
|
103
|
|
|
* - city
|
104
|
|
|
* - region
|
105
|
|
|
* - postal code
|
106
|
|
|
* - country
|
107
|
|
|
* delimitered by semicolon (be aware of masked delimiters)
|
108
|
|
|
*
|
109
|
|
|
* @param string $strValue
|
110
|
|
|
* @param array $aParams
|
111
|
|
|
* @internal only should be called by the VCardContactReader
|
112
|
|
|
*/
|
113
|
|
|
public function parseFullAddress(string $strValue, array $aParams) : void
|
114
|
|
|
{
|
115
|
|
|
$aSplit = $this->explodeMaskedString(';', $strValue);
|
116
|
|
|
if (isset($aSplit[2])) {
|
117
|
|
|
$this->strStr = $this->unmaskString($aSplit[2]); // street (including house number)
|
118
|
|
|
}
|
119
|
|
|
if (isset($aSplit[3])) {
|
120
|
|
|
$this->strCity = $this->unmaskString($aSplit[3]); // city
|
121
|
|
|
}
|
122
|
|
|
if (isset($aSplit[4])) {
|
123
|
|
|
$this->strRegion = $this->unmaskString($aSplit[4]); // region
|
124
|
|
|
}
|
125
|
|
|
if (isset($aSplit[5])) {
|
126
|
|
|
$this->strPostcode = $this->unmaskString($aSplit[5]); // postal code
|
127
|
|
|
}
|
128
|
|
|
if (isset($aSplit[6])) {
|
129
|
|
|
$this->strCountry = $this->unmaskString($aSplit[6]); // country
|
130
|
|
|
}
|
131
|
|
|
if (isset($aParams['TYPE'])) {
|
132
|
|
|
$this->strType = $aParams['TYPE'];
|
133
|
|
|
} else {
|
134
|
|
|
$this->strType = VCard::HOME;
|
135
|
|
|
}
|
136
|
|
|
}
|
137
|
|
|
|
138
|
|
|
/**
|
139
|
|
|
* Set street.
|
140
|
|
|
* @param string $strStr
|
141
|
|
|
*/
|
142
|
|
|
public function setStr(string $strStr) : void
|
143
|
|
|
{
|
144
|
|
|
$this->strStr = $strStr;
|
145
|
|
|
}
|
146
|
|
|
|
147
|
|
|
/**
|
148
|
|
|
* Set city.
|
149
|
|
|
* @param string $strCity
|
150
|
|
|
*/
|
151
|
|
|
public function setCity(string $strCity) : void
|
152
|
|
|
{
|
153
|
|
|
$this->strCity = $strCity;
|
154
|
|
|
}
|
155
|
|
|
|
156
|
|
|
/**
|
157
|
|
|
* Set Postcode
|
158
|
|
|
* @param string $strPostcode
|
159
|
|
|
*/
|
160
|
|
|
public function setPostcode(string $strPostcode) : void
|
161
|
|
|
{
|
162
|
|
|
$this->strPostcode = $strPostcode;
|
163
|
|
|
}
|
164
|
|
|
|
165
|
|
|
/**
|
166
|
|
|
* Set country
|
167
|
|
|
* @param string $strCountry
|
168
|
|
|
*/
|
169
|
|
|
public function setCountry(string $strCountry) : void
|
170
|
|
|
{
|
171
|
|
|
$this->strCountry = $strCountry;
|
172
|
|
|
}
|
173
|
|
|
|
174
|
|
|
/**
|
175
|
|
|
* Set region
|
176
|
|
|
* @param string $strRegion
|
177
|
|
|
*/
|
178
|
|
|
public function setRegion(string $strRegion) : void
|
179
|
|
|
{
|
180
|
|
|
$this->strRegion = $strRegion;
|
181
|
|
|
}
|
182
|
|
|
|
183
|
|
|
/**
|
184
|
|
|
* Set type.
|
185
|
|
|
* Any combination of the predefined types VCard::PREF, VCard::WORK, VCard::HOME
|
186
|
|
|
* VCard::POSTAL, VCard::PARCEL, VCard::INTER or VCard::DOMESTIC can be set.
|
187
|
|
|
* @param string|array $type one single type or an array of multiple types
|
188
|
|
|
*/
|
189
|
|
|
public function setType($type) : void
|
190
|
|
|
{
|
191
|
|
|
$this->strType = is_array($type) ? implode(',', $type) : $type;
|
192
|
|
|
}
|
193
|
|
|
|
194
|
|
|
/**
|
195
|
|
|
* Set this address as preferred.
|
196
|
|
|
* @param bool $bPreferred
|
197
|
|
|
*/
|
198
|
|
|
public function setPreferred(bool $bPreferred) : void
|
199
|
|
|
{
|
200
|
|
|
$this->bPreferred = $bPreferred;
|
201
|
|
|
}
|
202
|
|
|
|
203
|
|
|
/**
|
204
|
|
|
* Get street.
|
205
|
|
|
* @return string $strStr
|
206
|
|
|
*/
|
207
|
|
|
public function getStr() : string
|
208
|
|
|
{
|
209
|
|
|
return $this->strStr;
|
210
|
|
|
}
|
211
|
|
|
|
212
|
|
|
/**
|
213
|
|
|
* Get city.
|
214
|
|
|
* @return string $strCity
|
215
|
|
|
*/
|
216
|
|
|
public function getCity() : string
|
217
|
|
|
{
|
218
|
|
|
return $this->strCity;
|
219
|
|
|
}
|
220
|
|
|
|
221
|
|
|
/**
|
222
|
|
|
* Get postcode.
|
223
|
|
|
* @return string $strPostcode
|
224
|
|
|
*/
|
225
|
|
|
public function getPostcode() : string
|
226
|
|
|
{
|
227
|
|
|
return $this->strPostcode;
|
228
|
|
|
}
|
229
|
|
|
|
230
|
|
|
/**
|
231
|
|
|
* Get country.
|
232
|
|
|
* @return string $strCountry
|
233
|
|
|
*/
|
234
|
|
|
public function getCountry() : string
|
235
|
|
|
{
|
236
|
|
|
return $this->strCountry;
|
237
|
|
|
}
|
238
|
|
|
|
239
|
|
|
/**
|
240
|
|
|
* Get region.
|
241
|
|
|
* @return string $strRegion
|
242
|
|
|
*/
|
243
|
|
|
public function getRegion() : string
|
244
|
|
|
{
|
245
|
|
|
return $this->strRegion;
|
246
|
|
|
}
|
247
|
|
|
|
248
|
|
|
/**
|
249
|
|
|
* Get type.
|
250
|
|
|
* @return string $strType can b comma separated list of multiple types.
|
251
|
|
|
*/
|
252
|
|
|
public function getType() : string
|
253
|
|
|
{
|
254
|
|
|
return $this->strType;
|
255
|
|
|
}
|
256
|
|
|
|
257
|
|
|
/**
|
258
|
|
|
* Get preferred state.
|
259
|
|
|
* @return bool $bPreferred
|
260
|
|
|
*/
|
261
|
|
|
public function isPreferred() : bool
|
262
|
|
|
{
|
263
|
|
|
return $this->bPreferred;
|
264
|
|
|
}
|
265
|
|
|
} |