Skip to content

Commit 09a651e

Browse files
committed
Merge branch 'develop' of https://github.com/geekcom/validator-docs into develop
2 parents 8e8e1d3 + d50476f commit 09a651e

File tree

3 files changed

+44
-11
lines changed

3 files changed

+44
-11
lines changed

CHANGELOG.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@
22

33
<!-- dumped content start -->
44

5+
## [3.3.2]
6+
7+
- [#55] Igualando develop e master, Thanks to [@geekcom]
8+
- [#54] Arrumando cnh, Thanks to [@MrEko]
9+
- [#51] update CHANGELOG.md, Thanks to [@geekcom]
10+
11+
<!-- dumped content end -->
12+
13+
<!-- dumped content start -->
14+
515
## [3.3.1]
616

717
- [#50] minor correction, update doc, Thanks to [@geekcom]
@@ -98,4 +108,7 @@
98108
[@andrergcosta]: https://github.com/andrergcosta
99109
[@MrEko]: https://github.com/MrEko
100110
[#49]: https://github.com/geekcom/validator-docs/pull/49
101-
[#50]: https://github.com/geekcom/validator-docs/pull/50
111+
[#50]: https://github.com/geekcom/validator-docs/pull/50
112+
[#55]: https://github.com/geekcom/validator-docs/pull/55
113+
[#54]: https://github.com/geekcom/validator-docs/pull/54
114+
[#51]: https://github.com/geekcom/validator-docs/pull/51

src/validator-docs/Rules/Cnh.php

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
final class Cnh extends Sanitization
1111
{
1212
/**
13-
* Trecho retirado do respect validation
13+
* @author Evandro Kondrat
14+
* Trecho reescrito com base no algoritmo passado pelo Detran-PR
1415
*/
1516
public function validateCnh($attribute, $value): bool
1617
{
@@ -24,20 +25,32 @@ public function validateCnh($attribute, $value): bool
2425
return false;
2526
}
2627

27-
for ($c = $s1 = $s2 = 0, $p = 9; $c < 9; $c++, $p--) {
28-
$s1 += (int) $value[$c] * $p;
29-
$s2 += (int) $value[$c] * (10 - $p);
28+
$parcial = substr($value, 0, 9);
29+
30+
for ($i = 0 , $j = 2, $s = 0; $i < mb_strlen($parcial); $i++, $j++) {
31+
$s += (int) $parcial[$i] * $j;
3032
}
3133

32-
$dv1 = $s1 % 11;
33-
if ($value[9] != ($dv1 > 9) ? 0 : $dv1) {
34-
return false;
34+
$resto = $s % 11;
35+
if ($resto <= 1) {
36+
$dv1 = 0;
37+
} else {
38+
$dv1 = 11 - $resto;
3539
}
3640

37-
$dv2 = $s2 % 11 - ($dv1 > 9 ? 2 : 0);
41+
$parcial = $dv1.$parcial;
3842

39-
$check = $dv2 < 0 ? $dv2 + 11 : ($dv2 > 9 ? 0 : $dv2);
43+
for ($i = 0, $j = 2, $s = 0; $i < mb_strlen($parcial); $i++, $j++) {
44+
$s += (int) $parcial[$i] * $j;
45+
}
46+
47+
$resto = $s % 11;
48+
if ($resto <= 1) {
49+
$dv2 = 0;
50+
} else {
51+
$dv2 = 11 - $resto;
52+
}
4053

41-
return $value[10] == $check;
54+
return $dv1.$dv2 == substr($value, -2);
4255
}
4356
}

tests/TestValidator.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,13 @@ public function cnh()
130130
$this->assertTrue($correct->passes());
131131

132132
$this->assertTrue($incorrect->fails());
133+
134+
$correct = \Validator::make(
135+
['certo' => '04463004100'],
136+
['certo' => 'cnh']
137+
);
138+
139+
$this->assertTrue($correct->passes());
133140
}
134141

135142
/** @test **/

0 commit comments

Comments
 (0)