ROAlgorithm   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 24
c 1
b 0
f 0
dl 0
loc 58
rs 10
wmc 14

5 Methods

Rating   Name   Duplication   Size   Complexity  
A validate() 0 9 3
A isValidDate() 0 12 3
A isFollowLength() 0 3 1
A isFollowPattern() 0 3 2
A isFollowRomanianRule() 0 15 5
1
<?php
2
3
namespace LeKoala\Tin\Algo;
4
5
use LeKoala\Tin\Util\DateUtil;
6
use LeKoala\Tin\Util\StringUtil;
7
8
/**
9
 * Romania
10
 */
11
class ROAlgorithm extends TINAlgorithm
12
{
13
    const LENGTH = 13;
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
        return StatusCode::VALID;
24
    }
25
26
    public function isFollowLength(string $tin)
27
    {
28
        return StringUtil::isFollowLength($tin, self::LENGTH);
29
    }
30
31
    public function isFollowPattern(string $tin)
32
    {
33
        return $this->isValidDate($tin) && $this->isFollowRomanianRule($tin);
34
    }
35
36
    private function isFollowRomanianRule(string $tin)
37
    {
38
        $c1 = intval($tin[0]);
39
40
        if ($c1 == 0) {
41
            return false;
42
        }
43
44
        $county = intval(StringUtil::substring($tin, 7, 9));
45
46
        if ($county > 47 && $county != 51 && $county != 52) {
47
            return false;
48
        }
49
50
        return true;
51
    }
52
53
    /**
54
     * @param string $tin
55
     * @return boolean
56
     */
57
    private function isValidDate(string $tin)
58
    {
59
        $year = intval(StringUtil::substring($tin, 1, 3));
60
        $month = intval(StringUtil::substring($tin, 3, 5));
61
        $day = intval(StringUtil::substring($tin, 5, 7));
62
63
        $y1 = DateUtil::validate(1900 + $year, $month, $day);
64
        $y2 = DateUtil::validate(2000 + $year, $month, $day);
65
        if (!$y1 || !$y2) {
66
            return false;
67
        }
68
        return true;
69
    }
70
}
71