MTAlgorithm::validate()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 10
rs 10
1
<?php
2
3
namespace LeKoala\Tin\Algo;
4
5
use LeKoala\Tin\Util\StringUtil;
6
7
/**
8
 * Malta
9
 */
10
class MTAlgorithm extends TINAlgorithm
11
{
12
    const LENGTH = 8;
13
14
    public function validate(string $tin)
15
    {
16
        $normalizedTIN = StringUtil::fillWith0UntilLength($tin, self::LENGTH);
17
        if (!$this->isFollowLength($normalizedTIN)) {
18
            return StatusCode::INVALID_LENGTH;
19
        }
20
        if (!$this->isFollowMaltaRule($normalizedTIN)) {
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 isFollowMaltaRule(string $tin)
32
    {
33
        $c8 = $tin[7];
34
        $valid = ['M', 'G', 'A', 'P', 'L', 'H', 'B', 'Z'];
35
        if (!in_array($c8, $valid)) {
36
            return false;
37
        }
38
        return true;
39
    }
40
}
41