forked from ch2877/swiss-payment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StructuredPostalAddress.php
99 lines (87 loc) · 2.69 KB
/
StructuredPostalAddress.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
namespace Z38\SwissPayment;
use DOMDocument;
use InvalidArgumentException;
/**
* This class holds a structured representation of a postal address
*/
class StructuredPostalAddress implements PostalAddressInterface
{
/**
* @var string|null
*/
protected $street;
/**
* @var string|null
*/
protected $buildingNo;
/**
* @var string
*/
protected $postCode;
/**
* @var string
*/
protected $town;
/**
* @var string
*/
protected $country;
/**
* Constructor
*
* @param string|null $street Street name or null
* @param string|null $buildingNo Building number or null
* @param string $postCode Postal code
* @param string $town Town name
* @param string $country Country code (ISO 3166-1 alpha-2)
*
* @throws InvalidArgumentException When the address contains invalid characters or is too long.
*/
public function __construct($street, $buildingNo, $postCode, $town, $country = 'CH')
{
$this->street = Text::assertOptional($street, 70);
$this->buildingNo = Text::assertOptional($buildingNo, 16);
$this->postCode = Text::assert($postCode, 16);
$this->town = Text::assert($town, 35);
$this->country = Text::assertCountryCode($country);
}
/**
* Creates a new instance after sanitizing all inputs
*
* @param string|null $street Street name or null
* @param string|null $buildingNo Building number or null
* @param string $postCode Postal code
* @param string $town Town name
* @param string $country Country code (ISO 3166-1 alpha-2)
*
* @return StructuredPostalAddress
*/
public static function sanitize($street, $buildingNo, $postCode, $town, $country = 'CH')
{
return new self(
Text::sanitizeOptional($street, 70),
Text::sanitizeOptional($buildingNo, 16),
Text::sanitize($postCode, 16),
Text::sanitize($town, 35),
$country
);
}
/**
* {@inheritdoc}
*/
public function asDom(DOMDocument $doc)
{
$root = $doc->createElement('PstlAdr');
if ($this->street !== null) {
$root->appendChild(Text::xml($doc, 'StrtNm', $this->street));
}
if ($this->buildingNo !== null) {
$root->appendChild(Text::xml($doc, 'BldgNb', $this->buildingNo));
}
$root->appendChild(Text::xml($doc, 'PstCd', $this->postCode));
$root->appendChild(Text::xml($doc, 'TwnNm', $this->town));
$root->appendChild(Text::xml($doc, 'Ctry', $this->country));
return $root;
}
}