diff --git a/src/Helpers/Mask.php b/src/Helpers/Mask.php index fe81746..f607708 100644 --- a/src/Helpers/Mask.php +++ b/src/Helpers/Mask.php @@ -109,9 +109,15 @@ public static function cep($value) */ public static function cnpj($value) { - $value = preg_replace('/\D/', '', $value); + $value = (string) $value; + + if (preg_match('/[a-z]/', $value)) { + return null; + } + + $value = preg_replace('/[^A-Z0-9]/', '', $value); - if (strlen($value) == 14) { + if (preg_match('/^[A-Z0-9]{12}[0-9]{2}$/', $value)) { return vsprintf("%s%s.%s%s%s.%s%s%s/%s%s%s%s-%s%s", str_split($value)); } diff --git a/src/Helpers/Validate.php b/src/Helpers/Validate.php index f782a14..204d30b 100644 --- a/src/Helpers/Validate.php +++ b/src/Helpers/Validate.php @@ -50,34 +50,40 @@ public static function isValidCpf($cpf) */ public static function isValidCnpj($cnpj) { - $cnpj = preg_replace('/[^0-9]/', '', (string) $cnpj); + $cnpj = (string) $cnpj; - if (strlen($cnpj) != 14) { + if (preg_match('/[a-z]/', $cnpj)) { return false; } - if (preg_match('/(\d)\1{13}/', $cnpj)) { + $cnpj = preg_replace('/[^A-Z0-9]/', '', $cnpj); + + if (!preg_match('/^[A-Z0-9]{12}[0-9]{2}$/', $cnpj)) { + return false; + } + + if (preg_match('/(.)\1{13}/', $cnpj)) { return false; } for ($i = 0, $j = 5, $soma = 0; $i < 12; $i++) { - $soma += $cnpj[$i] * $j; + $soma += (ord($cnpj[$i]) - 48) * $j; $j = ($j == 2) ? 9 : $j - 1; } $resto = $soma % 11; - if ($cnpj[12] != ($resto < 2 ? 0 : 11 - $resto)) { + if ((int) $cnpj[12] != ($resto < 2 ? 0 : 11 - $resto)) { return false; } for ($i = 0, $j = 6, $soma = 0; $i < 13; $i++) { - $soma += $cnpj[$i] * $j; + $soma += (ord($cnpj[$i]) - 48) * $j; $j = ($j == 2) ? 9 : $j - 1; } $resto = $soma % 11; - return $cnpj[13] == ($resto < 2 ? 0 : 11 - $resto); + return (int) $cnpj[13] == ($resto < 2 ? 0 : 11 - $resto); } /** diff --git a/tests/Helpers/MaskTest.php b/tests/Helpers/MaskTest.php index c359a18..9c3adac 100644 --- a/tests/Helpers/MaskTest.php +++ b/tests/Helpers/MaskTest.php @@ -20,10 +20,15 @@ public function testMaskBank() public function testeMaskCnpj() { $expected = '99.999.999/9999-99'; - $actual = Mask::cnpj('99999999999999'); - $this->assertEquals($expected, $actual); + + $this->assertEquals('GC.85C.XG8/0001-89', Mask::cnpj('GC85CXG8000189')); + $this->assertEquals('3N.XJ6.DX8/0001-70', Mask::cnpj('3NXJ6DX8000170')); + $this->assertEquals('LL.APR.PG5/0001-06', Mask::cnpj('LLAPRPG5000106')); + $this->assertEquals('H6.HT4.V25/0001-85', Mask::cnpj('H6HT4V25000185')); + $this->assertEquals('G5.6R6.P3L/0001-45', Mask::cnpj('G56R6P3L000145')); + $this->assertNull(Mask::cnpj('gc85cxg8000189')); } public function testMaskCep() diff --git a/tests/Helpers/ValidateTest.php b/tests/Helpers/ValidateTest.php index 9abd100..18cb18e 100644 --- a/tests/Helpers/ValidateTest.php +++ b/tests/Helpers/ValidateTest.php @@ -55,6 +55,19 @@ public function cnpjProvider() ['56.396.710/0001-37', true], ['14528181000138', true], ['56.396.710/000137', true], + ['32.474.406/0001-00', true], + ['11.602.425/0001-97', true], + ['48.657.442/0001-31', true], + ['GC.85C.XG8/0001-89', true], + ['3N.XJ6.DX8/0001-70', true], + ['LL.APR.PG5/0001-06', true], + ['H6.HT4.V25/0001-85', true], + ['G5.6R6.P3L/0001-45', true], + ['GC85CXG8000189', true], + ['GC.85C.XG8/0001-80', false], + ['GC.85C.XG8/0001-AB', false], + ['gc.85c.xg8/0001-89', false], + ['gc85cxg8000189', false], ['1452818100013', false], ['14528181', false], ['145281810001383', false], diff --git a/tests/HelpersTest.php b/tests/HelpersTest.php index a1137e8..3675b2d 100644 --- a/tests/HelpersTest.php +++ b/tests/HelpersTest.php @@ -172,6 +172,7 @@ public function testMaskCnpj() $right = '11222333444455'; $this->assertEquals(Helpers::maskCnpj($right), '11.222.333/4444-55'); + $this->assertEquals(Helpers::maskCnpj('GC85CXG8000189'), 'GC.85C.XG8/0001-89'); $this->assertNull(Helpers::maskCnpj($wrong)); }