1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CodiceFiscale; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Codice Fiscale abstract calculator. |
7
|
|
|
* |
8
|
|
|
* @author Antonio Turdo <[email protected]> |
9
|
|
|
*/ |
10
|
|
|
abstract class AbstractCalculator { |
11
|
|
|
|
12
|
|
|
// women char |
13
|
|
|
const CHR_WOMEN = 'F'; |
14
|
|
|
|
15
|
|
|
// male char |
16
|
|
|
const CHR_MALE = 'M'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Array of all available months. |
20
|
|
|
*/ |
21
|
|
|
protected $months = array( |
22
|
|
|
'1' => 'A', |
23
|
|
|
'2' => 'B', |
24
|
|
|
'3' => 'C', |
25
|
|
|
'4' => 'D', |
26
|
|
|
'5' => 'E', |
27
|
|
|
'6' => 'H', |
28
|
|
|
'7' => 'L', |
29
|
|
|
'8' => 'M', |
30
|
|
|
'9' => 'P', |
31
|
|
|
'10' => 'R', |
32
|
|
|
'11' => 'S', |
33
|
|
|
'12' => 'T', |
34
|
|
|
); |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Array of all avaialable odd characters. |
38
|
|
|
*/ |
39
|
|
|
protected $odd = array( |
40
|
|
|
'0' => 1, |
41
|
|
|
'1' => 0, |
42
|
|
|
'2' => 5, |
43
|
|
|
'3' => 7, |
44
|
|
|
'4' => 9, |
45
|
|
|
'5' => 13, |
46
|
|
|
'6' => 15, |
47
|
|
|
'7' => 17, |
48
|
|
|
'8' => 19, |
49
|
|
|
'9' => 21, |
50
|
|
|
'A' => 1, |
51
|
|
|
'B' => 0, |
52
|
|
|
'C' => 5, |
53
|
|
|
'D' => 7, |
54
|
|
|
'E' => 9, |
55
|
|
|
'F' => 13, |
56
|
|
|
'G' => 15, |
57
|
|
|
'H' => 17, |
58
|
|
|
'I' => 19, |
59
|
|
|
'J' => 21, |
60
|
|
|
'K' => 2, |
61
|
|
|
'L' => 4, |
62
|
|
|
'M' => 18, |
63
|
|
|
'N' => 20, |
64
|
|
|
'O' => 11, |
65
|
|
|
'P' => 3, |
66
|
|
|
'Q' => 6, |
67
|
|
|
'R' => 8, |
68
|
|
|
'S' => 12, |
69
|
|
|
'T' => 14, |
70
|
|
|
'U' => 16, |
71
|
|
|
'V' => 10, |
72
|
|
|
'W' => 22, |
73
|
|
|
'X' => 25, |
74
|
|
|
'Y' => 24, |
75
|
|
|
'Z' => 23, |
76
|
|
|
); |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Array of all avaialable even characters. |
80
|
|
|
*/ |
81
|
|
|
protected $even = array( |
82
|
|
|
'0' => 0, |
83
|
|
|
'1' => 1, |
84
|
|
|
'2' => 2, |
85
|
|
|
'3' => 3, |
86
|
|
|
'4' => 4, |
87
|
|
|
'5' => 5, |
88
|
|
|
'6' => 6, |
89
|
|
|
'7' => 7, |
90
|
|
|
'8' => 8, |
91
|
|
|
'9' => 9, |
92
|
|
|
'A' => 0, |
93
|
|
|
'B' => 1, |
94
|
|
|
'C' => 2, |
95
|
|
|
'D' => 3, |
96
|
|
|
'E' => 4, |
97
|
|
|
'F' => 5, |
98
|
|
|
'G' => 6, |
99
|
|
|
'H' => 7, |
100
|
|
|
'I' => 8, |
101
|
|
|
'J' => 9, |
102
|
|
|
'K' => 10, |
103
|
|
|
'L' => 11, |
104
|
|
|
'M' => 12, |
105
|
|
|
'N' => 13, |
106
|
|
|
'O' => 14, |
107
|
|
|
'P' => 15, |
108
|
|
|
'Q' => 16, |
109
|
|
|
'R' => 17, |
110
|
|
|
'S' => 18, |
111
|
|
|
'T' => 19, |
112
|
|
|
'U' => 20, |
113
|
|
|
'V' => 21, |
114
|
|
|
'W' => 22, |
115
|
|
|
'X' => 23, |
116
|
|
|
'Y' => 24, |
117
|
|
|
'Z' => 25, |
118
|
|
|
); |
119
|
|
|
|
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Array of all avaialable omocodia characters. |
123
|
|
|
*/ |
124
|
|
|
protected $omocodiaCodes = array( |
125
|
|
|
'0' => 'L', |
126
|
|
|
'1' => 'M', |
127
|
|
|
'2' => 'N', |
128
|
|
|
'3' => 'P', |
129
|
|
|
'4' => 'Q', |
130
|
|
|
'5' => 'R', |
131
|
|
|
'6' => 'S', |
132
|
|
|
'7' => 'T', |
133
|
|
|
'8' => 'U', |
134
|
|
|
'9' => 'V', |
135
|
|
|
); |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Calculate the sum by the given dictionary for the given temporary codice fiscale. |
139
|
|
|
* |
140
|
|
|
* @param $temporaryCodiceFiscale The temporary codice fiscale. |
141
|
|
|
* @param $dictionaryArray The dictionary array. |
142
|
|
|
* @param $i The start index value. |
143
|
|
|
* @returns Returns the sum by the given dictionary for the given temporary codice fiscale. |
144
|
|
|
*/ |
145
|
|
|
protected function calculateSumByDictionary($temporaryCodiceFiscale, $dictionaryArray, $i) |
146
|
|
|
{ |
147
|
|
|
$sum = 0; |
148
|
|
|
for (; $i < 15; $i = $i + 2) { |
149
|
|
|
$k = $temporaryCodiceFiscale{$i}; |
150
|
|
|
$sum = $sum + $dictionaryArray[$k]; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return $sum; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Calculate the check digit. |
158
|
|
|
* |
159
|
|
|
* @param $temporaryCodiceFiscale The first part of the codice fiscale. |
160
|
|
|
* @returns Returns the check digit part of the codice fiscale. |
161
|
|
|
*/ |
162
|
|
|
protected function calculateCheckDigit($temporaryCodiceFiscale) |
163
|
|
|
{ |
164
|
|
|
$sumEven = $this->calculateSumByDictionary($temporaryCodiceFiscale, $this->even, 1); |
165
|
|
|
$sumOdd = $this->calculateSumByDictionary($temporaryCodiceFiscale, $this->odd, 0); |
166
|
|
|
|
167
|
|
|
return chr(($sumOdd + $sumEven) % 26 + 65); |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|