From 59fb51b8c87b0d2acb596ee6ca9f17ed425d5dd0 Mon Sep 17 00:00:00 2001 From: Humberto Lidio Antonelli Date: Wed, 6 Mar 2019 18:14:37 -0300 Subject: [PATCH 1/4] =?UTF-8?q?Cria=C3=A7=C3=A3o=20da=20fun=C3=A7=C3=A3o?= =?UTF-8?q?=20para=20verificar=20PIS/PASEP/NIS/NIT?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/validator-docs/Validator.php | 33 ++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/validator-docs/Validator.php b/src/validator-docs/Validator.php index 6b2205f..e3b17cf 100644 --- a/src/validator-docs/Validator.php +++ b/src/validator-docs/Validator.php @@ -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 @@ -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)); + } } From 1a070841359c6a2c572ed168fbaa58acbfa7ed32 Mon Sep 17 00:00:00 2001 From: Humberto Lidio Antonelli Date: Wed, 6 Mar 2019 18:16:31 -0300 Subject: [PATCH 2/4] =?UTF-8?q?Cria=C3=A7=C3=A3o=20da=20fun=C3=A7=C3=A3o?= =?UTF-8?q?=20para=20verificar=20PIS/PASEP/NIT/NIS?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/validator-docs/ValidatorProvider.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/validator-docs/ValidatorProvider.php b/src/validator-docs/ValidatorProvider.php index 941dab7..f4a767b 100644 --- a/src/validator-docs/ValidatorProvider.php +++ b/src/validator-docs/ValidatorProvider.php @@ -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', ]; } From 18b76da08f5f6f3dc9112d4f7244e7b97a06f33c Mon Sep 17 00:00:00 2001 From: Humberto Lidio Antonelli Date: Wed, 6 Mar 2019 18:18:35 -0300 Subject: [PATCH 3/4] Update README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index e6312c4..52cfd36 100644 --- a/README.md +++ b/README.md @@ -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: From b9c95b316d6b89562b29aaf088d16e1f3f538000 Mon Sep 17 00:00:00 2001 From: Humberto Lidio Antonelli Date: Wed, 6 Mar 2019 21:24:08 -0300 Subject: [PATCH 4/4] =?UTF-8?q?Cria=C3=A7=C3=A3o=20da=20fun=C3=A7=C3=A3o?= =?UTF-8?q?=20para=20verificar=20PIS/PASEP/NIS/NIT?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/TestValidator.php | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/tests/TestValidator.php b/tests/TestValidator.php index 836a7ec..de5d4e4 100644 --- a/tests/TestValidator.php +++ b/tests/TestValidator.php @@ -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()); + } }