ValidCreditorReferenceValidator::validate()   A
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 9
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 16
rs 9.6111
1
<?php declare(strict_types=1);
2
3
namespace Sprain\SwissQrBill\Constraint;
4
5
use kmukku\phpIso11649\phpIso11649;
6
use Symfony\Component\Validator\Constraint;
7
use Symfony\Component\Validator\ConstraintValidator;
8
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
9
10
/**
11
 * @internal
12
 */
13
final class ValidCreditorReferenceValidator extends ConstraintValidator
14
{
15
    public function validate($value, Constraint $constraint): void
16
    {
17
        if (!$constraint instanceof ValidCreditorReference) {
18
            throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\ValidCreditorReference');
19
        }
20
21
        if (null === $value || '' === $value) {
22
            return;
23
        }
24
25
        $referenceGenerator = new phpIso11649();
26
27
        if (false === $referenceGenerator->validateRfReference($value)) {
28
            $this->context->buildViolation($constraint->message)
29
                ->setParameter('{{ string }}', $value)
30
                ->addViolation();
31
        }
32
    }
33
}
34