1
|
|
|
<?php |
2
|
|
|
namespace Bpost\BpostApiClient\Bpost\Order; |
3
|
|
|
|
4
|
|
|
use Bpost\BpostApiClient\Exception\BpostLogicException\BpostInvalidLengthException; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* bPost Address class |
8
|
|
|
* |
9
|
|
|
* @author Tijs Verkoyen <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
class Address |
12
|
|
|
{ |
13
|
|
|
const TAG_NAME = 'common:address'; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
private $streetName; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
private $number; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
private $box; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
private $postalCode; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
private $locality; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
private $countryCode = 'BE'; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param string $box |
47
|
|
|
* @throws BpostInvalidLengthException |
48
|
|
|
*/ |
49
|
16 |
View Code Duplication |
public function setBox($box) |
|
|
|
|
50
|
|
|
{ |
51
|
16 |
|
$length = 8; |
52
|
16 |
|
if (mb_strlen($box) > $length) { |
53
|
3 |
|
throw new BpostInvalidLengthException('box', mb_strlen($box), $length); |
54
|
|
|
} |
55
|
13 |
|
$this->box = $box; |
56
|
13 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
15 |
|
public function getBox() |
62
|
|
|
{ |
63
|
15 |
|
return $this->box; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param string $countryCode |
68
|
|
|
* @throws BpostInvalidLengthException |
69
|
|
|
*/ |
70
|
21 |
View Code Duplication |
public function setCountryCode($countryCode) |
|
|
|
|
71
|
|
|
{ |
72
|
21 |
|
$length = 2; |
73
|
21 |
|
if (mb_strlen($countryCode) > $length) { |
74
|
3 |
|
throw new BpostInvalidLengthException('countryCode', mb_strlen($countryCode), $length); |
75
|
|
|
} |
76
|
18 |
|
$this->countryCode = strtoupper($countryCode); |
77
|
18 |
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
15 |
|
public function getCountryCode() |
83
|
|
|
{ |
84
|
15 |
|
return $this->countryCode; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param string $locality |
89
|
|
|
* @throws BpostInvalidLengthException |
90
|
|
|
*/ |
91
|
21 |
View Code Duplication |
public function setLocality($locality) |
|
|
|
|
92
|
|
|
{ |
93
|
21 |
|
$length = 40; |
94
|
21 |
|
if (mb_strlen($locality) > $length) { |
95
|
3 |
|
throw new BpostInvalidLengthException('locality', mb_strlen($locality), $length); |
96
|
|
|
} |
97
|
18 |
|
$this->locality = $locality; |
98
|
18 |
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
15 |
|
public function getLocality() |
104
|
|
|
{ |
105
|
15 |
|
return $this->locality; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param string $number |
110
|
|
|
* @throws BpostInvalidLengthException |
111
|
|
|
*/ |
112
|
21 |
View Code Duplication |
public function setNumber($number) |
|
|
|
|
113
|
|
|
{ |
114
|
21 |
|
$length = 8; |
115
|
21 |
|
if (mb_strlen($number) > $length) { |
116
|
3 |
|
throw new BpostInvalidLengthException('number', mb_strlen($number), $length); |
117
|
|
|
} |
118
|
18 |
|
$this->number = $number; |
119
|
18 |
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @return string |
123
|
|
|
*/ |
124
|
15 |
|
public function getNumber() |
125
|
|
|
{ |
126
|
15 |
|
return $this->number; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param string $postalCode |
131
|
|
|
* @throws BpostInvalidLengthException |
132
|
|
|
*/ |
133
|
21 |
View Code Duplication |
public function setPostalCode($postalCode) |
|
|
|
|
134
|
|
|
{ |
135
|
21 |
|
$length = 40; |
136
|
21 |
|
if (mb_strlen($postalCode) > $length) { |
137
|
3 |
|
throw new BpostInvalidLengthException('postalCode', mb_strlen($postalCode), $length); |
138
|
|
|
} |
139
|
18 |
|
$this->postalCode = $postalCode; |
140
|
18 |
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @return string |
144
|
|
|
*/ |
145
|
15 |
|
public function getPostalCode() |
146
|
|
|
{ |
147
|
15 |
|
return $this->postalCode; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param string $streetName |
152
|
|
|
* @throws BpostInvalidLengthException |
153
|
|
|
*/ |
154
|
21 |
View Code Duplication |
public function setStreetName($streetName) |
|
|
|
|
155
|
|
|
{ |
156
|
21 |
|
$length = 40; |
157
|
21 |
|
if (mb_strlen($streetName) > $length) { |
158
|
3 |
|
throw new BpostInvalidLengthException('streetName', mb_strlen($streetName), $length); |
159
|
|
|
} |
160
|
18 |
|
$this->streetName = $streetName; |
161
|
18 |
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @return string |
165
|
|
|
*/ |
166
|
16 |
|
public function getStreetName() |
167
|
|
|
{ |
168
|
16 |
|
return $this->streetName; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @param string $streetName |
|
|
|
|
173
|
|
|
* @param string $number |
|
|
|
|
174
|
|
|
* @param string $box |
|
|
|
|
175
|
|
|
* @param string $postalCode |
|
|
|
|
176
|
|
|
* @param string $locality |
|
|
|
|
177
|
|
|
* @param string $countryCode |
|
|
|
|
178
|
|
|
* |
179
|
|
|
* @throws BpostInvalidLengthException |
180
|
|
|
*/ |
181
|
21 |
|
public function __construct( |
182
|
|
|
$streetName = null, |
183
|
|
|
$number = null, |
184
|
|
|
$box = null, |
185
|
|
|
$postalCode = null, |
186
|
|
|
$locality = null, |
187
|
|
|
$countryCode = null |
188
|
|
|
) { |
189
|
21 |
|
if ($streetName != null) { |
|
|
|
|
190
|
11 |
|
$this->setStreetName($streetName); |
191
|
11 |
|
} |
192
|
21 |
|
if ($number != null) { |
|
|
|
|
193
|
11 |
|
$this->setNumber($number); |
194
|
11 |
|
} |
195
|
21 |
|
if ($box != null) { |
|
|
|
|
196
|
10 |
|
$this->setBox($box); |
197
|
10 |
|
} |
198
|
21 |
|
if ($postalCode != null) { |
|
|
|
|
199
|
11 |
|
$this->setPostalCode($postalCode); |
200
|
11 |
|
} |
201
|
21 |
|
if ($locality != null) { |
|
|
|
|
202
|
11 |
|
$this->setLocality($locality); |
203
|
11 |
|
} |
204
|
21 |
|
if ($countryCode != null) { |
|
|
|
|
205
|
11 |
|
$this->setCountryCode($countryCode); |
206
|
11 |
|
} |
207
|
21 |
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Return the object as an array for usage in the XML |
211
|
|
|
* |
212
|
|
|
* @param \DOMDocument $document |
213
|
|
|
* @param string $prefix |
214
|
|
|
* @return \DOMElement |
215
|
|
|
*/ |
216
|
14 |
|
public function toXML(\DOMDocument $document, $prefix = 'common') |
217
|
|
|
{ |
218
|
14 |
|
$tagName = static::TAG_NAME; |
219
|
14 |
|
$address = $document->createElement($tagName); |
220
|
14 |
|
$document->appendChild($address); |
221
|
|
|
|
222
|
14 |
|
if ($this->getStreetName() !== null) { |
223
|
14 |
|
$tagName = 'streetName'; |
224
|
14 |
|
if ($prefix !== null) { |
225
|
11 |
|
$tagName = $prefix . ':' . $tagName; |
226
|
11 |
|
} |
227
|
14 |
|
$address->appendChild( |
228
|
14 |
|
$document->createElement( |
229
|
14 |
|
$tagName, |
230
|
14 |
|
$this->getStreetName() |
231
|
14 |
|
) |
232
|
14 |
|
); |
233
|
14 |
|
} |
234
|
14 |
|
if ($this->getNumber() !== null) { |
235
|
14 |
|
$tagName = 'number'; |
236
|
14 |
|
if ($prefix !== null) { |
237
|
11 |
|
$tagName = $prefix . ':' . $tagName; |
238
|
11 |
|
} |
239
|
14 |
|
$address->appendChild( |
240
|
14 |
|
$document->createElement( |
241
|
14 |
|
$tagName, |
242
|
14 |
|
$this->getNumber() |
243
|
14 |
|
) |
244
|
14 |
|
); |
245
|
14 |
|
} |
246
|
14 |
|
if ($this->getBox() !== null) { |
247
|
11 |
|
$tagName = 'box'; |
248
|
11 |
|
if ($prefix !== null) { |
249
|
8 |
|
$tagName = $prefix . ':' . $tagName; |
250
|
8 |
|
} |
251
|
11 |
|
$address->appendChild( |
252
|
11 |
|
$document->createElement( |
253
|
11 |
|
$tagName, |
254
|
11 |
|
$this->getBox() |
255
|
11 |
|
) |
256
|
11 |
|
); |
257
|
11 |
|
} |
258
|
14 |
|
if ($this->getPostalCode() !== null) { |
259
|
14 |
|
$tagName = 'postalCode'; |
260
|
14 |
|
if ($prefix !== null) { |
261
|
11 |
|
$tagName = $prefix . ':' . $tagName; |
262
|
11 |
|
} |
263
|
14 |
|
$address->appendChild( |
264
|
14 |
|
$document->createElement( |
265
|
14 |
|
$tagName, |
266
|
14 |
|
$this->getPostalCode() |
267
|
14 |
|
) |
268
|
14 |
|
); |
269
|
14 |
|
} |
270
|
14 |
|
if ($this->getLocality() !== null) { |
271
|
14 |
|
$tagName = 'locality'; |
272
|
14 |
|
if ($prefix !== null) { |
273
|
11 |
|
$tagName = $prefix . ':' . $tagName; |
274
|
11 |
|
} |
275
|
14 |
|
$address->appendChild( |
276
|
14 |
|
$document->createElement( |
277
|
14 |
|
$tagName, |
278
|
14 |
|
$this->getLocality() |
279
|
14 |
|
) |
280
|
14 |
|
); |
281
|
14 |
|
} |
282
|
14 |
|
if ($this->getCountryCode() !== null) { |
283
|
14 |
|
$tagName = 'countryCode'; |
284
|
14 |
|
if ($prefix !== null) { |
285
|
11 |
|
$tagName = $prefix . ':' . $tagName; |
286
|
11 |
|
} |
287
|
14 |
|
$address->appendChild( |
288
|
14 |
|
$document->createElement( |
289
|
14 |
|
$tagName, |
290
|
14 |
|
$this->getCountryCode() |
291
|
14 |
|
) |
292
|
14 |
|
); |
293
|
14 |
|
} |
294
|
|
|
|
295
|
14 |
|
return $address; |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* @param \SimpleXMLElement $xml |
300
|
|
|
* @return Address |
301
|
|
|
*/ |
302
|
4 |
|
public static function createFromXML(\SimpleXMLElement $xml) |
303
|
|
|
{ |
304
|
4 |
|
$address = new Address(); |
305
|
|
|
|
306
|
4 |
|
if (isset($xml->streetName) && $xml->streetName != '') { |
307
|
4 |
|
$address->setStreetName((string) $xml->streetName); |
308
|
4 |
|
} |
309
|
4 |
|
if (isset($xml->number) && $xml->number != '') { |
310
|
4 |
|
$address->setNumber((string) $xml->number); |
311
|
4 |
|
} |
312
|
4 |
|
if (isset($xml->box) && $xml->box != '') { |
313
|
2 |
|
$address->setBox((string) $xml->box); |
314
|
2 |
|
} |
315
|
4 |
|
if (isset($xml->postalCode) && $xml->postalCode != '') { |
316
|
4 |
|
$address->setPostalCode((string) $xml->postalCode); |
317
|
4 |
|
} |
318
|
4 |
|
if (isset($xml->locality) && $xml->locality != '') { |
319
|
4 |
|
$address->setLocality((string) $xml->locality); |
320
|
4 |
|
} |
321
|
4 |
|
if (isset($xml->countryCode) && $xml->countryCode != '') { |
322
|
4 |
|
$address->setCountryCode((string) $xml->countryCode); |
323
|
4 |
|
} |
324
|
|
|
|
325
|
4 |
|
return $address; |
326
|
|
|
} |
327
|
|
|
} |
328
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.