1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Sprain\SwissQrBill\DataGroup\Element; |
4
|
|
|
|
5
|
|
|
use Sprain\SwissQrBill\DataGroup\QrCodeableInterface; |
6
|
|
|
use Sprain\SwissQrBill\Validator\SelfValidatableInterface; |
7
|
|
|
use Sprain\SwissQrBill\Validator\SelfValidatableTrait; |
8
|
|
|
use Symfony\Component\Validator\Constraints as Assert; |
9
|
|
|
use Symfony\Component\Validator\Mapping\ClassMetadata; |
10
|
|
|
|
11
|
|
|
final class PaymentAmountInformation implements QrCodeableInterface, SelfValidatableInterface |
12
|
|
|
{ |
13
|
|
|
use SelfValidatableTrait; |
14
|
|
|
|
15
|
|
|
public const CURRENCY_CHF = 'CHF'; |
16
|
|
|
public const CURRENCY_EUR = 'EUR'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The payment amount due |
20
|
|
|
*/ |
21
|
|
|
private ?float $amount; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Payment currency code (ISO 4217) |
25
|
|
|
*/ |
26
|
|
|
private string $currency; |
27
|
|
|
|
28
|
|
|
private function __construct(string $currency, ?float $amount) |
29
|
|
|
{ |
30
|
|
|
$this->currency = strtoupper($currency); |
31
|
|
|
$this->amount = $amount; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public static function create(string $currency, ?float $amount = null): self |
35
|
|
|
{ |
36
|
|
|
return new self($currency, $amount); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function getAmount(): ?float |
40
|
|
|
{ |
41
|
|
|
return $this->amount; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function getFormattedAmount(): ?string |
45
|
|
|
{ |
46
|
|
|
if (null === $this->amount) { |
47
|
|
|
return ''; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return number_format( |
51
|
|
|
$this->amount, |
52
|
|
|
2, |
53
|
|
|
'.', |
54
|
|
|
' ' |
55
|
|
|
); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function getCurrency(): string |
59
|
|
|
{ |
60
|
|
|
return $this->currency; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function getQrCodeData(): array |
64
|
|
|
{ |
65
|
|
|
if (null !== $this->getAmount()) { |
66
|
|
|
$amountOutput = number_format($this->getAmount(), 2, '.', ''); |
67
|
|
|
} else { |
68
|
|
|
$amountOutput = null; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return [ |
72
|
|
|
$amountOutput, |
73
|
|
|
$this->getCurrency() |
74
|
|
|
]; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public static function loadValidatorMetadata(ClassMetadata $metadata): void |
78
|
|
|
{ |
79
|
|
|
$metadata->addPropertyConstraints('amount', [ |
80
|
|
|
new Assert\Range([ |
81
|
|
|
'min' => 0, |
82
|
|
|
'max'=> 999999999.99 |
83
|
|
|
]), |
84
|
|
|
]); |
85
|
|
|
|
86
|
|
|
$metadata->addPropertyConstraints('currency', [ |
87
|
|
|
new Assert\Choice([ |
88
|
|
|
self::CURRENCY_CHF, |
89
|
|
|
self::CURRENCY_EUR |
90
|
|
|
]) |
91
|
|
|
]); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|