Passed
Push — master ( af1755...f0cfea )
by Manuel
01:58
created

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