Completed
Push — escaping ( 4299fb...bd3fe3 )
by z38
03:39
created

StructuredPostalAddress::sanitize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
ccs 8
cts 8
cp 1
rs 9.4285
cc 1
eloc 7
nc 1
nop 5
crap 1
1
<?php
2
3
namespace Z38\SwissPayment;
4
5
/**
6
 * This class holds a structured representation of a postal address
7
 */
8
class StructuredPostalAddress implements PostalAddressInterface
9
{
10
    /**
11
     * @var string|null
12
     */
13
    protected $street;
14
15
    /**
16
     * @var string|null
17
     */
18
    protected $buildingNo;
19
20
    /**
21
     * @var string
22
     */
23
    protected $postCode;
24
25
    /**
26
     * @var string
27
     */
28
    protected $town;
29
30
    /**
31
     * @var string
32
     */
33
    protected $country;
34
35
    /**
36
     * Constructor
37
     *
38
     * @param string|null $street     Street name or null
39
     * @param string|null $buildingNo Building number or null
40
     * @param string      $postCode   Postal code
41
     * @param string      $town       Town name
42
     * @param string      $country    Country code (ISO 3166-1 alpha-2)
43
     */
44 3
    public function __construct($street, $buildingNo, $postCode, $town, $country = 'CH')
45
    {
46 3
        $this->street = Text::assertOptional($street, 70);
47 3
        $this->buildingNo = Text::assertOptional($buildingNo, 16);
48 3
        $this->postCode = Text::assert($postCode, 16);
49 3
        $this->town = Text::assert($town, 35);
50 3
        $this->country = Text::assertCountryCode($country);
51 3
    }
52
53
    /**
54
     * Creates a new instance after sanitizing all inputs
55
     *
56
     * @param string|null $street     Street name or null
57
     * @param string|null $buildingNo Building number or null
58
     * @param string      $postCode   Postal code
59
     * @param string      $town       Town name
60
     * @param string      $country    Country code (ISO 3166-1 alpha-2)
61
     */
62 1
    public static function sanitize($street, $buildingNo, $postCode, $town, $country = 'CH')
63
    {
64 1
        return new self(
65 1
            Text::sanitizeOptional($street, 70),
66 1
            Text::sanitizeOptional($buildingNo, 16),
67 1
            Text::sanitize($postCode, 16),
68 1
            Text::sanitize($town, 35),
69 1
            Text::sanitizeCountryCode($country)
70 1
        );
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76 2
    public function asDom(\DOMDocument $doc)
77
    {
78 2
        $root = $doc->createElement('PstlAdr');
79
80 2
        if ($this->street !== null) {
81 2
            $root->appendChild(Text::xml($doc, 'StrtNm', $this->street));
82 2
        }
83 2
        if ($this->buildingNo !== null) {
84 2
            $root->appendChild(Text::xml($doc, 'BldgNb', $this->buildingNo));
85 2
        }
86 2
        $root->appendChild(Text::xml($doc, 'PstCd', $this->postCode));
87 2
        $root->appendChild(Text::xml($doc, 'TwnNm', $this->town));
88 2
        $root->appendChild(Text::xml($doc, 'Ctry', $this->country));
89
90 2
        return $root;
91
    }
92
}
93