diff --git a/README.md b/README.md index 305bf2a..ffefffb 100644 --- a/README.md +++ b/README.md @@ -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); @@ -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`) diff --git a/src/CustomRulesServiceProvider.php b/src/CustomRulesServiceProvider.php index 88f3a27..0b4da1f 100644 --- a/src/CustomRulesServiceProvider.php +++ b/src/CustomRulesServiceProvider.php @@ -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 @@ -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); diff --git a/src/Rules/Cnpj.php b/src/Rules/Cnpj.php new file mode 100644 index 0000000..0d6af75 --- /dev/null +++ b/src/Rules/Cnpj.php @@ -0,0 +1,51 @@ + 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