HRAlgorithm::isFollowRules()   A
last analyzed

Complexity

Conditions 5
Paths 9

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 10
c 1
b 0
f 0
nc 9
nop 1
dl 0
loc 13
rs 9.6111
1
<?php
2
3
namespace LeKoala\Tin\Algo;
4
5
use LeKoala\Tin\Util\StringUtil;
6
7
/**
8
 * Croatia
9
 */
10
class HRAlgorithm extends TINAlgorithm
11
{
12
    const LENGTH = 11;
13
    const PATTERN = "\\d{11}";
14
15
    public function validate(string $tin)
16
    {
17
        $str = StringUtil::clearString($tin);
18
        if (!StringUtil::isFollowLength($str, 11)) {
19
            return StatusCode::INVALID_LENGTH;
20
        }
21
        if (!StringUtil::isFollowPattern($str, self::PATTERN)) {
22
            return StatusCode::INVALID_PATTERN;
23
        }
24
        if (!$this->isFollowRules($str)) {
25
            return StatusCode::INVALID_SYNTAX;
26
        }
27
        return StatusCode::VALID;
28
    }
29
30
    public function isFollowRules(string $tin)
31
    {
32
        $sum = 0;
0 ignored issues
show
Unused Code introduced by
The assignment to $sum is dead and can be removed.
Loading history...
33
        $rest = 0;
34
        $sum = StringUtil::digitAt($tin, 0) + 10;
35
        for ($i = 1; $i < 11; $i++) {
36
            $rest = $sum % 10;
37
            $rest = (($rest == 0) ? 10 : $rest) * 2 % 11;
38
            $sum = $rest + StringUtil::digitAt($tin, $i);
39
        }
40
        $diff = 11 - $rest;
41
        $lastDigit = StringUtil::digitAt($tin, 10);
42
        return ($rest == 1 && $lastDigit == 0) || $lastDigit == $diff;
43
    }
44
}
45