Passed
Push — master ( 464b52...db8f1f )
by Manuel
02:10
created

Address::getFullAddress()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 0
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Sprain\SwissQrBill\DataGroups;
4
5
use Sprain\SwissQrBill\DataGroups\Interfaces\QrCodeData;
6
use Sprain\SwissQrBill\Validator\Interfaces\Validatable;
7
use Sprain\SwissQrBill\Validator\ValidatorTrait;
8
use Symfony\Component\Validator\Constraints as Assert;
9
use Symfony\Component\Validator\Mapping\ClassMetadata;
10
11
class Address implements QrCodeData, Validatable
12
{
13
    use ValidatorTrait;
14
15
    /**
16
     * Name or company
17
     *
18
     * @var string
19
     */
20
    private $name;
21
22
    /**
23
     * Street / P.O. box of the creditor.
24
     *
25
     * May not include building or house number.
26
     *
27
     * @var string
28
     */
29
    private $street;
30
31
    /**
32
     * House number of the creditor
33
     *
34
     * @var string
35
     */
36
    private $houseNumber;
37
38
    /**
39
     * Postal code without county code
40
     *
41
     * @var string
42
     */
43
    private $postalCode;
44
45
    /**
46
     * City
47
     *
48
     * @var string
49
     */
50
    private $city;
51
52
    /**
53
     * Country (ISO 3166-1 alpha-2)
54
     *
55
     * @var string
56
     */
57
    private $country;
58
59
60
    public function getName(): ?string
61
    {
62
        return $this->name;
63
    }
64
65
    public function setName(string $name) : self
66
    {
67
        $this->name = $name;
68
69
        return $this;
70
    }
71
72
    public function getStreet(): ?string
73
    {
74
        return $this->street;
75
    }
76
77
    public function setStreet(string $street = null) : self
78
    {
79
        $this->street = $street;
80
81
        return $this;
82
    }
83
84
    public function getHouseNumber(): ?string
85
    {
86
        return $this->houseNumber;
87
    }
88
89
    public function setHouseNumber(string $houseNumber = null) : self
90
    {
91
        $this->houseNumber = $houseNumber;
92
93
        return $this;
94
    }
95
96
    public function getPostalCode(): ?string
97
    {
98
        return $this->postalCode;
99
    }
100
101
    public function setPostalCode(string $postalCode) : self
102
    {
103
        $this->postalCode = $postalCode;
104
105
        return $this;
106
    }
107
108
    public function getCity(): ?string
109
    {
110
        return $this->city;
111
    }
112
113
    public function setCity(string $city) : self
114
    {
115
        $this->city = $city;
116
117
        return $this;
118
    }
119
120
    public function getCountry(): ?string
121
    {
122
        return $this->country;
123
    }
124
125
    public function setCountry(string $country) : self
126
    {
127
        $this->country = strtoupper($country);
128
129
        return $this;
130
    }
131
132
    public function getFullAddress() : string
133
    {
134
        $address = $this->getName();
135
136
        if ($this->getStreet()) {
137
            $address .= "\n" . $this->getStreet();
138
139
            if ($this->getHouseNumber()) {
140
                $address .= " " . $this->getHouseNumber();
141
            }
142
        }
143
144
        $address .= sprintf("\n%s-%s %s", $this->getCountry(), $this->getPostalCode(), $this->getCity());
145
146
        return $address;
147
    }
148
149
    public function getQrCodeData() : array
150
    {
151
        return [
152
            $this->getName(),
153
            $this->getStreet(),
154
            $this->getHouseNumber(),
155
            $this->getPostalCode(),
156
            $this->getCity(),
157
            $this->getCountry()
158
        ];
159
    }
160
161
    public static function loadValidatorMetadata(ClassMetadata $metadata)
162
    {
163
        $metadata->addPropertyConstraints('name', [
164
            new Assert\NotBlank(),
165
            new Assert\Length([
166
                'max' => 70
167
            ])
168
        ]);
169
170
        $metadata->addPropertyConstraints('street', [
171
            new Assert\Length([
172
                'max' => 70
173
            ])
174
        ]);
175
176
        $metadata->addPropertyConstraints('houseNumber', [
177
            new Assert\Length([
178
                'max' => 16
179
            ])
180
        ]);
181
182
        $metadata->addPropertyConstraints('postalCode', [
183
            new Assert\NotBlank(),
184
            new Assert\Length([
185
                'max' => 16
186
            ])
187
        ]);
188
189
        $metadata->addPropertyConstraints('city', [
190
            new Assert\NotBlank(),
191
            new Assert\Length([
192
                'max' => 35
193
            ])
194
        ]);
195
196
        $metadata->addPropertyConstraints('country', [
197
            new Assert\NotBlank(),
198
            new Assert\Country()
199
        ]);
200
    }
201
}