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

UnstructuredPostalAddress   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 64
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 3
A sanitize() 0 8 1
A asDom() 0 11 2
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 1
    public static function sanitize($addressLine1 = null, $addressLine2 = null, $country = 'CH')
49
    {
50 1
        return new self(
51 1
            Text::sanitizeOptional($addressLine1, 70),
52 1
            Text::sanitizeOptional($addressLine2, 70),
53
            $country
54 1
        );
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60 2
    public function asDom(\DOMDocument $doc)
61
    {
62 2
        $root = $doc->createElement('PstlAdr');
63
64 2
        $root->appendChild(Text::xml($doc, 'Ctry', $this->country));
65 2
        foreach ($this->addressLines as $line) {
66 2
            $root->appendChild(Text::xml($doc, 'AdrLine', $line));
67 2
        }
68
69 2
        return $root;
70
    }
71
}
72