Passed
Pull Request — master (#16)
by Manuel
02:01
created

StructuredAddress::getStreet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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