LTAlgorithm::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\DateUtil;
6
use LeKoala\Tin\Util\StringUtil;
7
8
/**
9
 * Lithuania
10
 */
11
class LTAlgorithm extends TINAlgorithm
12
{
13
    const LENGTH = 11;
14
    const PATTERN = "[1-6]\\d{2}[0-1]\\d[0-3]\\d{5}";
15
16
    public function validate(string $tin)
17
    {
18
        if (!$this->isFollowLength($tin)) {
19
            return StatusCode::INVALID_LENGTH;
20
        }
21
        if (!$this->isFollowPattern($tin)) {
22
            return StatusCode::INVALID_PATTERN;
23
        }
24
        if (!$this->isFollowRules($tin)) {
25
            return StatusCode::INVALID_SYNTAX;
26
        }
27
        return StatusCode::VALID;
28
    }
29
30
    private function checkMonth(string $tin)
0 ignored issues
show
Unused Code introduced by
The method checkMonth() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
31
    {
32
        $month = intval(StringUtil::substring($tin, 3, 5));
33
        return $month > 0 && $month < 13;
34
    }
35
36
    private function checkDay(string $tin)
0 ignored issues
show
Unused Code introduced by
The method checkDay() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
37
    {
38
        $day = intval(StringUtil::substring($tin, 5, 7));
39
        return $day > 0 && $day < 32;
40
    }
41
42
    public function isFollowLength(string $tin)
43
    {
44
        return StringUtil::isFollowLength($tin, self::LENGTH);
45
    }
46
47
    public function isFollowPattern(string $tin)
48
    {
49
        return StringUtil::isFollowPattern($tin, self::PATTERN);
50
    }
51
52
    public function isFollowRules(string $tin)
53
    {
54
        $sum = 0;
55
        $c11 = intval(StringUtil::substring($tin, 10));
56
        for ($i = 0; $i < 10; $i++) {
57
            $sum += $this->multiplyAccordingToWeight(intval(StringUtil::substring($tin, $i, $i + 1)), $i);
58
        }
59
        $remainderBy11 = $sum % 11;
60
        if ($remainderBy11 != 10) {
61
            return $c11 == $remainderBy11;
62
        }
63
        $sum2 = 0;
64
        for ($j = 0; $j < 10; $j++) {
65
            $sum2 += $this->multiplyAccordingToWeight2(intval(StringUtil::substring($tin, $j, $j + 1)), $j);
66
        }
67
        $remainderBy11 = $sum2 % 11;
68
        if ($remainderBy11 == 10) {
69
            return $c11 == 0;
70
        }
71
        return $c11 == $remainderBy11;
72
    }
73
74
    protected function multiplyAccordingToWeight($val, $index)
75
    {
76
        switch ($index) {
77
            case 0:
78
                return $val * 1;
79
            case 1:
80
                return $val * 2;
81
            case 2:
82
                return $val * 3;
83
            case 3:
84
                return $val * 4;
85
            case 4:
86
                return $val * 5;
87
            case 5:
88
                return $val * 6;
89
            case 6:
90
                return $val * 7;
91
            case 7:
92
                return $val * 8;
93
            case 8:
94
                return $val * 9;
95
            case 9:
96
                return $val * 1;
97
            default:
98
                return -1;
99
        }
100
    }
101
102
    protected function multiplyAccordingToWeight2($val, $index)
103
    {
104
        switch ($index) {
105
            case 0:
106
                return $val * 3;
107
            case 1:
108
                return $val * 4;
109
            case 2:
110
                return $val * 5;
111
            case 3:
112
                return $val * 6;
113
            case 4:
114
                return $val * 7;
115
            case 5:
116
                return $val * 8;
117
            case 6:
118
                return $val * 9;
119
            case 7:
120
                return $val * 1;
121
            case 8:
122
                return $val * 2;
123
            case 9:
124
                return $val * 3;
125
            default:
126
                return -1;
127
        }
128
    }
129
130
    private function isValidDate(string $tin)
0 ignored issues
show
Unused Code introduced by
The method isValidDate() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
131
    {
132
        $day = intval(StringUtil::substring($tin, 5, 7));
133
        $month = intval(StringUtil::substring($tin, 3, 5));
134
        $year = intval(StringUtil::substring($tin, 1, 3));
135
        return DateUtil::validate(1900 + $year, $month, $day) || DateUtil::validate(2000 + $year, $month, $day);
136
    }
137
}
138