FRAlgorithm::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
 * France
9
 */
10
class FRAlgorithm extends TINAlgorithm
11
{
12
    const LENGTH_1 = 13;
13
    const PATTERN_1 = "[0-3]\\d{12}";
14
15
    public function validate(string $tin)
16
    {
17
        $tin = str_replace(' ', '', $tin);
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
    public function isFollowLength(string $tin)
31
    {
32
        return StringUtil::isFollowLength($tin, self::LENGTH_1);
33
    }
34
35
    public function isFollowPattern(string $tin)
36
    {
37
        return StringUtil::isFollowPattern($tin, self::PATTERN_1);
38
    }
39
40
    public function isFollowRules(string $tin)
41
    {
42
        return $this->isFollowFranceRule1($tin);
43
    }
44
45
    public function isFollowFranceRule1(string $tin)
46
    {
47
        $number = floatval(StringUtil::substring($tin, 0, 10));
48
        $checkDigits = 0;
49
        $remainderBy511 = $number % 511;
50
        if ($remainderBy511 < 100) {
51
            if ($remainderBy511 < 10) {
52
                $checkDigits = intval(StringUtil::substring($tin, 12, 13));
53
            } else {
54
                $checkDigits = intval(StringUtil::substring($tin, 11, 13));
55
            }
56
        } else {
57
            $checkDigits = intval(StringUtil::substring($tin, 10, 13));
58
        }
59
        return $remainderBy511 == $checkDigits;
60
    }
61
}
62