Passed
Pull Request — master (#16)
by Manuel
02:10
created

PaymentReference::setReference()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Sprain\SwissQrBill\DataGroup\Element;
4
5
use Sprain\SwissQrBill\Constraint\ValidCreditorReference;
6
use Sprain\SwissQrBill\DataGroup\QrCodeableInterface;
7
use Sprain\SwissQrBill\Validator\SelfValidatableInterface;
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, QrCodeableInterface, SelfValidatableInterface
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 static function create(string $type, ?string $reference = null): self
37
    {
38
        $paymentReference = new self();
39
        $paymentReference->type = $type;
40
        $paymentReference->reference = $reference;
41
42
        return $paymentReference;
43
    }
44
45
    public function getType(): ?string
46
    {
47
        return $this->type;
48
    }
49
50
    public function getReference(): ?string
51
    {
52
        return $this->reference;
53
    }
54
55
    public function getQrCodeData(): array
56
    {
57
        return [
58
            $this->getType(),
59
            $this->getReference()
60
        ];
61
    }
62
63
    public static function loadValidatorMetadata(ClassMetadata $metadata): void
64
    {
65
        $metadata->setGroupSequenceProvider(true);
66
67
        $metadata->addPropertyConstraints('type', [
68
            new Assert\NotBlank([
69
                'groups' => ['default']
70
            ]),
71
            new Assert\Choice([
72
                'groups' => ['default'],
73
                'choices' => [
74
                    self::TYPE_QR,
75
                    self::TYPE_SCOR,
76
                    self::TYPE_NON
77
                ]
78
            ])
79
        ]);
80
81
        $metadata->addPropertyConstraints('reference', [
82
            new Assert\Type([
83
                'type' => 'alnum',
84
                'groups' => [self::TYPE_QR]
85
            ]),
86
            new Assert\NotBlank([
87
                'groups' => [self::TYPE_QR, self::TYPE_SCOR]
88
            ]),
89
            new Assert\Length([
90
                'min' => 27,
91
                'max' => 27,
92
                'groups' => [self::TYPE_QR]
93
            ]),
94
            new Assert\Blank([
95
                'groups' => [self::TYPE_NON]
96
            ]),
97
            new ValidCreditorReference([
98
                'groups' => [self::TYPE_SCOR]
99
            ])
100
        ]);
101
    }
102
103
    public function getGroupSequence()
104
    {
105
        $groups = [
106
            'default',
107
            $this->getType()
108
        ];
109
110
        return $groups;
111
    }
112
}