1 | <?php |
||
10 | class Calculator extends AbstractCalculator |
||
11 | { |
||
12 | private $subject; |
||
13 | private $omocodiaLevel = 0; |
||
14 | |||
15 | /** |
||
16 | * Array of available vowels. |
||
17 | */ |
||
18 | private $vowels = array('A', 'E', 'I', 'O', 'U'); |
||
19 | |||
20 | /** |
||
21 | * Create a Codice Fiscale instance. |
||
22 | * |
||
23 | * @param Subject $subject The subject that will have the codice fiscale. |
||
24 | * @param $properties An array with additional properties. |
||
25 | */ |
||
26 | 22 | public function __construct(Subject $subject, $properties = array()) |
|
27 | { |
||
28 | 22 | $this->subject = $subject; |
|
29 | |||
30 | 22 | if (array_key_exists('omocodiaLevel', $properties)) { |
|
31 | 20 | $this->omocodiaLevel = $properties['omocodiaLevel']; |
|
32 | 20 | } |
|
33 | 22 | } |
|
34 | |||
35 | /** |
||
36 | * Calculate the code fiscale. |
||
37 | * |
||
38 | * @returns Returns the complete codice fiscale. |
||
39 | */ |
||
40 | 22 | public function calculate() |
|
48 | |||
49 | /** |
||
50 | * Calculate all possibilities for the code fiscale. |
||
51 | * |
||
52 | * @returns Returns the complete codice fiscale. |
||
53 | */ |
||
54 | 3 | public function calculateAllPossibilities() |
|
55 | { |
||
56 | 3 | $allPossibilities = array(); |
|
57 | 3 | for ($i = 0; $i < 128; ++$i) { |
|
58 | 3 | $this->omocodiaLevel = $i; |
|
59 | 3 | $allPossibilities[] = $this->calculate(); |
|
60 | 3 | } |
|
61 | |||
62 | 3 | return $allPossibilities; |
|
63 | } |
||
64 | |||
65 | /** |
||
66 | * Calculate the surname part of the codice fiscale. |
||
67 | * |
||
68 | * @returns Returns the surname part of the codice fiscale. |
||
69 | */ |
||
70 | 22 | private function calculateSurname() |
|
71 | { |
||
72 | 22 | $consonants = str_replace($this->vowels, '', strtoupper($this->subject->getSurname())); |
|
73 | 22 | $consonants = $this->cleanString($consonants); |
|
74 | 22 | if (strlen($consonants) > 2) { |
|
75 | 18 | $result = substr($consonants, 0, 3); |
|
76 | 18 | } else { |
|
77 | 4 | $result = $this->calculateSmallString($consonants, $this->subject->getSurname()); |
|
78 | } |
||
79 | |||
80 | 22 | return $result; |
|
81 | } |
||
82 | |||
83 | /** |
||
84 | * Calculate the name part of the codice fiscale. |
||
85 | * |
||
86 | * @returns Returns the name part of the codice fiscale. |
||
87 | */ |
||
88 | 22 | private function calculateName() |
|
89 | { |
||
90 | 22 | $consonants = str_replace($this->vowels, '', strtoupper($this->subject->getName())); |
|
91 | 22 | $consonants = $this->cleanString($consonants); |
|
92 | 22 | if (strlen($consonants) > 3) { |
|
93 | 6 | $result = $consonants[0].$consonants[2].$consonants[3]; |
|
94 | 22 | } elseif (strlen($consonants) == 3) { |
|
95 | 1 | $result = $consonants; |
|
96 | 1 | } else { |
|
97 | 15 | $result = $this->calculateSmallString($consonants, $this->subject->getName()); |
|
98 | } |
||
99 | |||
100 | 22 | return $result; |
|
101 | } |
||
102 | |||
103 | /** |
||
104 | * Calculate small string for the given parameters (used by name and surname). |
||
105 | * |
||
106 | * @param $consonants A consonants string. |
||
107 | * @param $string The small string. |
||
108 | * @returns Returns the calculated result for the small string. |
||
109 | */ |
||
110 | 15 | private function calculateSmallString($consonants, $string) |
|
118 | |||
119 | /** |
||
120 | * Calculate the birth date and the gender. |
||
121 | * |
||
122 | * @returns Returns the birth date and gender part of the codice fiscale. |
||
123 | */ |
||
124 | 22 | private function calculateBirthDateAndGender() |
|
125 | { |
||
126 | 22 | $year = $this->subject->getBirthDate()->format('y'); |
|
127 | 22 | $month = $this->months[$this->subject->getBirthDate()->format('n')]; |
|
128 | 22 | $day = $this->subject->getBirthDate()->format('d'); |
|
129 | 22 | if (strtoupper($this->subject->getGender()) == self::CHR_WOMEN) { |
|
130 | 2 | $day += 40; |
|
131 | 2 | } |
|
132 | |||
133 | 22 | return $year.$month.$day; |
|
134 | } |
||
135 | |||
136 | /** |
||
137 | * Calculate the Belfiore code. |
||
138 | * |
||
139 | * @returns Returns the Belfiore code. |
||
140 | */ |
||
141 | 22 | private function calculateBelfioreCode() |
|
145 | |||
146 | /** |
||
147 | * Calculate the omocodia case (additional translation). |
||
148 | * |
||
149 | * @param $temporaryCodiceFiscale The first part of the codice fiscale. |
||
150 | * @returns Returns the new codice fiscale. |
||
151 | */ |
||
152 | 22 | private function calculateOmocodia($temporaryCodiceFiscale) |
|
153 | { |
||
154 | 22 | if ($this->omocodiaLevel > 0) { |
|
155 | 7 | if ($this->omocodiaLevel) { |
|
156 | 7 | $temporaryCodiceFiscale = $this->calculateOmocodiaSection(2, $temporaryCodiceFiscale, 1, 1, 14); |
|
157 | 7 | $temporaryCodiceFiscale = $this->calculateOmocodiaSection(4, $temporaryCodiceFiscale, 2, 3, 13); |
|
158 | 7 | $temporaryCodiceFiscale = $this->calculateOmocodiaSection(8, $temporaryCodiceFiscale, 4, 7, 12); |
|
159 | 7 | $temporaryCodiceFiscale = $this->calculateOmocodiaSection(16, $temporaryCodiceFiscale, 8, 15, 10); |
|
160 | 7 | $temporaryCodiceFiscale = $this->calculateOmocodiaSection(32, $temporaryCodiceFiscale, 16, 31, 9); |
|
161 | 7 | $temporaryCodiceFiscale = $this->calculateOmocodiaSection(64, $temporaryCodiceFiscale, 32, 63, 7); |
|
162 | 7 | $temporaryCodiceFiscale = $this->calculateOmocodiaSection(128, $temporaryCodiceFiscale, 64, 127, 6); |
|
163 | 7 | } |
|
164 | 7 | } |
|
165 | |||
166 | 22 | return $temporaryCodiceFiscale; |
|
167 | } |
||
168 | |||
169 | /** |
||
170 | * Calculate a section of the omocodia. |
||
171 | * |
||
172 | * @param $divider The divider. |
||
173 | * @param $temporaryCodiceFiscale The first part of the codice fiscale on which make the substitutions. |
||
174 | * @param $startingIndex The starting index. |
||
175 | * @param $endingIndex The ending index. |
||
176 | * @param $characterIndex The index to use to make the substitutions on the $temporaryCodiceFiscale. |
||
177 | * @returns Returns the temporary codice fiscale with the substitutions made. |
||
178 | */ |
||
179 | 7 | private function calculateOmocodiaSection($divider, $temporaryCodiceFiscale, $startingIndex, $endingIndex, $characterIndex) |
|
180 | { |
||
181 | 7 | if ($this->omocodiaLevel % $divider >= $startingIndex && $this->omocodiaLevel % $divider <= $endingIndex) { |
|
182 | 7 | $k = $temporaryCodiceFiscale{$characterIndex}; |
|
183 | 7 | $newChar = $this->omocodiaCodes[$k]; |
|
184 | 7 | $temporaryCodiceFiscale{$characterIndex} = $newChar; |
|
185 | 7 | } |
|
186 | 7 | return $temporaryCodiceFiscale; |
|
187 | } |
||
188 | |||
189 | /** |
||
190 | * Clean the string removing some characters. |
||
191 | * |
||
192 | * @param $string The string to clean. |
||
193 | * @returns Returns a clean string. |
||
194 | */ |
||
195 | 22 | private function cleanString($string) |
|
199 | } |
||
200 |