Skip to content

Commit a338406

Browse files
committed
Adicionando validação para Renavam
1 parent 6affec2 commit a338406

File tree

4 files changed

+68
-0
lines changed

4 files changed

+68
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace geekcom\ValidatorDocs\Rules;
4+
5+
class Renavam extends Sanitization
6+
{
7+
public function validateRenavam($attribute, $renavam): bool
8+
{
9+
$renavam = $this->sanitize($renavam);
10+
$sum = 0;
11+
$renavamArray = str_split($renavam);
12+
$digitCount = 0;
13+
14+
for ($i=5; $i >= 2; $i--) {
15+
$sum += $renavamArray[$digitCount] * $i;
16+
$digitCount++;
17+
}
18+
19+
$valor = $sum % 11;
20+
21+
$digit = $valor;
22+
23+
if ($valor == 11 || $valor == 0 || $valor >= 10) {
24+
$digit = 0;
25+
}
26+
27+
if ($digit == $renavamArray[4]) {
28+
return true;
29+
}
30+
31+
return false;
32+
}
33+
}

src/validator-docs/Validator.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace geekcom\ValidatorDocs;
66

7+
use geekcom\ValidatorDocs\Rules\Renavam;
78
use Illuminate\Validation\Validator as BaseValidator;
89
use geekcom\ValidatorDocs\Rules\TituloEleitoral;
910
use geekcom\ValidatorDocs\Rules\Cns;
@@ -103,4 +104,11 @@ protected function validateCertidao($attribute, $value): bool
103104

104105
return $certidao->validateCertidao($attribute, $value);
105106
}
107+
108+
protected function validateRenavam($attribute, $value): bool
109+
{
110+
$renavam = new Renavam();
111+
112+
return $renavam->validateRenavam($attribute, $value);
113+
}
106114
}

src/validator-docs/ValidatorProvider.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ protected function getMessages()
3636
'titulo_eleitor' => 'Título de Eleitor inválido',
3737
'cnpj' => 'CNPJ inválido',
3838
'cpf' => 'CPF inválido',
39+
'renavam' => 'Renavam inválido',
3940
'cpf_cnpj' => 'CPF ou CNPJ inválido',
4041
'nis' => 'PIS/PASEP/NIT/NIS inválido',
4142
'cns' => 'Cartão Nacional de Saúde inválido',

tests/TestValidator.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,4 +265,30 @@ public function formatoDacertidao()
265265
);
266266
$this->assertTrue($correct->passes());
267267
}
268+
269+
/**
270+
* @test
271+
*/
272+
public function formatoRenavam()
273+
{
274+
$correct = Validator::make(
275+
['certo' => '197073212'],
276+
['certo' => 'renavam']
277+
);
278+
279+
$incorrect = Validator::make(
280+
['errado' => '1234555582'],
281+
['errado' => 'renavam']
282+
);
283+
284+
$this->assertTrue($correct->passes());
285+
$this->assertTrue($incorrect->fails());
286+
287+
$correct = Validator::make(
288+
['certo' => '1970.73212'],
289+
['certo' => 'renavam']
290+
);
291+
292+
$this->assertTrue($correct->passes());
293+
}
268294
}

0 commit comments

Comments
 (0)