StructuredAddress   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 190
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 16
eloc 74
c 1
b 0
f 0
dl 0
loc 190
rs 10

12 Methods

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