Skip to content

Commit d80b802

Browse files
authored
Merge pull request #41 from MrEko/add-certidao-validator
Validação de Certidão de Nascimento, Casamento ou Óbito
2 parents 44bfdaf + 2d690dc commit d80b802

File tree

4 files changed

+113
-2
lines changed

4 files changed

+113
-2
lines changed

README.md

+18-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ _Validação de documentos do Brasil usando **Laravel 6**_
77

88
> Para a versão compatível com Laravel 5 consulte o branch https://github.com/geekcom/validator-docs/tree/5.x.x
99
10-
Biblioteca Laravel para validação de CPF, CNPJ, CPF/CNPJ (quando salvos no mesmo atributo), CNH, PIS/PASEP/NIT/NIS, Título de Eleitor e Cartão Nacional de Saúde(CNS).
10+
Biblioteca Laravel para validação de CPF, CNPJ, CPF/CNPJ (quando salvos no mesmo atributo), CNH, PIS/PASEP/NIT/NIS, Título de Eleitor, Cartão Nacional de Saúde(CNS) e Certidões(nascimento/casamento/óbito).
1111

1212
## Instalação
1313

@@ -93,6 +93,14 @@ $this->validate($request, [
9393
]);
9494
```
9595

96+
* **certidao** - Verifica se uma certidão de nascimento/casamento/óbito é válida.
97+
98+
```php
99+
$this->validate($request, [
100+
'certidao' => 'required|certidao',
101+
]);
102+
```
103+
96104
* **formato_cnpj** - Verifica se o formato de um CNPJ é válida. ( 99.999.999/9999-99 )
97105

98106
```php
@@ -124,6 +132,14 @@ $this->validate($request, [
124132
'formato_nis' => 'required|formato_nis',
125133
]);
126134
```
135+
136+
* **formato_certidao** - Verifica se o formato de uma certidão é válida. ( 99999.99.99.9999.9.99999.999.9999999-99 ou 99999 99 99 9999 9 99999 999 9999999 99)
137+
138+
```php
139+
$this->validate($request, [
140+
'formato_certidao' => 'required|formato_certidao',
141+
]);
142+
```
127143
----------------------------------------------------------------------------------------------------------------------------
128144

129145
## Combinando validação e formato
@@ -170,6 +186,7 @@ public function store(Request $request)
170186
* **CPF** - http://geradordecpf.org
171187
* **NIS** - https://www.4devs.com.br/gerador_de_pis_pasep
172188
* **CNS** - https://geradornv.com.br/gerador-cns/
189+
* **CERTIDÃO** - https://www.treinaweb.com.br/ferramentas-para-desenvolvedores/gerador/certidao
173190

174191
Fique a vontade para contribuir fazendo um fork.
175192

src/validator-docs/Validator.php

+53
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ protected function validateFormatoNis($attribute, $value)
3838
return preg_match('/^\d{3}\.\d{5}\.\d{2}-\d{1}$/', $value) > 0;
3939
}
4040

41+
/*
42+
* O Número de Matrícula tem a configuração aaaaaa.bb.cc.dddd.e.fffff.ggg.hhhhhhh-ii
43+
*/
44+
protected function validateFormatoCertidao($attribute, $value)
45+
{
46+
return preg_match('/^\d{6}[. ]\d{2}[. ]\d{2}[. ]\d{4}[. ]\d{1}[. ]\d{5}[. ]\d{3}[. ]\d{7}[- ]\d{2}$/', $value) > 0;
47+
}
48+
4149
protected function validateCpf($attribute, $value)
4250
{
4351
$c = preg_replace('/\D/', '', $value);
@@ -214,4 +222,49 @@ private function somaPonderadaCns($value)
214222

215223
return $soma;
216224
}
225+
226+
/*
227+
* CERTIDÃO DE NASCIMENTO/CASAMENTO/ÓBITO
228+
* Fonte: http://ghiorzi.org/DVnew.htm#zc
229+
*
230+
* Nota: se o resto for "10", o DV será "1"
231+
*/
232+
protected function validateCertidao($attribute, $value)
233+
{
234+
// Remove não numericos
235+
$certidao = preg_replace('/[^\d]/', '', $value);
236+
237+
if (!preg_match("/[0-9]{32}/", $certidao)) {
238+
return false;
239+
}
240+
241+
$num = substr($certidao, 0, -2);
242+
$dv = substr($certidao, -2);
243+
244+
$dv1 = $this->somaPonderadaCertidao($num) % 11;
245+
$dv1 = $dv1 > 9 ? 1 : $dv1;
246+
$dv2 = $this->somaPonderadaCertidao($num.$dv1) % 11;
247+
$dv2 = $dv2 > 9 ? 1 : $dv2;
248+
249+
// Compara o dv recebido com os dois numeros calculados
250+
if ($dv === $dv1.$dv2) {
251+
return true;
252+
} else {
253+
return false;
254+
}
255+
}
256+
257+
private function somaPonderadaCertidao($value) {
258+
$soma = 0;
259+
260+
$multiplicador = 32 - strlen($value);
261+
for ($i = 0; $i < strlen($value); $i++) {
262+
$soma += $value[$i] * $multiplicador;
263+
264+
$multiplicador += 1;
265+
$multiplicador = $multiplicador > 10 ? 0 : $multiplicador;
266+
}
267+
268+
return $soma;
269+
}
217270
}

src/validator-docs/ValidatorProvider.php

+2
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,12 @@ protected function getMessages()
4343
'cpf_cnpj' => 'CPF ou CNPJ inválido',
4444
'nis' => 'PIS/PASEP/NIT/NIS inválido',
4545
'cns' => 'Cartão Nacional de Saúde inválido',
46+
'certidao' => 'Número da Certidão inválido',
4647
'formato_cnpj' => 'Formato inválido para CNPJ',
4748
'formato_cpf' => 'Formato inválido para CPF',
4849
'formato_cpf_cnpj' => 'Formato inválido para CPF ou CNPJ',
4950
'formato_nis' => 'Formato inválido para PIS/PASEP/NIT/NIS',
51+
'formato_certidao' => 'Formato inválido para Certidão',
5052
];
5153
}
5254

tests/TestValidator.php

+40-1
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ public function testNisFormato()
179179

180180
$this->assertTrue($incorrect->fails());
181181
}
182-
182+
183183
public function testCns()
184184
{
185185
// Definitiva
@@ -210,4 +210,43 @@ public function testCns()
210210
$this->assertTrue($correct->passes());
211211
$this->assertTrue($incorrect->fails());
212212
}
213+
214+
public function testCertidao()
215+
{
216+
$correct = \Validator::make(
217+
['certo' => '659447 02 55 9015 1 99679 468 2559590-16'],
218+
['certo' => 'certidao']
219+
);
220+
221+
$incorrect = \Validator::make(
222+
['errado' => '659447 02 55 2015 1 27861 468 2559590-32'],
223+
['errado' => 'certidao']
224+
);
225+
226+
$this->assertTrue($correct->passes());
227+
$this->assertTrue($incorrect->fails());
228+
}
229+
230+
public function testCertidaoFormato()
231+
{
232+
$correct = \Validator::make(
233+
['certo' => '434546.02.55.2019.1.71037.134.6484858-10'],
234+
['certo' => 'formato-certidao']
235+
);
236+
237+
$incorrect = \Validator::make(
238+
['errado' => '201.733.7434-9'],
239+
['errado' => 'formato-certidao']
240+
);
241+
242+
$this->assertTrue($correct->passes());
243+
$this->assertTrue($incorrect->fails());
244+
245+
// com ' ' no lugar de '.'
246+
$correct = \Validator::make(
247+
['certo' => '434546 02 55 2019 1 71037 134 6484858 10'],
248+
['certo' => 'formato-certidao']
249+
);
250+
$this->assertTrue($correct->passes());
251+
}
213252
}

0 commit comments

Comments
 (0)