Skip to content

Commit d297552

Browse files
author
ekondrat
committed
Adicionando validação de certidão de nascimento/casamento/óbito
1 parent 2e19e5e commit d297552

File tree

4 files changed

+110
-0
lines changed

4 files changed

+110
-0
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,14 @@ $this->validate($request, [
9191
]);
9292
```
9393

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

96104
```php
@@ -122,6 +130,14 @@ $this->validate($request, [
122130
'formato_nis' => 'required|formato_nis',
123131
]);
124132
```
133+
134+
* **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)
135+
136+
```php
137+
$this->validate($request, [
138+
'formato_certidao' => 'required|formato_certidao',
139+
]);
140+
```
125141
----------------------------------------------------------------------------------------------------------------------------
126142

127143
## Combinando validação e formato
@@ -166,6 +182,7 @@ public function store(Request $request)
166182
* **CNPJ** - http://www.geradorcnpj.com/
167183
* **CPF** - http://geradordecpf.org
168184
* **NIS** - https://www.4devs.com.br/gerador_de_pis_pasep
185+
* **CERTIDÃO** - https://www.treinaweb.com.br/ferramentas-para-desenvolvedores/gerador/certidao
169186

170187
Fique a vontade para contribuir fazendo um fork.
171188

src/validator-docs/Validator.php

Lines changed: 52 additions & 0 deletions
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);
@@ -191,4 +199,48 @@ protected function validateNis($attribute, $value)
191199

192200
return ($nis[10] == (((10 * $d) % 11) % 10));
193201
}
202+
203+
/*
204+
* CERTIDÃO DE NASCIMENTO/CASAMENTO/ÓBITO
205+
* Fonte: http://ghiorzi.org/DVnew.htm#zc
206+
*
207+
* Nota: se o resto for "10", o DV será "1"
208+
*/
209+
protected function validateCertidao($attribute, $value)
210+
{
211+
// Remove não numericos
212+
$certidao = preg_replace('/[^\d]/', '', $value);
213+
214+
if (!preg_match("/[0-9]{32}/", $certidao)) {
215+
return false;
216+
}
217+
218+
$num = substr($certidao, 0, -2);
219+
$dv = substr($certidao, -2);
220+
221+
$dv1 = $this->somaPonderadaCertidao($num) % 11;
222+
$dv1 = $dv1 > 9 ? 1 : $dv1;
223+
$dv2 = $this->somaPonderadaCertidao($num.$dv1) % 11;
224+
$dv2 = $dv2 > 9 ? 1 : $dv2;
225+
226+
// Compara o dv recebido com os dois numeros calculados
227+
if ($dv === $dv1.$dv2) {
228+
return true;
229+
} else {
230+
return false;
231+
}
232+
}
233+
234+
private function somaPonderadaCertidao($value) {
235+
$soma = 0;
236+
237+
$multiplicador = 32 - strlen($value);
238+
for ($i = 0; $i < strlen($value); $i++) {
239+
$soma += $value[$i] * $multiplicador;
240+
241+
$multiplicador += 1;
242+
$multiplicador = $multiplicador > 10 ? 0 : $multiplicador;
243+
}
244+
return $soma;
245+
}
194246
}

src/validator-docs/ValidatorProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,12 @@ protected function getMessages()
4242
'cpf' => 'CPF inválido',
4343
'cpf_cnpj' => 'CPF ou CNPJ inválido',
4444
'nis' => 'PIS/PASEP/NIT/NIS inválido',
45+
'certidao' => 'Número da Certidão inválido',
4546
'formato_cnpj' => 'Formato inválido para CNPJ',
4647
'formato_cpf' => 'Formato inválido para CPF',
4748
'formato_cpf_cnpj' => 'Formato inválido para CPF ou CNPJ',
4849
'formato_nis' => 'Formato inválido para PIS/PASEP/NIT/NIS',
50+
'formato_certidao' => 'Formato inválido para Certidão',
4951
];
5052
}
5153

tests/TestValidator.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,4 +180,43 @@ public function testNisFormato()
180180
$this->assertTrue($incorrect->fails());
181181
}
182182

183+
public function testCertidao()
184+
{
185+
$correct = \Validator::make(
186+
['certo' => '659447 02 55 9015 1 99679 468 2559590-16'],
187+
['certo' => 'certidao']
188+
);
189+
190+
$incorrect = \Validator::make(
191+
['errado' => '659447 02 55 2015 1 27861 468 2559590-32'],
192+
['errado' => 'certidao']
193+
);
194+
195+
$this->assertTrue($correct->passes());
196+
$this->assertTrue($incorrect->fails());
197+
}
198+
199+
public function testCertidaoFormato()
200+
{
201+
$correct = \Validator::make(
202+
['certo' => '434546.02.55.2019.1.71037.134.6484858-10'],
203+
['certo' => 'formato-certidao']
204+
);
205+
206+
$incorrect = \Validator::make(
207+
['errado' => '201.733.7434-9'],
208+
['errado' => 'formato-certidao']
209+
);
210+
211+
$this->assertTrue($correct->passes());
212+
$this->assertTrue($incorrect->fails());
213+
214+
// com ' ' no lugar de '.'
215+
$correct = \Validator::make(
216+
['certo' => '434546 02 55 2019 1 71037 134 6484858 10'],
217+
['certo' => 'formato-certidao']
218+
);
219+
$this->assertTrue($correct->passes());
220+
}
221+
183222
}

0 commit comments

Comments
 (0)