Skip to content

melhorias de documentacao e sintaxe, v3.0.0 #40

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 1 commit into from
Oct 23, 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
22 changes: 13 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ _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 e Título de Eleitor.
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).

## Instalação

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

```
"require": {
"geekcom/validator-docs" : "2.*"
"geekcom/validator-docs" : "^3.0"
},
```

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

Após a instalação, adicione ao arquivo `config/app.php` ao array `providers` a seguinte linha:

```php
geekcom\ValidatorDocs\ValidatorProvider::class
```

----------------------------------------------------------------------------------------------------------------------------

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

* **cns** - Verifica se um Cartão Nciona de Saúde (CNS) é válido.

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

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

```php
Expand Down Expand Up @@ -126,7 +128,7 @@ $this->validate($request, [

## Combinando validação e formato

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:
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:

```php
$this->validate($request, [
Expand All @@ -151,6 +153,7 @@ public function store(Request $request)
'cnh' => 'required|cnh',
'titulo_eleitor' => 'required|titulo_eleitor',
'nis' => 'required|nis',
'cns' => 'required|cns',
]);

dd($data);
Expand All @@ -166,6 +169,7 @@ public function store(Request $request)
* **CNPJ** - http://www.geradorcnpj.com/
* **CPF** - http://geradordecpf.org
* **NIS** - https://www.4devs.com.br/gerador_de_pis_pasep
* **CNS** - https://geradornv.com.br/gerador-cns/

Fique a vontade para contribuir fazendo um fork.

Expand Down
7 changes: 4 additions & 3 deletions src/validator-docs/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,17 +194,18 @@ protected function validateNis($attribute, $value)

protected function validateCns($attribute, $value)
{
// Remove não numericos
$cns = preg_replace('/[^\d]/', '', $value);

// CNS definitivo começam em 1 ou 2 / CNS provisórios em 7, 8 ou 9
// CNSs definitivos começam em 1 ou 2 / CNSs provisórios em 7, 8 ou 9
if (preg_match("/[1-2][0-9]{10}00[0-1][0-9]/", $cns) || preg_match("/[7-9][0-9]{14}/", $cns)) {
return $this->somaPonderadaCns($cns) % 11 == 0;
}

return false;
}

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

for ($i = 0; $i < strlen($value); $i++) {
Expand Down