HUAlgorithm::isFollowPattern()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace LeKoala\Tin\Algo;
4
5
use LeKoala\Tin\Util\StringUtil;
6
7
/**
8
 * Hungary
9
 */
10
class HUAlgorithm extends TINAlgorithm
11
{
12
    const LENGTH = 10;
13
    const PATTERN = "8\\d{9}";
14
15
    public function validate(string $tin)
16
    {
17
        if (!$this->isFollowLength($tin)) {
18
            return StatusCode::INVALID_LENGTH;
19
        }
20
        if (!$this->isFollowPattern($tin)) {
21
            return StatusCode::INVALID_PATTERN;
22
        }
23
        if (!$this->isFollowRules($tin)) {
24
            return StatusCode::INVALID_SYNTAX;
25
        }
26
        return StatusCode::VALID;
27
    }
28
29
    public function isFollowLength(string $tin)
30
    {
31
        return StringUtil::isFollowLength($tin, self::LENGTH);
32
    }
33
34
    public function isFollowPattern(string $tin)
35
    {
36
        return StringUtil::isFollowPattern($tin, self::PATTERN);
37
    }
38
39
    public function isFollowRules(string $tin)
40
    {
41
        $c10 = StringUtil::digitAt($tin, 9);
42
        $sum = 0;
43
        for ($i = 0; $i < 9; $i++) {
44
            $c11 = intval(StringUtil::substring($tin, $i, $i + 1));
45
            $sum += $c11 * ($i + 1);
46
        }
47
        $remainderBy11 = $sum % 11;
48
        return $remainderBy11 == $c10;
49
    }
50
}
51