-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adiciona classe de validações para CPF e CNPJ
Essa atualização inclui uma nova classe de auxílio `Validations` no módulo `NFEioServiceInvoices`. Essa classe contém métodos para validar CPFs e CNPJs, conforme regras de verificação do Brasil, garantindo a integridade dos dados tratados em nossos serviços.
- Loading branch information
1 parent
a1540bf
commit 3fffe50
Showing
1 changed file
with
55 additions
and
0 deletions.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
modules/addons/NFEioServiceInvoices/lib/Helpers/Validations.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
namespace NFEioServiceInvoices\Helpers; | ||
|
||
class Validations | ||
{ | ||
|
||
/** | ||
* Verifica se dado valor é um CPF válido usando calculo de verificação de CPF. | ||
* @param $cpf CPF a ser validado. Pode ser enviado com ou sem máscara. | ||
* @return bool true se CPF válido, senão false | ||
*/ | ||
public static function validateCPF($cpf): bool | ||
{ | ||
$cpf = preg_replace('/[^0-9]/', '', $cpf); | ||
if (strlen($cpf) != 11 || preg_match('/(\d)\1{10}/', $cpf)) { | ||
return false; | ||
} | ||
for ($t = 9; $t < 11; $t++) { | ||
for ($d = 0, $c = 0; $c < $t; $c++) { | ||
$d += $cpf[$c] * (($t + 1) - $c); | ||
} | ||
$d = ((10 * $d) % 11) % 10; | ||
if ($cpf[$c] != $d) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
/** | ||
* Verifica se dado valor é um CNPJ válido usando calculo de verificação de CNPJ. | ||
* @param $cnpj CNPJ a ser validado. Pode ser enviado com ou sem máscara. | ||
* @return bool true se CNPJ válido, senão false | ||
*/ | ||
public static function validateCNPJ($cnpj): bool | ||
{ | ||
$cnpj = preg_replace('/[^0-9]/', '', $cnpj); | ||
if (strlen($cnpj) != 14 || preg_match('/(\d)\1{13}/', $cnpj)) { | ||
return false; | ||
} | ||
for ($t = 12; $t < 14; $t++) { | ||
for ($d = 0, $p = $t - 7, $c = 0; $c < $t; $c++) { | ||
$d += $cnpj[$c] * $p; | ||
$p = ($p < 3) ? 9 : --$p; | ||
} | ||
$d = ((10 * $d) % 11) % 10; | ||
if ($cnpj[$c] != $d) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
} |