1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Z38\SwissPayment; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* This class holds a unstructured representation of a postal address |
7
|
|
|
*/ |
8
|
|
|
class UnstructuredPostalAddress implements PostalAddressInterface |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var array |
12
|
|
|
*/ |
13
|
|
|
protected $addressLines; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected $country; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Constructor |
22
|
|
|
* |
23
|
|
|
* @param string $addressLine1 Street name and house number |
24
|
|
|
* @param string $addressLine2 Postcode and town |
25
|
|
|
* @param string $country Country code (ISO 3166-1 alpha-2) |
26
|
|
|
*/ |
27
|
3 |
|
public function __construct($addressLine1 = null, $addressLine2 = null, $country = 'CH') |
28
|
|
|
{ |
29
|
3 |
|
$this->addressLines = []; |
30
|
3 |
|
if ($addressLine1 !== null) { |
31
|
3 |
|
$this->addressLines[] = Text::assert($addressLine1, 70); |
32
|
3 |
|
} |
33
|
3 |
|
if ($addressLine2 !== null) { |
34
|
3 |
|
$this->addressLines[] = Text::assert($addressLine2, 70); |
35
|
3 |
|
} |
36
|
3 |
|
$this->country = Text::assertCountryCode($country); |
37
|
3 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Creates a new instance after sanitizing all inputs |
41
|
|
|
* |
42
|
|
|
* @param string $addressLine1 Street name and house number |
43
|
|
|
* @param string $addressLine2 Postcode and town |
44
|
|
|
* @param string $country Country code (ISO 3166-1 alpha-2) |
45
|
|
|
*/ |
46
|
1 |
|
public static function sanitize($addressLine1 = null, $addressLine2 = null, $country = 'CH') |
47
|
|
|
{ |
48
|
1 |
|
return new self( |
49
|
1 |
|
Text::sanitizeOptional($addressLine1, 70), |
50
|
1 |
|
Text::sanitizeOptional($addressLine2, 70), |
51
|
1 |
|
Text::sanitizeCountryCode($country) |
52
|
1 |
|
); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritdoc} |
57
|
|
|
*/ |
58
|
2 |
|
public function asDom(\DOMDocument $doc) |
59
|
|
|
{ |
60
|
2 |
|
$root = $doc->createElement('PstlAdr'); |
61
|
|
|
|
62
|
2 |
|
$root->appendChild(Text::xml($doc, 'Ctry', $this->country)); |
63
|
2 |
|
foreach ($this->addressLines as $line) { |
64
|
2 |
|
$root->appendChild(Text::xml($doc, 'AdrLine', $line)); |
65
|
2 |
|
} |
66
|
|
|
|
67
|
2 |
|
return $root; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|