BGAlgorithm   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 36
c 1
b 0
f 0
dl 0
loc 70
rs 10
wmc 15

6 Methods

Rating   Name   Duplication   Size   Complexity  
A validate() 0 12 5
A isFollowBulgariaRule() 0 18 2
A isFollowRules() 0 3 1
A isValidDate() 0 12 5
A isFollowLength() 0 3 1
A isFollowPattern() 0 3 1
1
<?php
2
3
namespace LeKoala\Tin\Algo;
4
5
use LeKoala\Tin\Util\DateUtil;
6
use LeKoala\Tin\Util\StringUtil;
7
8
/**
9
 * Bulgaria
10
 */
11
class BGAlgorithm extends TINAlgorithm
12
{
13
    const LENGTH = 10;
14
    const PATTERN = "\\d{10}";
15
16
    public function validate(string $tin)
17
    {
18
        if (!$this->isFollowLength($tin)) {
19
            return StatusCode::INVALID_LENGTH;
20
        }
21
        if (!$this->isFollowPattern($tin) || !$this->isValidDate($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
    public function isFollowLength(string $tin)
31
    {
32
        return StringUtil::isFollowLength($tin, self::LENGTH);
33
    }
34
35
    public function isFollowPattern(string $tin)
36
    {
37
        return StringUtil::isFollowPattern($tin, self::PATTERN);
38
    }
39
40
    public function isFollowRules(string $tin)
41
    {
42
        return $this->isFollowBulgariaRule($tin);
43
    }
44
45
    public function isFollowBulgariaRule(string $tin)
46
    {
47
        $c1 = StringUtil::digitAt($tin, 0);
48
        $c2 = StringUtil::digitAt($tin, 1);
49
        $c3 = StringUtil::digitAt($tin, 2);
50
        $c4 = StringUtil::digitAt($tin, 3);
51
        $c5 = StringUtil::digitAt($tin, 4);
52
        $c6 = StringUtil::digitAt($tin, 5);
53
        $c7 = StringUtil::digitAt($tin, 6);
54
        $c8 = StringUtil::digitAt($tin, 7);
55
        $c9 = StringUtil::digitAt($tin, 8);
56
        $c10 = StringUtil::digitAt($tin, 9);
57
        $sum = $c1 * 2 + $c2 * 4 + $c3 * 8 + $c4 * 5 + $c5 * 10 + $c6 * 9 + $c7 * 7 + $c8 * 3 + $c9 * 6;
58
        $remainderBy11 = $sum % 11;
59
        if ($remainderBy11 == 10) {
60
            return $c10 == 0;
61
        }
62
        return $remainderBy11 == $c10;
63
    }
64
65
    /**
66
     * @param string $tin
67
     * @return boolean
68
     */
69
    private function isValidDate(string $tin)
70
    {
71
        $year = intval(StringUtil::substring($tin, 0, 2));
72
        $month = intval(StringUtil::substring($tin, 2, 4));
73
        $day = intval(StringUtil::substring($tin, 4, 6));
74
        if ($month >= 21 && $month <= 32) {
75
            return DateUtil::validate(1800 + $year, $month - 20, $day);
76
        }
77
        if ($month >= 41 && $month <= 52) {
78
            return DateUtil::validate(2000 + $year, $month - 40, $day);
79
        }
80
        return DateUtil::validate(1900 + $year, $month, $day);
81
    }
82
}
83