Passed
Push — master ( 4f3790...b8156a )
by Stefan
01:34
created

VCardAddress::getPreferred()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
declare(strict_types=1);
3
4
namespace SKien\VCard;
5
6
/**
7
 * Class representing address within a contact.
8
 * Each contact may contains multiple adresses.
9
 *
10
 * uses helpers from trait VCardHelper
11
 * @see VCardHelper
12
 *
13
* history:
14
 * date         version
15
 * 2020-02-23   initial version.
16
 * 2020-05-28   renamed namespace to fit PSR-4 recommendations for autoloading.
17
 * 2020-07-22   added missing PHP 7.4 type hints / docBlock changes
18
 *
19
 * @package SKien-VCard
20
 * @since 1.0.0
21
 * @version 1.0.3
22
 * @author Stefanius <[email protected]>
23
 * @copyright MIT License - see the LICENSE file for details
24
 */
25
class VCardAddress
26
{
27
    use VCardHelper;
28
29
    /** @var string  street (including house number)    */
30
    protected string $strStr = '';
31
    /** @var string  city   */
32
    protected string $strCity = '';
33
    /** @var string  postcode   */
34
    protected string $strPostcode = '';
35
    /** @var string  country */
36
    protected string $strCountry = '';
37
    /** @var string  region */
38
    protected string $strRegion = '';
39
    /** @var string  type (VCard::HOME, VCard::WORK, VCard::POSTAL, VCard::PARCEL)  */
40
    protected string $strType = '';
41
    /** @var bool  preferred address  */
42
    protected bool $bPreferred = false;
43
44
    /**
45
     * Full address information.
46
     * build semicolon delimitered string containing:
47
     *  - post office address (not supported)
48
     *  - extended address (not supported)
49
     *  - street (including house number)
50
     *  - city
51
     *  - region
52
     *  - postal code
53
     *  - country
54
     * @return string
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
     * label for address
77
     * @return string
78
     */
79
    public function buildLabel(): string
80
    {
81
        $strField = 'LABEL;TYPE=' . $this->strType;
82
        if ($this->bPreferred) {
83
            $strField .= ',PREF';
84
        }
85
86
        // values separated by semikolon
87
        $strValue  = $this->strStr . PHP_EOL;
88
        $strValue .= $this->strPostcode . ' ' . $this->strCity . PHP_EOL;
89
        if (strlen($this->strRegion) > 0 || strlen($this->strCountry) > 0 ) {
90
            $strSep = (strlen($this->strRegion) > 0 && strlen($this->strCountry) > 0 ) ? ' - ' : '';
91
            $strValue .= $this->strRegion . $strSep . $this->strCountry . PHP_EOL;
92
        }
93
94
        return $this->buildProperty($strField, $strValue);
95
    }
96
97
    /**
98
     * explode string into address components:
99
     *  - post office address (not supported)
100
     *  - extended address (not supported)
101
     *  - street (including house number)
102
     *  - city
103
     *  - region
104
     *  - postal code
105
     *  - country
106
     *  delimitered by semicolon (be aware of masked delimiters)
107
     *
108
     * @param string $strValue
109
     * @param array  $aParams
110
     */
111
    public function parseFullAddress(string $strValue, array $aParams) : void
112
    {
113
        $aSplit = $this->explodeMaskedString(';', $strValue);
114
        if (isset($aSplit[2])) {
115
            $this->strStr = $this->unmaskString($aSplit[2]);        // street (including house number)
116
        }
117
        if (isset($aSplit[3])) {
118
            $this->strCity = $this->unmaskString($aSplit[3]);       // city
119
        }
120
        if (isset($aSplit[4])) {
121
            $this->strRegion = $this->unmaskString($aSplit[4]);     // region
122
        }
123
        if (isset($aSplit[5])) {
124
            $this->strPostcode = $this->unmaskString($aSplit[5]);   // postal code
125
        }
126
        if (isset($aSplit[6])) {
127
            $this->strCountry = $this->unmaskString($aSplit[6]);    // country
128
        }
129
        if (isset($aParams['TYPE'])) {
130
            $this->strType = $aParams['TYPE'];
131
        } else {
132
            $this->strType = VCard::HOME;
133
        }
134
    }
135
136
    /**
137
     * @param string $strStr
138
     */
139
    public function setStr(string $strStr) : void
140
    {
141
        $this->strStr = $strStr;
142
    }
143
144
    /**
145
     * @param string $strCity
146
     */
147
    public function setCity(string $strCity) : void
148
    {
149
        $this->strCity = $strCity;
150
    }
151
152
    /**
153
     * @param string $strPostcode
154
     */
155
    public function setPostcode(string $strPostcode) : void
156
    {
157
        $this->strPostcode = $strPostcode;
158
    }
159
160
    /**
161
     * @param string $strCountry
162
     */
163
    public function setCountry(string $strCountry) : void
164
    {
165
        $this->strCountry = $strCountry;
166
    }
167
168
    /**
169
     * @param string $strRegion
170
     */
171
    public function setRegion(string $strRegion) : void
172
    {
173
        $this->strRegion = $strRegion;
174
    }
175
176
    /**
177
     * @param string $strType
178
     */
179
    public function setType(string $strType) : void
180
    {
181
        $this->strType = $strType;
182
    }
183
184
    /**
185
     * @param bool $bPreferred
186
     */
187
    public function setPreferred(bool $bPreferred) : void
188
    {
189
        $this->bPreferred = $bPreferred;
190
    }
191
192
    /**
193
     * @return string $strStr
194
     */
195
    public function getStr() : string
196
    {
197
        return $this->strStr;
198
    }
199
200
    /**
201
     * @return string  $strCity
202
     */
203
    public function getCity() : string
204
    {
205
        return $this->strCity;
206
    }
207
208
    /**
209
     * @return string  $strPostcode
210
     */
211
    public function getPostcode() : string
212
    {
213
        return $this->strPostcode;
214
    }
215
216
    /**
217
     * @return string  $strCountry
218
     */
219
    public function getCountry() : string
220
    {
221
        return $this->strCountry;
222
    }
223
224
    /**
225
     * @return string  $strRegion
226
     */
227
    public function getRegion() : string
228
    {
229
        return $this->strRegion;
230
    }
231
232
    /**
233
     * @return string  $strType
234
     */
235
    public function getType() : string
236
    {
237
        return $this->strType;
238
    }
239
240
    /**
241
     * @return bool $bPreferred
242
     */
243
    public function getPreferred() : bool
244
    {
245
        return $this->bPreferred;
246
    }
247
}