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

UnstructuredPostalAddress::sanitize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 9.4285
cc 1
eloc 5
nc 1
nop 3
crap 1
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