Conditions | 8 |
Paths | 10 |
Total Lines | 33 |
Code Lines | 18 |
Lines | 0 |
Ratio | 0 % |
Tests | 18 |
CRAP Score | 8 |
Changes | 0 |
1 | <?php |
||
10 | 21 | public function validate($value, Constraint $constraint) |
|
11 | { |
||
12 | 21 | if (empty($value)) { |
|
13 | 2 | return true; |
|
14 | } |
||
15 | |||
16 | 19 | if (!preg_match('/^[0-9]{11}$/', $value) || $value === '00000000000') { |
|
17 | 3 | $this->buildViolation($value, $constraint); |
|
18 | |||
19 | 3 | return false; |
|
20 | } |
||
21 | |||
22 | 16 | $sum = 0; |
|
23 | 16 | foreach(str_split($value) as $index => $char) { |
|
24 | 16 | $tempDigit = intval($char); |
|
25 | 16 | if ($this->isOdd($index)) { |
|
26 | 16 | $tempDigit *= 2; |
|
27 | 16 | if ($tempDigit > 9) { |
|
28 | 15 | $tempDigit -= 9; |
|
29 | } |
||
30 | } |
||
31 | |||
32 | 16 | $sum += $tempDigit; |
|
33 | } |
||
34 | |||
35 | 16 | if ($sum % 10 !== 0) { |
|
36 | 2 | $this->buildViolation($value, $constraint); |
|
37 | |||
38 | 2 | return false; |
|
39 | } |
||
40 | |||
41 | 14 | return true; |
|
42 | } |
||
43 | |||
55 | } |