Completed
Pull Request — master (#12)
by Antonio
01:54
created

AbstractCalculator   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 161
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
c 1
b 0
f 0
lcom 1
cbo 0
dl 0
loc 161
ccs 11
cts 11
cp 1
rs 10

2 Methods

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