Skip to content

Merge #38

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 4 commits 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: 22 additions & 0 deletions src/validator-docs/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,4 +191,26 @@ protected function validateNis($attribute, $value)

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

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
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) {
$soma = 0;

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

return $soma;
}
}
1 change: 1 addition & 0 deletions src/validator-docs/ValidatorProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ protected function getMessages()
'cpf' => 'CPF inválido',
'cpf_cnpj' => 'CPF ou CNPJ inválido',
'nis' => 'PIS/PASEP/NIT/NIS inválido',
'cns' => 'Cartão Nacional de Saúde 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',
Expand Down
32 changes: 31 additions & 1 deletion tests/TestValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public function testTituloEleitor()

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

public function testNis()
{
$correct = \Validator::make(
Expand Down Expand Up @@ -180,4 +180,34 @@ public function testNisFormato()
$this->assertTrue($incorrect->fails());
}

public function testCns()
{
// Definitiva
$correct = \Validator::make(
['certo' => '116 3876 9194 0009'],
['certo' => 'cns']
);

$incorrect = \Validator::make(
['errado' => '116 5698 9194 0009'],
['errado' => 'cns']
);

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

// Provisoria
$correct = \Validator::make(
['certo' => '892 1623 5477 0008'],
['certo' => 'cns']
);

$incorrect = \Validator::make(
['errado' => '892 2641 5477 0008'],
['errado' => 'cns']
);

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