Completed
Push — master ( 31cc79...dd89a0 )
by Manuel
05:17
created

PaymentReference   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 44
dl 0
loc 103
rs 10
c 0
b 0
f 0
wmc 7

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setReference() 0 5 1
A getGroupSequence() 0 8 1
A getQrCodeData() 0 5 1
A getType() 0 3 1
A setType() 0 5 1
A loadValidatorMetadata() 0 36 1
A getReference() 0 3 1
1
<?php
2
3
namespace Sprain\SwissQrBill\DataGroup;
4
5
use Sprain\SwissQrBill\Constraint\ValidCreditorReference;
6
use Sprain\SwissQrBill\DataGroup\Interfaces\QrCodeData;
7
use Sprain\SwissQrBill\Validator\Interfaces\SelfValidatable;
8
use Sprain\SwissQrBill\Validator\SelfValidatableTrait;
9
use Symfony\Component\Validator\Constraints as Assert;
10
use Symfony\Component\Validator\GroupSequenceProviderInterface;
11
use Symfony\Component\Validator\Mapping\ClassMetadata;
12
13
class PaymentReference implements GroupSequenceProviderInterface, QrCodeData, SelfValidatable
14
{
15
    use SelfValidatableTrait;
16
17
    const TYPE_QR = 'QRR';
18
    const TYPE_SCOR = 'SCOR';
19
    const TYPE_NON = 'NON';
20
21
    /**
22
     * Reference type
23
     *
24
     * @var string
25
     */
26
    private $type;
27
28
    /**
29
     * Structured reference number
30
     * Either a QR reference or a Creditor Reference (ISO 11649)
31
     *
32
     * @var string
33
     */
34
    private $reference;
35
36
    public function getType(): ?string
37
    {
38
        return $this->type;
39
    }
40
41
    public function setType(string $type) : self
42
    {
43
        $this->type = $type;
44
45
        return $this;
46
    }
47
48
    public function getReference(): ?string
49
    {
50
        return $this->reference;
51
    }
52
53
    public function setReference(string $reference = null) : self
54
    {
55
        $this->reference = $reference;
56
57
        return $this;
58
    }
59
60
    public function getQrCodeData() : array
61
    {
62
        return [
63
            $this->getType(),
64
            $this->getReference()
65
        ];
66
    }
67
68
    public static function loadValidatorMetadata(ClassMetadata $metadata)
69
    {
70
        $metadata->setGroupSequenceProvider(true);
71
72
        $metadata->addPropertyConstraints('type', [
73
            new Assert\NotBlank([
74
                'groups' => ['default']
75
            ]),
76
            new Assert\Choice([
77
                'groups' => ['default'],
78
                'choices' => [
79
                    self::TYPE_QR,
80
                    self::TYPE_SCOR,
81
                    self::TYPE_NON
82
                ]
83
            ])
84
        ]);
85
86
        $metadata->addPropertyConstraints('reference', [
87
            new Assert\Type([
88
                'type' => 'alnum',
89
                'groups' => [self::TYPE_QR]
90
            ]),
91
            new Assert\NotBlank([
92
                'groups' => [self::TYPE_QR, self::TYPE_SCOR]
93
            ]),
94
            new Assert\Length([
95
                'min' => 27,
96
                'max' => 27,
97
                'groups' => [self::TYPE_QR]
98
            ]),
99
            new Assert\Blank([
100
                'groups' => [self::TYPE_NON]
101
            ]),
102
            new ValidCreditorReference([
103
                'groups' => [self::TYPE_SCOR]
104
            ])
105
        ]);
106
    }
107
108
    public function getGroupSequence()
109
    {
110
        $groups = [
111
            'default',
112
            $this->getType()
113
        ];
114
115
        return $groups;
116
    }
117
}