Conditions | 3 |
Paths | 3 |
Total Lines | 17 |
Code Lines | 8 |
Lines | 0 |
Ratio | 0 % |
Tests | 8 |
CRAP Score | 3 |
Changes | 0 |
1 | <?php |
||
37 | 134 | public static function calculateCheckDigit(string $number): string |
|
38 | { |
||
39 | 134 | if (!ctype_digit($number)) { |
|
40 | 5 | throw new LogicException( |
|
41 | 5 | "Number can only contain numerical characters, found '$number'" |
|
42 | ); |
||
43 | } |
||
44 | |||
45 | 129 | $sum = 0; |
|
46 | |||
47 | 129 | foreach (array_reverse(str_split($number)) as $pos => $digit) { |
|
48 | 129 | $sum += $digit * self::getWeight($pos, 2); |
|
49 | } |
||
50 | |||
51 | // Calculate check digit from remainder |
||
52 | 129 | return self::REMAINDER_TO_CHECK_DIGIT_MAP[11 - $sum % 11]; |
|
53 | } |
||
54 | |||
72 |