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