forked from z38/swiss-payment
-
Notifications
You must be signed in to change notification settings - Fork 6
/
UnstructuredPostalAddress.php
76 lines (67 loc) · 2.03 KB
/
UnstructuredPostalAddress.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
<?php
namespace Z38\SwissPayment;
use DOMDocument;
use InvalidArgumentException;
/**
* This class holds an unstructured representation of a postal address
*/
class UnstructuredPostalAddress implements PostalAddressInterface
{
/**
* @var array
*/
protected $addressLines;
/**
* @var string
*/
protected $country;
/**
* Constructor
*
* @param string $addressLine1 Street name and house number
* @param string $addressLine2 Postcode and town
* @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($addressLine1 = null, $addressLine2 = null, $country = 'CH')
{
$this->addressLines = [];
if ($addressLine1 !== null) {
$this->addressLines[] = Text::assert($addressLine1, 70);
}
if ($addressLine2 !== null) {
$this->addressLines[] = Text::assert($addressLine2, 70);
}
$this->country = Text::assertCountryCode($country);
}
/**
* Creates a new instance after sanitizing all inputs
*
* @param string $addressLine1 Street name and house number
* @param string $addressLine2 Postcode and town
* @param string $country Country code (ISO 3166-1 alpha-2)
*
* @return UnstructuredPostalAddress
*/
public static function sanitize($addressLine1 = null, $addressLine2 = null, $country = 'CH')
{
return new self(
Text::sanitizeOptional($addressLine1, 70),
Text::sanitizeOptional($addressLine2, 70),
$country
);
}
/**
* {@inheritdoc}
*/
public function asDom(DOMDocument $doc)
{
$root = $doc->createElement('PstlAdr');
$root->appendChild(Text::xml($doc, 'Ctry', $this->country));
foreach ($this->addressLines as $line) {
$root->appendChild(Text::xml($doc, 'AdrLine', $line));
}
return $root;
}
}