MTAlgorithm   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A validate() 0 10 3
A isFollowMaltaRule() 0 8 2
A isFollowLength() 0 3 1
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