diff --git a/src/pt-br-validator/Rules/Cnpj.php b/src/pt-br-validator/Rules/Cnpj.php index 3007621..d4685a4 100644 --- a/src/pt-br-validator/Rules/Cnpj.php +++ b/src/pt-br-validator/Rules/Cnpj.php @@ -21,7 +21,7 @@ class Cnpj implements Rule */ public function passes($attribute, $value) { - $c = preg_replace('/\D/', '', $value); + $c = preg_replace('/((?![0-9A-Z]).)/', '', strtoupper($value)); $b = [6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2]; @@ -38,13 +38,13 @@ public function passes($attribute, $value) return false; } - for ($i = 0, $n = 0; $i < 12; $n += $c[$i] * $b[++$i]); + for ($i = 0, $n = 0; $i < 12; $n += (ord($c[$i]) - 48) * $b[++$i]); if ($c[12] != ((($n %= 11) < 2) ? 0 : 11 - $n)) { return false; } - for ($i = 0, $n = 0; $i <= 12; $n += $c[$i] * $b[$i++]); + for ($i = 0, $n = 0; $i <= 12; $n += (ord($c[$i]) - 48) * $b[$i++]); if ($c[13] != ((($n %= 11) < 2) ? 0 : 11 - $n)) { return false; diff --git a/src/pt-br-validator/Rules/FormatoCnpj.php b/src/pt-br-validator/Rules/FormatoCnpj.php index 3bdb2db..e4f5307 100644 --- a/src/pt-br-validator/Rules/FormatoCnpj.php +++ b/src/pt-br-validator/Rules/FormatoCnpj.php @@ -21,7 +21,7 @@ class FormatoCnpj implements Rule */ public function passes($attribute, $value) { - return preg_match('/^\d{2}\.\d{3}\.\d{3}\/\d{4}-\d{2}$/', $value) > 0; + return preg_match('/^[0-9A-Z]{2}\.[0-9A-Z]{3}\.[0-9A-Z]{3}\/[0-9A-Z]{4}-\d{2}$/', $value) > 0; } public function message() diff --git a/tests/TestRules.php b/tests/TestRules.php index 6bc42b6..225aecc 100644 --- a/tests/TestRules.php +++ b/tests/TestRules.php @@ -127,7 +127,7 @@ public function testFormatoCpf() public function testCnpj() { $validator = \Validator::make([ - 'valido' => '16.651.801/0001-57' + 'valido' => '12.ABC.345/01DE-35' ], [ 'valido' => ['required', new \LaravelLegends\PtBrValidator\Rules\Cnpj] ]); @@ -135,7 +135,7 @@ public function testCnpj() $this->assertTrue($validator->passes()); $validator = \Validator::make([ - 'invalido' => '16.651.801/0001-52' + 'invalido' => '12.ABC.345/01DE-36' ], [ 'invalido' => ['required', new \LaravelLegends\PtBrValidator\Rules\Cnpj] ]); @@ -148,7 +148,7 @@ public function testCnpj() public function testFormatoCnpj() { $validator = \Validator::make([ - 'valido' => '16.651.801/0001-57' + 'valido' => '12.ABC.345/01DE-35' ], [ 'valido' => ['required', new \LaravelLegends\PtBrValidator\Rules\FormatoCnpj] ]); @@ -156,7 +156,7 @@ public function testFormatoCnpj() $this->assertTrue($validator->passes()); $validator = \Validator::make([ - 'invalido' => '16.651.801/000152' + 'invalido' => '12.ABC.345/01DE36' ], [ 'invalido' => ['required', new \LaravelLegends\PtBrValidator\Rules\FormatoCnpj] ]);