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

StructuredPostalAddress   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 89
ccs 26
cts 26
cp 1
rs 10
c 2
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A sanitize() 0 10 1
A asDom() 0 16 3
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
     * @throws \InvalidArgumentException When the address contains invalid characters or is too long.
45
     */
46 3
    public function __construct($street, $buildingNo, $postCode, $town, $country = 'CH')
47
    {
48 3
        $this->street = Text::assertOptional($street, 70);
49 3
        $this->buildingNo = Text::assertOptional($buildingNo, 16);
50 3
        $this->postCode = Text::assert($postCode, 16);
51 3
        $this->town = Text::assert($town, 35);
52 3
        $this->country = Text::assertCountryCode($country);
53 3
    }
54
55
    /**
56
     * Creates a new instance after sanitizing all inputs
57
     *
58
     * @param string|null $street     Street name or null
59
     * @param string|null $buildingNo Building number or null
60
     * @param string      $postCode   Postal code
61
     * @param string      $town       Town name
62
     * @param string      $country    Country code (ISO 3166-1 alpha-2)
63
     *
64
     * @return StructuredPostalAddress
65
     */
66 1
    public static function sanitize($street, $buildingNo, $postCode, $town, $country = 'CH')
67
    {
68 1
        return new self(
69 1
            Text::sanitizeOptional($street, 70),
70 1
            Text::sanitizeOptional($buildingNo, 16),
71 1
            Text::sanitize($postCode, 16),
72 1
            Text::sanitize($town, 35),
73
            $country
74 1
        );
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80 2
    public function asDom(\DOMDocument $doc)
81
    {
82 2
        $root = $doc->createElement('PstlAdr');
83
84 2
        if ($this->street !== null) {
85 2
            $root->appendChild(Text::xml($doc, 'StrtNm', $this->street));
86 2
        }
87 2
        if ($this->buildingNo !== null) {
88 2
            $root->appendChild(Text::xml($doc, 'BldgNb', $this->buildingNo));
89 2
        }
90 2
        $root->appendChild(Text::xml($doc, 'PstCd', $this->postCode));
91 2
        $root->appendChild(Text::xml($doc, 'TwnNm', $this->town));
92 2
        $root->appendChild(Text::xml($doc, 'Ctry', $this->country));
93
94 2
        return $root;
95
    }
96
}
97