From 8c77aeae1b58749f22541a34442f45bd44a44e56 Mon Sep 17 00:00:00 2001 From: Ezequias Antunes Date: Wed, 22 Jul 2026 13:19:41 -0300 Subject: [PATCH 1/3] =?UTF-8?q?Atualiza=20valida=C3=A7=C3=A3o=20do=20CNPJ?= =?UTF-8?q?=20para=20permitir=20o=20novo=20formato=20de=20caracteres=20alf?= =?UTF-8?q?anum=C3=A9ricos?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Helpers/Validate.php | 15 ++++++++------- tests/Helpers/ValidateTest.php | 11 +++++++++++ tests/HelpersTest.php | 1 + 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/Helpers/Validate.php b/src/Helpers/Validate.php index f782a14..6d836c6 100644 --- a/src/Helpers/Validate.php +++ b/src/Helpers/Validate.php @@ -50,34 +50,35 @@ public static function isValidCpf($cpf) */ public static function isValidCnpj($cnpj) { - $cnpj = preg_replace('/[^0-9]/', '', (string) $cnpj); + $cnpj = preg_replace('/[^A-Za-z0-9]/', '', (string) $cnpj); + $cnpj = strtoupper($cnpj); - if (strlen($cnpj) != 14) { + if (!preg_match('/^[A-Z0-9]{12}[0-9]{2}$/', $cnpj)) { return false; } - if (preg_match('/(\d)\1{13}/', $cnpj)) { + 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/ValidateTest.php b/tests/Helpers/ValidateTest.php index 9abd100..f092509 100644 --- a/tests/Helpers/ValidateTest.php +++ b/tests/Helpers/ValidateTest.php @@ -55,6 +55,17 @@ 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], ['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)); } From 8853defcf520a0ad20843c5349b6a29b3d1674a5 Mon Sep 17 00:00:00 2001 From: Ezequias Antunes Date: Wed, 22 Jul 2026 13:20:15 -0300 Subject: [PATCH 2/3] =?UTF-8?q?Atualiza=20a=20fun=C3=A7=C3=A3o=20de=20m?= =?UTF-8?q?=C3=A1scara=20do=20CNPJ=20para=20permitir=20caracteres=20alfanu?= =?UTF-8?q?m=C3=A9ricos?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Helpers/Mask.php | 5 +++-- tests/Helpers/MaskTest.php | 8 ++++++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/Helpers/Mask.php b/src/Helpers/Mask.php index fe81746..9f7de20 100644 --- a/src/Helpers/Mask.php +++ b/src/Helpers/Mask.php @@ -109,9 +109,10 @@ public static function cep($value) */ public static function cnpj($value) { - $value = preg_replace('/\D/', '', $value); + $value = preg_replace('/[^A-Za-z0-9]/', '', (string) $value); + $value = strtoupper($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/tests/Helpers/MaskTest.php b/tests/Helpers/MaskTest.php index c359a18..7ff1e11 100644 --- a/tests/Helpers/MaskTest.php +++ b/tests/Helpers/MaskTest.php @@ -20,10 +20,14 @@ 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')); } public function testMaskCep() From 8d928616801ef9e62c03e4a5ae3dfb1704eee858 Mon Sep 17 00:00:00 2001 From: Ezequias Antunes Date: Wed, 22 Jul 2026 13:40:53 -0300 Subject: [PATCH 3/3] =?UTF-8?q?Considera=20CNPJ=20v=C3=A1lidos=20somente?= =?UTF-8?q?=20aqueles=20que=20possuem=20os=20caracteres=20em=20caixa=20alt?= =?UTF-8?q?a?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Helpers/Mask.php | 9 +++++++-- src/Helpers/Validate.php | 9 +++++++-- tests/Helpers/MaskTest.php | 1 + tests/Helpers/ValidateTest.php | 2 ++ 4 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/Helpers/Mask.php b/src/Helpers/Mask.php index 9f7de20..f607708 100644 --- a/src/Helpers/Mask.php +++ b/src/Helpers/Mask.php @@ -109,8 +109,13 @@ public static function cep($value) */ public static function cnpj($value) { - $value = preg_replace('/[^A-Za-z0-9]/', '', (string) $value); - $value = strtoupper($value); + $value = (string) $value; + + if (preg_match('/[a-z]/', $value)) { + return null; + } + + $value = preg_replace('/[^A-Z0-9]/', '', $value); 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 6d836c6..204d30b 100644 --- a/src/Helpers/Validate.php +++ b/src/Helpers/Validate.php @@ -50,8 +50,13 @@ public static function isValidCpf($cpf) */ public static function isValidCnpj($cnpj) { - $cnpj = preg_replace('/[^A-Za-z0-9]/', '', (string) $cnpj); - $cnpj = strtoupper($cnpj); + $cnpj = (string) $cnpj; + + if (preg_match('/[a-z]/', $cnpj)) { + return false; + } + + $cnpj = preg_replace('/[^A-Z0-9]/', '', $cnpj); if (!preg_match('/^[A-Z0-9]{12}[0-9]{2}$/', $cnpj)) { return false; diff --git a/tests/Helpers/MaskTest.php b/tests/Helpers/MaskTest.php index 7ff1e11..9c3adac 100644 --- a/tests/Helpers/MaskTest.php +++ b/tests/Helpers/MaskTest.php @@ -28,6 +28,7 @@ public function testeMaskCnpj() $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 f092509..18cb18e 100644 --- a/tests/Helpers/ValidateTest.php +++ b/tests/Helpers/ValidateTest.php @@ -66,6 +66,8 @@ public function cnpjProvider() ['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],