Passed
Push — master ( 084100...7393e4 )
by Stefan
06:52
created

VCardAddress::setPOBox()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 1
c 1
b 0
f 1
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
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
        if (isset($aSplit[0])) {
127
            // post office box
128
            $this->strPOBox = $this->unmaskString($aSplit[0]);
129
        }
130
        if (isset($aSplit[1])) {
131
            // extended address (e.g. apartment or suite number)
132
            $this->strExtAddress = $this->unmaskString($aSplit[1]);
133
        }
134
        if (isset($aSplit[2])) {
135
            // street (including house number)
136
            $this->strStr = $this->unmaskString($aSplit[2]);
137
        }
138
        if (isset($aSplit[3])) {
139
            // city
140
            $this->strCity = $this->unmaskString($aSplit[3]);
141
        }
142
        if (isset($aSplit[4])) {
143
            // region
144
            $this->strRegion = $this->unmaskString($aSplit[4]);
145
        }
146
        if (isset($aSplit[5])) {
147
            // postal code
148
            $this->strPostcode = $this->unmaskString($aSplit[5]);
149
        }
150
        if (isset($aSplit[6])) {
151
            // country
152
            $this->strCountry = $this->unmaskString($aSplit[6]);
153
        }
154
        if (isset($aParams['TYPE'])) {
155
            $this->strType = $aParams['TYPE'];
156
        } else {
157
            $this->strType = VCard::HOME;
158
        }
159
    }
160
161
    /**
162
     * Set street.
163
     * @param string $strStr
164
     */
165
    public function setStr(string $strStr) : void
166
    {
167
        $this->strStr = $strStr;
168
    }
169
170
    /**
171
     * Set city.
172
     * @param string $strCity
173
     */
174
    public function setCity(string $strCity) : void
175
    {
176
        $this->strCity = $strCity;
177
    }
178
179
    /**
180
     * Set Postcode
181
     * @param string $strPostcode
182
     */
183
    public function setPostcode(string $strPostcode) : void
184
    {
185
        $this->strPostcode = $strPostcode;
186
    }
187
188
    /**
189
     * Set country.
190
     * @param string $strCountry
191
     */
192
    public function setCountry(string $strCountry) : void
193
    {
194
        $this->strCountry = $strCountry;
195
    }
196
197
    /**
198
     * Set region.
199
     * @param string $strRegion
200
     */
201
    public function setRegion(string $strRegion) : void
202
    {
203
        $this->strRegion = $strRegion;
204
    }
205
206
    /**
207
     * Set post office box.
208
     * @param string $strPOBox
209
     */
210
    public function setPOBox(string $strPOBox) : void
211
    {
212
        $this->strPOBox = $strPOBox;
213
    }
214
215
    /**
216
     * Set extended address (e.g. apartment or suite number).
217
     * @param string $strExtAddress
218
     */
219
    public function setExtAddress(string $strExtAddress) : void
220
    {
221
        $this->strExtAddress = $strExtAddress;
222
    }
223
224
    /**
225
     * Set type.
226
     * Any combination of the predefined types VCard::PREF, VCard::WORK, VCard::HOME
227
     * VCard::POSTAL, VCard::PARCEL, VCard::INTER or VCard::DOMESTIC can be set.
228
     * @param string|array<string> $type    one single type or an array of multiple types
229
     */
230
    public function setType($type) : void
231
    {
232
        $this->strType = is_array($type) ? implode(',', $type) : $type;
233
    }
234
235
    /**
236
     * Set this address as preferred.
237
     * @param bool $bPreferred
238
     */
239
    public function setPreferred(bool $bPreferred) : void
240
    {
241
        $this->bPreferred = $bPreferred;
242
    }
243
244
    /**
245
     * Get street.
246
     * @return string $strStr
247
     */
248
    public function getStr() : string
249
    {
250
        return $this->strStr;
251
    }
252
253
    /**
254
     * Get city.
255
     * @return string  $strCity
256
     */
257
    public function getCity() : string
258
    {
259
        return $this->strCity;
260
    }
261
262
    /**
263
     * Get postcode.
264
     * @return string  $strPostcode
265
     */
266
    public function getPostcode() : string
267
    {
268
        return $this->strPostcode;
269
    }
270
271
    /**
272
     * Get country.
273
     * @return string  $strCountry
274
     */
275
    public function getCountry() : string
276
    {
277
        return $this->strCountry;
278
    }
279
280
    /**
281
     * Get region.
282
     * @return string  $strRegion
283
     */
284
    public function getRegion() : string
285
    {
286
        return $this->strRegion;
287
    }
288
289
    /**
290
     * Get post office box.
291
     * @return string  $strPOBox
292
     */
293
    public function getPOBox() : string
294
    {
295
        return $this->strPOBox;
296
    }
297
298
    /**
299
     * Get extended address (e.g. apartment or suite number).
300
     * @return string  $strRegion
301
     */
302
    public function getExtAddress() : string
303
    {
304
        return $this->strExtAddress;
305
    }
306
307
    /**
308
     * Get type.
309
     * @return string  $strType can b comma separated list of multiple types.
310
     */
311
    public function getType() : string
312
    {
313
        return $this->strType;
314
    }
315
316
    /**
317
     * Get preferred state.
318
     * @return bool $bPreferred
319
     */
320
    public function isPreferred() : bool
321
    {
322
        return $this->bPreferred;
323
    }
324
}