Header   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 34
c 0
b 0
f 0
dl 0
loc 88
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 3 1
A __construct() 0 5 1
A getVersion() 0 3 1
A getQrType() 0 3 1
A loadValidatorMetadata() 0 26 1
A getCoding() 0 3 1
A getQrCodeData() 0 6 1
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 Header implements QrCodeableInterface, SelfValidatableInterface
12
{
13
    use SelfValidatableTrait;
14
15
    public const QRTYPE_SPC = 'SPC';
16
    public const VERSION_0200 = '0200';
17
    public const CODING_LATIN = 1;
18
19
    /**
20
     * Unambiguous indicator for the Swiss QR code.
21
     */
22
    private string $qrType;
23
24
    /**
25
     * Version of the specifications (Implementation Guidelines) in use on
26
     * the date on which the Swiss QR code was created.
27
     * The first two positions indicate the main version, the following the
28
     * two positions the sub-version ("0200" for version 2.0).
29
     */
30
    private string $version;
31
32
    /**
33
     * Character set code
34
     */
35
    private int $coding;
36
37
    private function __construct(string $qrType, string $version, int $coding)
38
    {
39
        $this->qrType = $qrType;
40
        $this->version = $version;
41
        $this->coding = $coding;
42
    }
43
44
    public static function create(string $qrType, string $version, int $coding): self
45
    {
46
        return new self($qrType, $version, $coding);
47
    }
48
49
    public function getQrType(): string
50
    {
51
        return $this->qrType;
52
    }
53
54
    public function getVersion(): string
55
    {
56
        return $this->version;
57
    }
58
59
    public function getCoding(): int
60
    {
61
        return $this->coding;
62
    }
63
64
    public function getQrCodeData(): array
65
    {
66
        return [
67
            $this->getQrType(),
68
            $this->getVersion(),
69
            $this->getCoding()
70
        ];
71
    }
72
73
    public static function loadValidatorMetadata(ClassMetadata $metadata): void
74
    {
75
        // Fixed length, three-digit, alphanumeric
76
        $metadata->addPropertyConstraints('qrType', [
77
            new Assert\NotBlank(),
78
            new Assert\Regex([
79
                'pattern' => '/^[a-zA-Z0-9]{3}$/',
80
                'match' => true
81
            ])
82
        ]);
83
84
        // Fixed length, four-digit, numeric
85
        $metadata->addPropertyConstraints('version', [
86
            new Assert\NotBlank(),
87
            new Assert\Regex([
88
                'pattern' => '/^\d{4}$/',
89
                'match' => true
90
            ])
91
        ]);
92
93
        // One-digit, numeric
94
        $metadata->addPropertyConstraints('coding', [
95
            new Assert\NotBlank(),
96
            new Assert\Regex([
97
                'pattern' => '/^\d{1}$/',
98
                'match' => true
99
            ])
100
        ]);
101
    }
102
}
103