|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Sprain\SwissQrBill\Reference; |
|
4
|
|
|
|
|
5
|
|
|
use kmukku\phpIso11649\phpIso11649; |
|
6
|
|
|
use Sprain\SwissQrBill\String\StringModifier; |
|
7
|
|
|
use Sprain\SwissQrBill\Validator\Exception\InvalidCreditorReferenceException; |
|
8
|
|
|
use Sprain\SwissQrBill\Validator\SelfValidatableInterface; |
|
9
|
|
|
use Sprain\SwissQrBill\Validator\SelfValidatableTrait; |
|
10
|
|
|
use Symfony\Component\Validator\Constraints as Assert; |
|
11
|
|
|
use Symfony\Component\Validator\Mapping\ClassMetadata; |
|
12
|
|
|
|
|
13
|
|
|
final class RfCreditorReferenceGenerator implements SelfValidatableInterface |
|
14
|
|
|
{ |
|
15
|
|
|
use SelfValidatableTrait; |
|
16
|
|
|
|
|
17
|
|
|
private string $reference; |
|
18
|
|
|
|
|
19
|
|
|
public static function generate(string $reference): string |
|
20
|
|
|
{ |
|
21
|
|
|
$generator = new self($reference); |
|
22
|
|
|
|
|
23
|
|
|
return $generator->doGenerate(); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function __construct(string $reference) |
|
27
|
|
|
{ |
|
28
|
|
|
$this->reference = StringModifier::stripWhitespace($reference); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function doGenerate(): string |
|
32
|
|
|
{ |
|
33
|
|
|
if (!$this->isValid()) { |
|
34
|
|
|
throw new InvalidCreditorReferenceException( |
|
35
|
|
|
'The provided data is not valid to generate a creditor reference.' |
|
36
|
|
|
); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
$generator = new phpIso11649(); |
|
40
|
|
|
|
|
41
|
|
|
return $generator->generateRfReference($this->reference, false); |
|
|
|
|
|
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public static function loadValidatorMetadata(ClassMetadata $metadata): void |
|
45
|
|
|
{ |
|
46
|
|
|
$metadata->addPropertyConstraints('reference', [ |
|
47
|
|
|
new Assert\Regex([ |
|
48
|
|
|
'pattern' => '/^[a-zA-Z0-9]*$/', |
|
49
|
|
|
'match' => true |
|
50
|
|
|
]), |
|
51
|
|
|
new Assert\Length([ |
|
52
|
|
|
'min' => 1, |
|
53
|
|
|
'max' => 21 // 25 minus 'RF' prefix minus 2-digit check sum |
|
54
|
|
|
]), |
|
55
|
|
|
new Assert\NotBlank() |
|
56
|
|
|
]); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|