|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LeKoala\Tin\Algo; |
|
4
|
|
|
|
|
5
|
|
|
use LeKoala\Tin\Util\DateUtil; |
|
6
|
|
|
use LeKoala\Tin\Util\StringUtil; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Belgium |
|
10
|
|
|
*/ |
|
11
|
|
|
class BEAlgorithm extends TINAlgorithm |
|
12
|
|
|
{ |
|
13
|
|
|
const LENGTH = 11; |
|
14
|
|
|
const PATTERN = "\\d{2}[0-1]\\d[0-3]\\d{6}"; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var integer |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $resultDateValidation; |
|
20
|
|
|
|
|
21
|
|
|
public function validate(string $tin) |
|
22
|
|
|
{ |
|
23
|
|
|
$str = StringUtil::clearString($tin); |
|
24
|
|
|
if (!$this->isFollowLength($str)) { |
|
25
|
|
|
return StatusCode::INVALID_LENGTH; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
$this->resultDateValidation = $this->isValidDate($str); |
|
29
|
|
|
if (!$this->isFollowPattern($str) || $this->resultDateValidation == 0) { |
|
30
|
|
|
return StatusCode::INVALID_PATTERN; |
|
31
|
|
|
} |
|
32
|
|
|
if (!$this->isFollowRules($str)) { |
|
33
|
|
|
return StatusCode::INVALID_SYNTAX; |
|
34
|
|
|
} |
|
35
|
|
|
return StatusCode::VALID; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @return integer |
|
41
|
|
|
*/ |
|
42
|
|
|
public function getResultDateValidation() |
|
43
|
|
|
{ |
|
44
|
|
|
return $this->resultDateValidation; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param integer $resultDateValidation |
|
49
|
|
|
*/ |
|
50
|
|
|
public function setResultDateValidation($resultDateValidation) |
|
51
|
|
|
{ |
|
52
|
|
|
$this->resultDateValidation = $resultDateValidation; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function isFollowRules(string $tin) |
|
56
|
|
|
{ |
|
57
|
|
|
return $this->isFollowBelgiumRule1AndIsDateValid($tin) || $this->isFollowBelgiumRule2AndIsDateValid($tin); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function isFollowBelgiumRule1AndIsDateValid(string $tin) |
|
61
|
|
|
{ |
|
62
|
|
|
$dateValid = ($this->resultDateValidation == 1 || $this->resultDateValidation == 3); |
|
63
|
|
|
return $this->isFollowBelgiumRule1($tin) && $dateValid; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function isFollowBelgiumRule2AndIsDateValid(string $tin) |
|
67
|
|
|
{ |
|
68
|
|
|
$dateValid = $this->resultDateValidation >= 2; |
|
69
|
|
|
return $this->isFollowBelgiumRule2($tin) && $dateValid; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function isFollowLength(string $tin) |
|
73
|
|
|
{ |
|
74
|
|
|
return StringUtil::isFollowLength($tin, self::LENGTH); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function isFollowPattern(string $tin) |
|
78
|
|
|
{ |
|
79
|
|
|
return StringUtil::isFollowPattern($tin, self::PATTERN); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function isFollowBelgiumRule1(string $tin) |
|
83
|
|
|
{ |
|
84
|
|
|
$divisionRemainderBy97 = intval(StringUtil::substring($tin, 0, 9)) % 97; |
|
85
|
|
|
|
|
86
|
|
|
return 97 - $divisionRemainderBy97 == intval(StringUtil::substring($tin, 9, 11)); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function isFollowBelgiumRule2(string $tin) |
|
90
|
|
|
{ |
|
91
|
|
|
$divisionRemainderBy97 = floatval('2' . StringUtil::substring($tin, 0, 9)) % 97; |
|
92
|
|
|
return 97 - $divisionRemainderBy97 == intval(StringUtil::substring($tin, 9, 11)); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @param string $tin |
|
97
|
|
|
* @return integer |
|
98
|
|
|
*/ |
|
99
|
|
|
private function isValidDate(string $tin) |
|
100
|
|
|
{ |
|
101
|
|
|
$year = intval(StringUtil::substring($tin, 0, 2)); |
|
102
|
|
|
$month = intval(StringUtil::substring($tin, 2, 4)); |
|
103
|
|
|
$day = intval(StringUtil::substring($tin, 4, 6)); |
|
104
|
|
|
|
|
105
|
|
|
$y1 = DateUtil::validate(1900 + $year, $month, $day); |
|
106
|
|
|
$y2 = DateUtil::validate(2000 + $year, $month, $day); |
|
107
|
|
|
if ($day == 0 || $month == 0 || $y1 && $y2) { |
|
108
|
|
|
return 3; |
|
109
|
|
|
} |
|
110
|
|
|
if ($y1) { |
|
111
|
|
|
return 1; |
|
112
|
|
|
} |
|
113
|
|
|
if ($y2) { |
|
114
|
|
|
return 2; |
|
115
|
|
|
} |
|
116
|
|
|
return 0; |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|