From 72b99d67d4f68b727c386eb30bdd6b594143c31e Mon Sep 17 00:00:00 2001 From: ekondrat Date: Wed, 23 Oct 2019 13:35:14 -0300 Subject: [PATCH] =?UTF-8?q?Adicionando=20valida=C3=A7=C3=A3o=20do=20Cart?= =?UTF-8?q?=C3=A3o=20Nacional=20de=20Sa=C3=BAde?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/validator-docs/Validator.php | 22 ++++++++++++++++ src/validator-docs/ValidatorProvider.php | 1 + tests/TestValidator.php | 32 +++++++++++++++++++++++- 3 files changed, 54 insertions(+), 1 deletion(-) diff --git a/src/validator-docs/Validator.php b/src/validator-docs/Validator.php index ee9c6d9..f454008 100644 --- a/src/validator-docs/Validator.php +++ b/src/validator-docs/Validator.php @@ -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; + } } diff --git a/src/validator-docs/ValidatorProvider.php b/src/validator-docs/ValidatorProvider.php index d62491f..b451d93 100644 --- a/src/validator-docs/ValidatorProvider.php +++ b/src/validator-docs/ValidatorProvider.php @@ -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', diff --git a/tests/TestValidator.php b/tests/TestValidator.php index 56d96f1..7440540 100644 --- a/tests/TestValidator.php +++ b/tests/TestValidator.php @@ -145,7 +145,7 @@ public function testTituloEleitor() $this->assertTrue($incorrect->fails()); } - + public function testNis() { $correct = \Validator::make( @@ -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()); + } }