1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Luilliarcec\LaravelEcuadorIdentification\Support\Identifications; |
5
|
|
|
|
6
|
|
|
use Luilliarcec\LaravelEcuadorIdentification\Contracts\IdentificationContract; |
7
|
|
|
use Luilliarcec\LaravelEcuadorIdentification\Exceptions\EcuadorIdentificationException; |
8
|
|
|
|
9
|
|
|
class EcuadorValidations |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Number of provinces of Ecuador |
13
|
|
|
* |
14
|
|
|
* @var \Illuminate\Config\Repository |
15
|
|
|
*/ |
16
|
|
|
protected $provinces; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Length of the different types of identification |
20
|
|
|
* |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
protected $lenght; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Billing code for identification types |
27
|
|
|
* |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
protected $billingCode; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* EcuadorValidations constructor. |
34
|
|
|
*/ |
35
|
|
|
public function __construct() |
36
|
|
|
{ |
37
|
|
|
$this->provinces = config('laravel-ecuador-identification.provinces'); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Validate length identification, province code, third digit |
42
|
|
|
* |
43
|
|
|
* @param $number |
44
|
|
|
* @param IdentificationContract $type |
45
|
|
|
* @throws EcuadorIdentificationException |
46
|
|
|
*/ |
47
|
|
|
protected function validateInitial($number, IdentificationContract $type) |
48
|
|
|
{ |
49
|
|
|
try { |
50
|
|
|
$this->validateLength($number, (int)$this->lenght); |
51
|
|
|
$this->validateProvinceCode(substr($number, 0, 2)); |
52
|
|
|
$this->validateThirdDigit($number[2], $type); |
53
|
|
|
} catch (EcuadorIdentificationException $e) { |
54
|
|
|
throw new EcuadorIdentificationException($e->getMessage()); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Initial validation of the identification, not empty, only digits, not less than the given length. |
60
|
|
|
* |
61
|
|
|
* @param string $value CI or RUC |
62
|
|
|
* @param int $len Number of characters required |
63
|
|
|
* @return bool |
64
|
|
|
* @throws EcuadorIdentificationException When the value is empty, when the value isn't digits and |
65
|
|
|
* when the value doesn't have the required length |
66
|
|
|
*/ |
67
|
|
|
private function validateLength($value, $len) |
68
|
|
|
{ |
69
|
|
|
if (empty($value)) { |
70
|
|
|
throw new EcuadorIdentificationException('Field must have a value.'); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if (!ctype_digit($value)) { |
74
|
|
|
throw new EcuadorIdentificationException('Must be digits.'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if (strlen($value) != $len) { |
78
|
|
|
throw new EcuadorIdentificationException("Must be {$len} digits."); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return true; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Validate the province code (first two numbers of CI/RUC) |
86
|
|
|
* The first 2 positions correspond to the province where it was issued, |
87
|
|
|
* so the first two numbers will not be greater than 24 or less than 1 |
88
|
|
|
* |
89
|
|
|
* @param string $value First two numbers of CI/RUC |
90
|
|
|
* @return boolean |
91
|
|
|
* @throws EcuadorIdentificationException When the province code is not between 1 and 24 |
92
|
|
|
*/ |
93
|
|
|
private function validateProvinceCode($value) |
94
|
|
|
{ |
95
|
|
|
if ($value < 1 || $value > $this->provinces) { |
96
|
|
|
throw new EcuadorIdentificationException("In your province code must be between 01 and {$this->provinces}."); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return true; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Valid the third digit |
104
|
|
|
* |
105
|
|
|
* It allows the third digit of the document to be valid. |
106
|
|
|
* Depending on the type field (type of identification) validations are performed. |
107
|
|
|
* |
108
|
|
|
* NATURAL_PERSON |
109
|
|
|
* For Certificates and RUC of natural persons the third digit is less than 6 so |
110
|
|
|
* it must be between 0 and 5 (0,1,2,3,4,5) |
111
|
|
|
* |
112
|
|
|
* PRIVATE_COMPANY |
113
|
|
|
* For RUC of private companies the third digit must be equal to 9. |
114
|
|
|
* |
115
|
|
|
* PUBLIC_COMPANY |
116
|
|
|
* For RUC of public companies the third digit must be equal to 6. |
117
|
|
|
* |
118
|
|
|
* @param string $value Third digit of CI/RUC |
119
|
|
|
* @param IdentificationContract $type Type of identifier |
120
|
|
|
* @return boolean |
121
|
|
|
* @throws EcuadorIdentificationException When it does not comply with the validation according to the type of identifier |
122
|
|
|
*/ |
123
|
|
|
private function validateThirdDigit($value, IdentificationContract $type) |
124
|
|
|
{ |
125
|
|
|
if ($type instanceof NaturalRuc || $type instanceof PersonalIdentification) { |
126
|
|
|
$min = config('laravel-ecuador-identification.personal-identification.third-digit.min'); |
127
|
|
|
$max = config('laravel-ecuador-identification.personal-identification.third-digit.max'); |
128
|
|
|
|
129
|
|
|
if ($value < $min || $value > $max) |
130
|
|
|
throw new EcuadorIdentificationException("Field must have the third digit between {$min} and {$max}."); |
131
|
|
|
|
132
|
|
|
return true; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
if ($type instanceof PrivateRuc) { |
136
|
|
|
$thirdDigit = config('laravel-ecuador-identification.private-ruc.third-digit'); |
137
|
|
|
} elseif ($type instanceof PublicRuc) { |
138
|
|
|
$thirdDigit = config('laravel-ecuador-identification.public-ruc.third-digit'); |
139
|
|
|
} else { |
140
|
|
|
throw new EcuadorIdentificationException('Field does not have this type of identification.'); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
if ($value != $thirdDigit) |
144
|
|
|
throw new EcuadorIdentificationException("Field must have the third digit equal to {$thirdDigit}."); |
145
|
|
|
|
146
|
|
|
return true; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Validation of the last digits |
151
|
|
|
* |
152
|
|
|
* Public Ruc => 0001 |
153
|
|
|
* Other Ruc => 001 |
154
|
|
|
* |
155
|
|
|
* @param string $value The last digits |
156
|
|
|
* @param IdentificationContract $type Type of identifier |
157
|
|
|
* @return boolean |
158
|
|
|
* @throws EcuadorIdentificationException When not equal to 001 |
159
|
|
|
*/ |
160
|
|
|
protected function validateLastDigits($value, IdentificationContract $type) |
161
|
|
|
{ |
162
|
|
|
$lastDigits = null; |
|
|
|
|
163
|
|
|
|
164
|
|
|
if ($type instanceof NaturalRuc) { |
165
|
|
|
$lastDigits = config('laravel-ecuador-identification.natural-ruc.last-digits'); |
166
|
|
|
} elseif ($type instanceof PrivateRuc) { |
167
|
|
|
$lastDigits = config('laravel-ecuador-identification.private-ruc.last-digits'); |
168
|
|
|
} elseif ($type instanceof PublicRuc) { |
169
|
|
|
$lastDigits = config('laravel-ecuador-identification.public-ruc.last-digits'); |
170
|
|
|
} else { |
171
|
|
|
throw new EcuadorIdentificationException('Field does not have this type of identification.'); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
if ($value != $lastDigits) { |
175
|
|
|
throw new EcuadorIdentificationException("Field does not have the last digits equal to {$lastDigits}"); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
return true; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Module 10 Algorithm to validate if Certificates and RUC of natural person are valid. |
183
|
|
|
* |
184
|
|
|
* Coefficients used to validate the tenth digit of the Certificates, |
185
|
|
|
* are: 2, 1, 2, 1, 2, 1, 2, 1, 2 |
186
|
|
|
* |
187
|
|
|
* Step 1: Multiply each digit of the card by the coefficient, |
188
|
|
|
* except for the verification digit (tenth digit), |
189
|
|
|
* if it is greater than 10 sums between digits. Example: |
190
|
|
|
* |
191
|
|
|
* 2 1 2 1 2 1 2 1 2 (Coefficients) |
192
|
|
|
* 1 7 1 0 0 3 4 0 6 (Certificate) |
193
|
|
|
* 2 7 2 0 0 3 8 0 [12] => continue to step 2. |
194
|
|
|
* |
195
|
|
|
* Step 2: If any of the multiplication results is greater than 10, |
196
|
|
|
* it is added between digits of the result. Example: [12] => 1 + 2 = Result (3) |
197
|
|
|
* |
198
|
|
|
* Step 3: The result of the multiplications is added. Example: |
199
|
|
|
* 2 7 2 0 0 3 8 0 3 = Result (25) |
200
|
|
|
* |
201
|
|
|
* Step 4: The result of the sum is divided by 10 and the remainder of the division is obtained |
202
|
|
|
* If the remainder is 0 the check digit is 0 |
203
|
|
|
* Otherwise, the residue is subtracted from 10 |
204
|
|
|
* |
205
|
|
|
* If the result is equal to the verification digit, the value is correct. |
206
|
|
|
* |
207
|
|
|
* @param string $number Certificates or RUC of natural person |
208
|
|
|
* @return boolean |
209
|
|
|
* @throws EcuadorIdentificationException The verified digit does not match the verification digit. |
210
|
|
|
*/ |
211
|
|
|
protected function moduleTen($number) |
212
|
|
|
{ |
213
|
|
|
$check_digit_position = config('laravel-ecuador-identification.personal-identification.check-digit-position'); |
214
|
|
|
$coefficients = config('laravel-ecuador-identification.personal-identification.coefficients'); |
215
|
|
|
|
216
|
|
|
$check_digit_value = $number[$check_digit_position - 1]; |
217
|
|
|
$numbers = str_split(substr($number, 0, 9)); |
218
|
|
|
|
219
|
|
|
$total = 0; |
220
|
|
|
|
221
|
|
|
foreach ($numbers as $key => $value) { |
222
|
|
|
$proceeds = ($value * $coefficients[$key]); |
223
|
|
|
|
224
|
|
|
if ($proceeds >= 10) { |
225
|
|
|
$proceeds = str_split($proceeds); |
226
|
|
|
$proceeds = array_sum($proceeds); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
$total += $proceeds; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
$residue = $total % 10; |
233
|
|
|
|
234
|
|
|
$verified_digit_value = $residue == 0 ? 0 : 10 - $residue; |
235
|
|
|
|
236
|
|
|
if ($verified_digit_value != $check_digit_value) { |
237
|
|
|
throw new EcuadorIdentificationException('Field is invalid'); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
return true; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Module 11 Algorithm to validate if RUC of Public Companies and Private Companies are valid. |
245
|
|
|
* |
246
|
|
|
* For Public Companies (Third Digit => [6]): |
247
|
|
|
* => The verifier digit is the ninth digit. |
248
|
|
|
* => Coefficients used to validate the ninth digit of the Public Company RUC, (Third digit = [6]) |
249
|
|
|
* are: 3, 2, 7, 6, 5, 4, 3, 2 |
250
|
|
|
* |
251
|
|
|
* |
252
|
|
|
* For Private Companies (Third Digit => [9]): |
253
|
|
|
* => The verifier digit is the tenth digit. |
254
|
|
|
* => Coefficients used to validate the tenth digit of the Private Company RUC, (Third digit = [9]) |
255
|
|
|
* are: 4, 3, 2, 7, 6, 5, 4, 3, 2 |
256
|
|
|
* |
257
|
|
|
* Step 1: Multiply each digit of the RUC with its respective coefficient, |
258
|
|
|
* except the verification digit. Example: |
259
|
|
|
* |
260
|
|
|
* Public Companies |
261
|
|
|
* 3 2 7 6 5 4 3 2 (Coefficients) |
262
|
|
|
* 1 7 6 0 0 0 1 0 [4] 0 0 0 1 (Public RUC) [4] => Ninth Digit (Check Digit) |
263
|
|
|
* 3 14 42 0 0 0 3 0 => Multiplication Result |
264
|
|
|
* |
265
|
|
|
* Private Companies |
266
|
|
|
* 4 3 2 7 6 5 4 3 2 (Coefficients) |
267
|
|
|
* 1 7 9 0 0 8 5 7 8 [3] 0 0 1 (Private RUC) [3] => Tenth Digit (Check Digit) |
268
|
|
|
* 4 21 18 0 0 40 20 21 16 => Multiplication Result |
269
|
|
|
* |
270
|
|
|
* Step 2: The multiplication results are added |
271
|
|
|
* |
272
|
|
|
* Public Companies |
273
|
|
|
* 3 14 42 0 0 0 3 0 = Result (62) |
274
|
|
|
* |
275
|
|
|
* * Private Companies |
276
|
|
|
* 4 21 18 0 0 40 20 21 16 = Result (140) |
277
|
|
|
* |
278
|
|
|
* Step 3: The result of the sum is divided to 11 and the remainder of the division is obtained. |
279
|
|
|
* If the remainder is 0 the check digit is 0 |
280
|
|
|
* Otherwise, the residue is subtracted from 11 |
281
|
|
|
* |
282
|
|
|
* If the result is equal to the verification digit, the value is correct. |
283
|
|
|
* |
284
|
|
|
* @param string $number Private Company RUC or Public Compnay RUC |
285
|
|
|
* @param IdentificationContract $type Type of identifier |
286
|
|
|
* @return boolean |
287
|
|
|
* @throws EcuadorIdentificationException The verified digit does not match the verification digit. |
288
|
|
|
*/ |
289
|
|
|
protected function moduleEleven($number, IdentificationContract $type) |
290
|
|
|
{ |
291
|
|
|
if ($type instanceof PrivateRuc) { |
292
|
|
|
$coefficients = config('laravel-ecuador-identification.private-ruc.coefficients'); |
293
|
|
|
$check_digit_position = config('laravel-ecuador-identification.private-ruc.check-digit-position'); |
294
|
|
|
$check_digit_value = $number[$check_digit_position - 1]; |
295
|
|
|
$numbers = str_split(substr($number, 0, 9)); |
296
|
|
|
} elseif ($type instanceof PublicRuc) { |
297
|
|
|
$coefficients = config('laravel-ecuador-identification.public-ruc.coefficients'); |
298
|
|
|
$check_digit_position = config('laravel-ecuador-identification.public-ruc.check-digit-position'); |
299
|
|
|
$check_digit_value = $number[$check_digit_position - 1]; |
300
|
|
|
$numbers = str_split(substr($number, 0, 8)); |
301
|
|
|
} else { |
302
|
|
|
throw new EcuadorIdentificationException('Field does not have this type of identification.'); |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
$total = 0; |
306
|
|
|
|
307
|
|
|
foreach ($numbers as $key => $value) { |
308
|
|
|
$proceeds = ($value * $coefficients[$key]); |
309
|
|
|
$total += $proceeds; |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
$residue = $total % 11; |
313
|
|
|
|
314
|
|
|
$verified_digit_value = $residue == 0 ? 0 : 11 - $residue; |
315
|
|
|
|
316
|
|
|
if ($verified_digit_value != $check_digit_value) { |
317
|
|
|
throw new EcuadorIdentificationException('Field is invalid'); |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
return true; |
321
|
|
|
} |
322
|
|
|
} |
323
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.