Skip to content

Commit

Permalink
Add Brazilian ZIP Code
Browse files Browse the repository at this point in the history
  • Loading branch information
wemersonrv committed May 18, 2019
1 parent d50db25 commit 7bf43ab
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 7 deletions.
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ $rules = [
'cpf' => 'required,cpf',
'cnpj' => 'required,cnpj',
'cellphone' => 'mobile_br',
'postal_code' => 'cep',
];

$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!
'mobile_br' => 'Invalid mobile number.', // The show is yours, do as you want!
'cep' => 'The :attribute must be a valid Brazilian ZIP Code (CEP).',
];

$validator = \Validator::make($request->all(), $rules, $errorMsgs);
Expand All @@ -42,13 +44,15 @@ if($validator->fails()){

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

## Release History

* 0.4.0
* Brazilian ZIP Code (CEP)
* ADD: Brazilian Postal Code (`cep`)
* 0.3.1
* Sanitize value before validation
* CHANGE: Extract only digits (without mask chars) to validate
Expand Down
9 changes: 8 additions & 1 deletion src/CustomRulesServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Wemersonrv\CustomRules\Rules\Cpf;
use Wemersonrv\CustomRules\Rules\Cnpj;
use Wemersonrv\CustomRules\Rules\MobileBr;
use Wemersonrv\CustomRules\Rules\Cep;

class CustomRulesServiceProvider extends ServiceProvider
{
Expand All @@ -30,11 +31,17 @@ 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);
});

\Validator::extend('cep', function($attribute, $value, $parameter, $validator){
return (new Cep())->passes($attribute, $value);
});
}
}
45 changes: 45 additions & 0 deletions src/Rules/Cep.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Wemersonrv\CustomRules\Rules;

use Illuminate\Contracts\Validation\Rule;

class Cep 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

$value = preg_replace("/\D+/", "", $value); // Limpa máscara ( se houver )

$digitsPattern = '/\d{8}/'; // Regex Padrão de 8 dígitos numéricos
$repeatPattern = '/(\d)\1{7}/'; // regex Padrões repetidos: 00000000000 11111111111 etc


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

return true;
}

/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return 'The :attribute must be a valid Brazilian CPF.';
}
}
2 changes: 1 addition & 1 deletion src/Rules/MobileBr.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,6 @@ public function passes($attribute, $value)
*/
public function message()
{
return 'The :attribute must be a valid Brazilian CPF.';
return 'The :attribute must be a valid Brazilian mobile number.';
}
}

0 comments on commit 7bf43ab

Please sign in to comment.