Skip to content

Commit

Permalink
Add Brazilian CNPJ
Browse files Browse the repository at this point in the history
  • Loading branch information
wemersonrv committed May 17, 2019
1 parent 84daafe commit e60457f
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,14 @@ Use it with laravel validator rules in the way you most like: Custom Request, `\
```php
$rules = [
'cpf' => 'required,cpf',
'cnpj' => 'required,cnpj',
'cellphone' => 'mobile_br',
];

$errorMsgs = [
'cpf' => 'The :attribute must be a valid Brazilian CPF.',
'cnpj' => 'The :attribute must be a valid Brazilian CNPJ.',
'cellphone' => 'Invalid mobile number.', // The show is yours, do as you want!
];

$validator = \Validator::make($request->all(), $rules, $errorMsgs);
Expand All @@ -37,13 +41,17 @@ if($validator->fails()){
## To-do List

* [x] Brazilian CPF
* [ ] Brazilian CNPJ
* [x] Brazilian CNPJ
* [x] Brazilian mobile phone with 9 digit
* [ ] Brazilian Zip code (CEP)
* [ ] Brazilian landline phone
* [ ] Exists compound (Multi column exists check)

## Release History

* 0.3.0
* Brazilian CNPJ
* ADD: Brazilian CNPJ rule (`cnpj`)
* 0.2.0
* Brazilian Mobile (with 9 digit check)
* ADD: Brazilian Mobile rule (`mobile_br`)
Expand Down
4 changes: 4 additions & 0 deletions src/CustomRulesServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Support\ServiceProvider;

use Wemersonrv\CustomRules\Rules\Cpf;
use Wemersonrv\CustomRules\Rules\Cnpj;
use Wemersonrv\CustomRules\Rules\MobileBr;

class CustomRulesServiceProvider extends ServiceProvider
Expand All @@ -28,6 +29,9 @@ public function boot()
{
\Validator::extend('cpf', function($attribute, $value, $parameter, $validator){
return (new Cpf())->passes($attribute, $value);
});
\Validator::extend('cnpj', function($attribute, $value, $parameter, $validator){
return (new Cnpj())->passes($attribute, $value);
});
\Validator::extend('mobile_br', function($attribute, $value, $parameter, $validator){
return (new MobileBr())->passes($attribute, $value);
Expand Down
51 changes: 51 additions & 0 deletions src/Rules/Cnpj.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Wemersonrv\CustomRules\Rules;

use Illuminate\Contracts\Validation\Rule;

class Cnpj implements Rule
{
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
if(!$value) return true; // Vazio, sem required antes
$digitsPattern = '/\d{14}/'; // Regex Padrão de 14 dígitos numéricos
$repeatPattern = '/(\d)\1{13}/'; // regex Padrões repetidos: 00000000000000 11111111111111 etc

if( !preg_match($digitsPattern, $value) // Padrão diferente de 14 digitos numéricos
|| preg_match($repeatPattern, $value) // Capturou sequencia
|| strlen($value) > 14 ){ // mais de 14 digitos
return false;
}
$body = substr($value, 0, 12);
$range = '543298765432';

// Dígitos verificadores
for($pass=0; $pass<2; $pass++){
$result = 0;
for($i=0; $i<strlen($body); $i++){
$result += $body[$i] * $range[$i];
}
$body .= $result % 11 ? 11 - ($result % 11) : 0;
$range = "6{$range}";
}
return $body === $value;
}

/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return 'The :attribute must be a valid Brazilian CNPJ.';
}
}

0 comments on commit e60457f

Please sign in to comment.