Passed
Push — master ( af81ee...d4daf1 )
by Manuel
38s queued 11s
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\ClassMetadata;
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
    public static function create(string $qrType, string $version, int $coding): self
44
    {
45
        $header = new self();
46
        $header->coding = $coding;
47
        $header->qrType = $qrType;
48
        $header->version = $version;
49
50
        return $header;
51
    }
52
53
    public function getQrType(): ?string
54
    {
55
        return $this->qrType;
56
    }
57
58
    public function getVersion(): ?string
59
    {
60
        return $this->version;
61
    }
62
63
    public function getCoding(): ?int
64
    {
65
        return $this->coding;
66
    }
67
68
    public function getQrCodeData(): array
69
    {
70
        return [
71
            $this->getQrType(),
72
            $this->getVersion(),
73
            $this->getCoding()
74
        ];
75
    }
76
77
    public static function loadValidatorMetadata(ClassMetadata $metadata): void
78
    {
79
        // Fixed length, three-digit, alphanumeric
80
        $metadata->addPropertyConstraints('qrType', [
81
            new Assert\NotBlank(),
82
            new Assert\Regex([
83
                'pattern' => '/^[a-zA-Z0-9]{3}$/',
84
                'match' => true
85
            ])
86
        ]);
87
88
        // Fixed length, four-digit, numeric
89
        $metadata->addPropertyConstraints('version', [
90
            new Assert\NotBlank(),
91
            new Assert\Regex([
92
                'pattern' => '/^\d{4}$/',
93
                'match' => true
94
            ])
95
        ]);
96
97
        // One-digit, numeric
98
        $metadata->addPropertyConstraints('coding', [
99
            new Assert\NotBlank(),
100
            new Assert\Regex([
101
                'pattern' => '/^\d{1}$/',
102
                'match' => true
103
            ])
104
        ]);
105
    }
106
}