Skip to content

Master from develop #20

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 8 commits into from
Mar 7, 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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ a diferença é que agora, você terá os seguintes métodos de validação:
* **cnpj** - Verifica se o CNPJ é valido. Para testar, basta utilizar o site http://www.geradorcnpj.com/
* **cpf** - Verifica se o CPF é valido. Para testar, basta utilizar o site http://geradordecpf.org
* **cpf_cnpj** - Verifica se é um CPF ou CNPJ valido. Para testar, basta utilizar um dos sites acima
* **nis** - Verifica se o PIS/PASEP/NIT/NIS é valido. Para testar, basta utilizar o site https://www.4devs.com.br/gerador_de_pis_pasep
* **formato_cnpj** - Verifica se a mascara do CNPJ é válida. ( 99.999.999/9999-99 )
* **formato_cpf** - Verifica se a mascara do CPF é válida. ( 999.999.999-99 )
* **formato_cpf_cnpj** - Verifica se a mascara do CPF ou CNPJ é válida. ( 999.999.999-99 ) ou ( 99.999.999/9999-99 )
* **formato_nis** - Verifica se a mascara do PIS/PASEP/NIT/NIS é válida. ( 999.99999-99.9 )


Então, podemos realizar um simples teste onde dizemos que o campo CPF será obrigatório e usamos a biblioteca para validar:
Expand Down
33 changes: 33 additions & 0 deletions src/validator-docs/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,17 @@ protected function validateFormatoCpfCnpj($attribute, $value)
{
return $this->validateFormatoCpf($attribute, $value) || $this->validateFormatoCnpj($attribute, $value);
}

/**
* Valida o formato do PIS/PASEP/NIS/NIT
* @param string $attribute
* @param string $value
* @return boolean
*/
protected function validateFormatoNis($attribute, $value)
{
return preg_match('/^\d{3}\.\d{5}\.\d{2}-\d{1}$/', $value) > 0;
}

/**
* Valida CPF
Expand Down Expand Up @@ -224,5 +235,27 @@ protected function validateTituloEleitor($attribute, $value)

return true;
}

/**
* Valida PIS/PASEP/NIS/NIT
* @param string $attribute
* @param string $value
* @return boolean
*/

protected function validateNis($attribute, $value)
{
$nis = sprintf('%011s', preg_replace('{\D}', '', $value));

if (strlen($nis) != 11 || preg_match("/^{$nis[0]}{11}$/", $nis)) {
return false;
}

for ($d = 0, $p = 2, $c = 9; $c >= 0; $c--, ($p < 9) ? $p++ : $p = 2) {
$d += $nis[$c] * $p;
}

return ($nis[10] == (((10 * $d) % 11) % 10));
}

}
2 changes: 2 additions & 0 deletions src/validator-docs/ValidatorProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@ protected function getMessages()
'cnpj' => 'CNPJ inválido',
'cpf' => 'CPF inválido',
'cpf_cnpj' => 'CPF ou CNPJ inválido',
'nis' => 'PIS/PASEP/NIT/NIS 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',
];
}

Expand Down
34 changes: 34 additions & 0 deletions tests/TestValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,5 +145,39 @@ public function testTituloEleitor()

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

public function testNis()
{
$correct = \Validator::make(
['certo' => '201.73374.34-9'],
['certo' => 'nis']
);

$incorrect = \Validator::make(
['errado' => '201.73374.34-0'],
['errado' => 'nis']
);

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

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

public function testNisormato()
{
$correct = \Validator::make(
['certo' => '201.73374.34-9'],
['certo' => 'formato-nis']
);

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

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

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

}