Passed
Push — master ( 7bcc6f...9b4044 )
by Thanos
01:49
created

KadValidator::validate()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4.0119

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 10
cts 11
cp 0.9091
rs 9.6666
c 0
b 0
f 0
cc 4
nc 4
nop 2
crap 4.0119
1
<?php
2
3
namespace SymfonyGreekValidation;
4
5
use Symfony\Component\Validator\Constraint;
6
use Symfony\Component\Validator\ConstraintValidator;
7
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
8
9
class KadValidator extends ConstraintValidator
10
{
11 12
    public function validate($value, Constraint $constraint)
12
    {
13 12
        if (!$constraint instanceof Kad) {
14
            throw new UnexpectedTypeException($constraint, Kad::class);
15
        }
16
17 12
        if (empty($value)) {
18 2
            return;
19
        }
20
21 10
        if (!preg_match('/^([0-9]{2}\.){3}[0-9]{2}$/', $value)) {
22 5
            $this->context->buildViolation($constraint->message)
23 5
                ->setParameter('{{ string }}', $value)
24 5
                ->addViolation();
25
26 5
            return;
27
        }
28
    }
29
}