Completed
Pull Request — master (#22)
by z38
05:31 queued 03:38
created

StructuredPostalAddress   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

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

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 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