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
|
|
|
class CombinedAddress implements AddressInterface, SelfValidatableInterface, QrCodeableInterface |
13
|
|
|
{ |
14
|
|
|
use SelfValidatableTrait; |
15
|
|
|
|
16
|
|
|
public const ADDRESS_TYPE = 'K'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Name or company |
20
|
|
|
*/ |
21
|
|
|
private string $name; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Address line 1 |
25
|
|
|
* |
26
|
|
|
* Street and building number or P.O. Box |
27
|
|
|
*/ |
28
|
|
|
private ?string $addressLine1; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Address line 2 |
32
|
|
|
* |
33
|
|
|
* Postal code and town |
34
|
|
|
*/ |
35
|
|
|
private string $addressLine2; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Country (ISO 3166-1 alpha-2) |
39
|
|
|
*/ |
40
|
|
|
private string $country; |
41
|
|
|
|
42
|
|
|
private function __construct( |
43
|
|
|
string $name, |
44
|
|
|
?string $addressLine1, |
45
|
|
|
string $addressLine2, |
46
|
|
|
string $country |
47
|
|
|
) { |
48
|
|
|
$this->name = $name; |
49
|
|
|
$this->addressLine1 = $addressLine1; |
50
|
|
|
$this->addressLine2 = $addressLine2; |
51
|
|
|
$this->country = strtoupper($country); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public static function create( |
55
|
|
|
string $name, |
56
|
|
|
?string $addressLine1, |
57
|
|
|
string $addressLine2, |
58
|
|
|
string $country |
59
|
|
|
): self { |
60
|
|
|
return new self( |
61
|
|
|
$name, |
62
|
|
|
$addressLine1, |
63
|
|
|
$addressLine2, |
64
|
|
|
$country |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function getName(): string |
69
|
|
|
{ |
70
|
|
|
return $this->name; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function getAddressLine1(): ?string |
74
|
|
|
{ |
75
|
|
|
return $this->addressLine1; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function getAddressLine2(): string |
79
|
|
|
{ |
80
|
|
|
return $this->addressLine2; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function getCountry(): string |
84
|
|
|
{ |
85
|
|
|
return $this->country; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function getFullAddress(): string |
89
|
|
|
{ |
90
|
|
|
$address = $this->getName(); |
91
|
|
|
|
92
|
|
|
if ($this->getAddressLine1()) { |
93
|
|
|
$address .= "\n" . $this->getAddressLine1(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
if (in_array($this->getCountry(), ['CH', 'FL'])) { |
97
|
|
|
$address .= "\n" . $this->getAddressLine2(); |
98
|
|
|
} else { |
99
|
|
|
$address .= sprintf("\n%s-%s", $this->getCountry(), $this->getAddressLine2()); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $address; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function getQrCodeData(): array |
106
|
|
|
{ |
107
|
|
|
return [ |
108
|
|
|
$this->getAddressLine2() ? self::ADDRESS_TYPE : '', |
109
|
|
|
$this->getName(), |
110
|
|
|
$this->getAddressLine1(), |
111
|
|
|
$this->getAddressLine2(), |
112
|
|
|
'', |
113
|
|
|
'', |
114
|
|
|
$this->getCountry() |
115
|
|
|
]; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public static function loadValidatorMetadata(ClassMetadata $metadata): void |
119
|
|
|
{ |
120
|
|
|
$metadata->addPropertyConstraints('name', [ |
121
|
|
|
new Assert\NotBlank(), |
122
|
|
|
new Assert\Length([ |
123
|
|
|
'max' => 70 |
124
|
|
|
]) |
125
|
|
|
]); |
126
|
|
|
|
127
|
|
|
$metadata->addPropertyConstraints('addressLine1', [ |
128
|
|
|
new Assert\Length([ |
129
|
|
|
'max' => 70 |
130
|
|
|
]) |
131
|
|
|
]); |
132
|
|
|
|
133
|
|
|
$metadata->addPropertyConstraints('addressLine2', [ |
134
|
|
|
new Assert\NotBlank(), |
135
|
|
|
new Assert\Length([ |
136
|
|
|
'max' => 70 |
137
|
|
|
]) |
138
|
|
|
]); |
139
|
|
|
|
140
|
|
|
$metadata->addPropertyConstraints('country', [ |
141
|
|
|
new Assert\NotBlank(), |
142
|
|
|
new Assert\Country() |
143
|
|
|
]); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|