Skip to content

Validação de Certidão de Nascimento, Casamento ou Óbito #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ _Validação de documentos do Brasil usando **Laravel 6**_

> Para a versão compatível com Laravel 5 consulte o branch https://github.com/geekcom/validator-docs/tree/5.x.x

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).
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).

## Instalação

Expand Down Expand Up @@ -93,6 +93,14 @@ $this->validate($request, [
]);
```

* **certidao** - Verifica se uma certidão de nascimento/casamento/óbito é válida.

```php
$this->validate($request, [
'certidao' => 'required|certidao',
]);
```

* **formato_cnpj** - Verifica se o formato de um CNPJ é válida. ( 99.999.999/9999-99 )

```php
Expand Down Expand Up @@ -124,6 +132,14 @@ $this->validate($request, [
'formato_nis' => 'required|formato_nis',
]);
```

* **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)

```php
$this->validate($request, [
'formato_certidao' => 'required|formato_certidao',
]);
```
----------------------------------------------------------------------------------------------------------------------------

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

Fique a vontade para contribuir fazendo um fork.

Expand Down
53 changes: 53 additions & 0 deletions src/validator-docs/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ protected function validateFormatoNis($attribute, $value)
return preg_match('/^\d{3}\.\d{5}\.\d{2}-\d{1}$/', $value) > 0;
}

/*
* O Número de Matrícula tem a configuração aaaaaa.bb.cc.dddd.e.fffff.ggg.hhhhhhh-ii
*/
protected function validateFormatoCertidao($attribute, $value)
{
return preg_match('/^\d{6}[. ]\d{2}[. ]\d{2}[. ]\d{4}[. ]\d{1}[. ]\d{5}[. ]\d{3}[. ]\d{7}[- ]\d{2}$/', $value) > 0;
}

protected function validateCpf($attribute, $value)
{
$c = preg_replace('/\D/', '', $value);
Expand Down Expand Up @@ -214,4 +222,49 @@ private function somaPonderadaCns($value)

return $soma;
}

/*
* CERTIDÃO DE NASCIMENTO/CASAMENTO/ÓBITO
* Fonte: http://ghiorzi.org/DVnew.htm#zc
*
* Nota: se o resto for "10", o DV será "1"
*/
protected function validateCertidao($attribute, $value)
{
// Remove não numericos
$certidao = preg_replace('/[^\d]/', '', $value);

if (!preg_match("/[0-9]{32}/", $certidao)) {
return false;
}

$num = substr($certidao, 0, -2);
$dv = substr($certidao, -2);

$dv1 = $this->somaPonderadaCertidao($num) % 11;
$dv1 = $dv1 > 9 ? 1 : $dv1;
$dv2 = $this->somaPonderadaCertidao($num.$dv1) % 11;
$dv2 = $dv2 > 9 ? 1 : $dv2;

// Compara o dv recebido com os dois numeros calculados
if ($dv === $dv1.$dv2) {
return true;
} else {
return false;
}
}

private function somaPonderadaCertidao($value) {
$soma = 0;

$multiplicador = 32 - strlen($value);
for ($i = 0; $i < strlen($value); $i++) {
$soma += $value[$i] * $multiplicador;

$multiplicador += 1;
$multiplicador = $multiplicador > 10 ? 0 : $multiplicador;
}

return $soma;
}
}
2 changes: 2 additions & 0 deletions src/validator-docs/ValidatorProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,12 @@ protected function getMessages()
'cpf_cnpj' => 'CPF ou CNPJ inválido',
'nis' => 'PIS/PASEP/NIT/NIS inválido',
'cns' => 'Cartão Nacional de Saúde inválido',
'certidao' => 'Número da Certidão inválido',
'formato_cnpj' => 'Formato inválido para CNPJ',
'formato_cpf' => 'Formato inválido para CPF',
'formato_cpf_cnpj' => 'Formato inválido para CPF ou CNPJ',
'formato_nis' => 'Formato inválido para PIS/PASEP/NIT/NIS',
'formato_certidao' => 'Formato inválido para Certidão',
];
}

Expand Down
41 changes: 40 additions & 1 deletion tests/TestValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ public function testNisFormato()

$this->assertTrue($incorrect->fails());
}

public function testCns()
{
// Definitiva
Expand Down Expand Up @@ -210,4 +210,43 @@ public function testCns()
$this->assertTrue($correct->passes());
$this->assertTrue($incorrect->fails());
}

public function testCertidao()
{
$correct = \Validator::make(
['certo' => '659447 02 55 9015 1 99679 468 2559590-16'],
['certo' => 'certidao']
);

$incorrect = \Validator::make(
['errado' => '659447 02 55 2015 1 27861 468 2559590-32'],
['errado' => 'certidao']
);

$this->assertTrue($correct->passes());
$this->assertTrue($incorrect->fails());
}

public function testCertidaoFormato()
{
$correct = \Validator::make(
['certo' => '434546.02.55.2019.1.71037.134.6484858-10'],
['certo' => 'formato-certidao']
);

$incorrect = \Validator::make(
['errado' => '201.733.7434-9'],
['errado' => 'formato-certidao']
);

$this->assertTrue($correct->passes());
$this->assertTrue($incorrect->fails());

// com ' ' no lugar de '.'
$correct = \Validator::make(
['certo' => '434546 02 55 2019 1 71037 134 6484858 10'],
['certo' => 'formato-certidao']
);
$this->assertTrue($correct->passes());
}
}