Skip to content

Commit 4ba1ee7

Browse files
authored
Merge branch 'master' into add-certidao-validator
2 parents a721bad + 44bfdaf commit 4ba1ee7

File tree

4 files changed

+69
-12
lines changed

4 files changed

+69
-12
lines changed

README.md

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ _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 Certidões(nascimento/casamento/óbito).
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

1414
No arquivo `composer.json`, adicione validator-docs como dependência do seu projeto:
1515

1616
```
1717
"require": {
18-
"geekcom/validator-docs" : "2.*"
18+
"geekcom/validator-docs" : "^3.0"
1919
},
2020
```
2121

@@ -31,12 +31,6 @@ Ou simplesmente execute o comando:
3131
composer require geekcom/validator-docs
3232
```
3333

34-
Após a instalação, adicione ao arquivo `config/app.php` ao array `providers` a seguinte linha:
35-
36-
```php
37-
geekcom\ValidatorDocs\ValidatorProvider::class
38-
```
39-
4034
----------------------------------------------------------------------------------------------------------------------------
4135

4236
## Como usar - Validações disponíveis
@@ -91,6 +85,14 @@ $this->validate($request, [
9185
]);
9286
```
9387

88+
* **cns** - Verifica se um Cartão Nciona de Saúde (CNS) é válido.
89+
90+
```php
91+
$this->validate($request, [
92+
'cns' => 'required|cns',
93+
]);
94+
```
95+
9496
* **certidao** - Verifica se uma certidão de nascimento/casamento/óbito é válida.
9597

9698
```php
@@ -142,7 +144,7 @@ $this->validate($request, [
142144

143145
## Combinando validação e formato
144146

145-
No exemplo abaixo, fazemos um teste onde verificamos a formatação e a validade de um CPF ou CNPJ, para os casos onde a informação deva ser salva em um mesmo atributo:
147+
No exemplo abaixo, fazemos um teste onde verificamos a formatação e a validade de um CPF ou CNPJ, para os casos onde a informação deve ser salva em um mesmo atributo:
146148

147149
```php
148150
$this->validate($request, [
@@ -167,6 +169,7 @@ public function store(Request $request)
167169
'cnh' => 'required|cnh',
168170
'titulo_eleitor' => 'required|titulo_eleitor',
169171
'nis' => 'required|nis',
172+
'cns' => 'required|cns',
170173
]);
171174

172175
dd($data);
@@ -182,6 +185,7 @@ public function store(Request $request)
182185
* **CNPJ** - http://www.geradorcnpj.com/
183186
* **CPF** - http://geradordecpf.org
184187
* **NIS** - https://www.4devs.com.br/gerador_de_pis_pasep
188+
* **CNS** - https://geradornv.com.br/gerador-cns/
185189
* **CERTIDÃO** - https://www.treinaweb.com.br/ferramentas-para-desenvolvedores/gerador/certidao
186190

187191
Fique a vontade para contribuir fazendo um fork.

src/validator-docs/Validator.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,29 @@ protected function validateNis($attribute, $value)
200200
return ($nis[10] == (((10 * $d) % 11) % 10));
201201
}
202202

203+
protected function validateCns($attribute, $value)
204+
{
205+
$cns = preg_replace('/[^\d]/', '', $value);
206+
207+
// CNSs definitivos começam em 1 ou 2 / CNSs provisórios em 7, 8 ou 9
208+
if (preg_match("/[1-2][0-9]{10}00[0-1][0-9]/", $cns) || preg_match("/[7-9][0-9]{14}/", $cns)) {
209+
return $this->somaPonderadaCns($cns) % 11 == 0;
210+
}
211+
212+
return false;
213+
}
214+
215+
private function somaPonderadaCns($value)
216+
{
217+
$soma = 0;
218+
219+
for ($i = 0; $i < strlen($value); $i++) {
220+
$soma += $value[$i] * (15 - $i);
221+
}
222+
223+
return $soma;
224+
}
225+
203226
/*
204227
* CERTIDÃO DE NASCIMENTO/CASAMENTO/ÓBITO
205228
* Fonte: http://ghiorzi.org/DVnew.htm#zc
@@ -241,6 +264,5 @@ private function somaPonderadaCertidao($value) {
241264
$multiplicador += 1;
242265
$multiplicador = $multiplicador > 10 ? 0 : $multiplicador;
243266
}
244-
return $soma;
245267
}
246268
}

src/validator-docs/ValidatorProvider.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ 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+
'cns' => 'Cartão Nacional de Saúde inválido',
4546
'certidao' => 'Número da Certidão inválido',
4647
'formato_cnpj' => 'Formato inválido para CNPJ',
4748
'formato_cpf' => 'Formato inválido para CPF',

tests/TestValidator.php

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public function testTituloEleitor()
145145

146146
$this->assertTrue($incorrect->fails());
147147
}
148-
148+
149149
public function testNis()
150150
{
151151
$correct = \Validator::make(
@@ -179,7 +179,38 @@ public function testNisFormato()
179179

180180
$this->assertTrue($incorrect->fails());
181181
}
182+
183+
public function testCns()
184+
{
185+
// Definitiva
186+
$correct = \Validator::make(
187+
['certo' => '116 3876 9194 0009'],
188+
['certo' => 'cns']
189+
);
190+
191+
$incorrect = \Validator::make(
192+
['errado' => '116 5698 9194 0009'],
193+
['errado' => 'cns']
194+
);
195+
196+
$this->assertTrue($correct->passes());
197+
$this->assertTrue($incorrect->fails());
182198

199+
// Provisoria
200+
$correct = \Validator::make(
201+
['certo' => '892 1623 5477 0008'],
202+
['certo' => 'cns']
203+
);
204+
205+
$incorrect = \Validator::make(
206+
['errado' => '892 2641 5477 0008'],
207+
['errado' => 'cns']
208+
);
209+
210+
$this->assertTrue($correct->passes());
211+
$this->assertTrue($incorrect->fails());
212+
}
213+
183214
public function testCertidao()
184215
{
185216
$correct = \Validator::make(
@@ -218,5 +249,4 @@ public function testCertidaoFormato()
218249
);
219250
$this->assertTrue($correct->passes());
220251
}
221-
222252
}

0 commit comments

Comments
 (0)