Completed
Pull Request — master (#22)
by z38
03:08 queued 01:44
created

UnstructuredPostalAddress::sanitize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
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
     * @throws \InvalidArgumentException When the address contains invalid characters or is too long.
28
     */
29 3
    public function __construct($addressLine1 = null, $addressLine2 = null, $country = 'CH')
30
    {
31 3
        $this->addressLines = [];
32 3
        if ($addressLine1 !== null) {
33 3
            $this->addressLines[] = Text::assert($addressLine1, 70);
34 3
        }
35 3
        if ($addressLine2 !== null) {
36 3
            $this->addressLines[] = Text::assert($addressLine2, 70);
37 3
        }
38 3
        $this->country = Text::assertCountryCode($country);
39 3
    }
40
41
    /**
42
     * Creates a new instance after sanitizing all inputs
43
     *
44
     * @param string $addressLine1 Street name and house number
45
     * @param string $addressLine2 Postcode and town
46
     * @param string $country      Country code (ISO 3166-1 alpha-2)
47
     *
48
     * @return UnstructuredPostalAddress
49
     */
50 1
    public static function sanitize($addressLine1 = null, $addressLine2 = null, $country = 'CH')
51
    {
52 1
        return new self(
53 1
            Text::sanitizeOptional($addressLine1, 70),
54 1
            Text::sanitizeOptional($addressLine2, 70),
55
            $country
56 1
        );
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62 2
    public function asDom(\DOMDocument $doc)
63
    {
64 2
        $root = $doc->createElement('PstlAdr');
65
66 2
        $root->appendChild(Text::xml($doc, 'Ctry', $this->country));
67 2
        foreach ($this->addressLines as $line) {
68 2
            $root->appendChild(Text::xml($doc, 'AdrLine', $line));
69 2
        }
70
71 2
        return $root;
72
    }
73
}
74