Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Alphanumeric in CNPJ #79

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/pt-br-validator/Rules/Cnpj.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];

Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/pt-br-validator/Rules/FormatoCnpj.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
8 changes: 4 additions & 4 deletions tests/TestRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,15 @@ 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]
]);

$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]
]);
Expand All @@ -148,15 +148,15 @@ 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]
]);

$this->assertTrue($validator->passes());

$validator = \Validator::make([
'invalido' => '16.651.801/000152'
'invalido' => '12.ABC.345/01DE36'
], [
'invalido' => ['required', new \LaravelLegends\PtBrValidator\Rules\FormatoCnpj]
]);
Expand Down