CZAlgorithm::isFollowLength1()   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
 * Czech Republic
10
 *
11
 * TODO: implement modulus check
12
 */
13
class CZAlgorithm extends TINAlgorithm
14
{
15
    const LENGTH_1 = 9;
16
    const LENGTH_2 = 10;
17
18
    public function validate(string $tin)
19
    {
20
        $normalizedTIN = str_replace("/", "", $tin);
21
        if (!$this->isFollowLength1($normalizedTIN) && !$this->isFollowLength2($normalizedTIN)) {
22
            return StatusCode::INVALID_LENGTH;
23
        }
24
        if (!$this->isValidDate($normalizedTIN)) {
25
            return StatusCode::INVALID_PATTERN;
26
        }
27
        return StatusCode::VALID;
28
    }
29
30
    public function isFollowLength1(string $tin)
31
    {
32
        return StringUtil::isFollowLength($tin, self::LENGTH_1);
33
    }
34
35
    public function isFollowLength2(string $tin)
36
    {
37
        return StringUtil::isFollowLength($tin, self::LENGTH_2);
38
    }
39
40
    /**
41
     * @param string $tin
42
     * @return boolean
43
     */
44
    private function isValidDate(string $tin)
45
    {
46
        $year = intval(StringUtil::substring($tin, 0, 2));
47
        $month = intval(StringUtil::substring($tin, 2, 4));
48
        // female have +50 in their month
49
        if ($month > 50) {
50
            $month = $month - 50;
51
        }
52
        // some people have +20 in their month
53
        if ($month > 12) {
54
            $month = $month - 20;
55
        }
56
        $day = intval(StringUtil::substring($tin, 4, 6));
57
58
        $y1 = DateUtil::validate(1900 + $year, $month, $day);
59
        $y2 = DateUtil::validate(2000 + $year, $month, $day);
60
        if (!$y1 || !$y2) {
61
            return false;
62
        }
63
        return true;
64
    }
65
}
66