Skip to content

Commit

Permalink
Add Mac Address validator
Browse files Browse the repository at this point in the history
  • Loading branch information
wemersonrv committed May 18, 2019
1 parent 7f95958 commit 6b6ae18
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 5 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ $rules = [
'cellphone' => 'mobile_br',
'landline' => 'landline_br',
'postal_code' => 'cep',
'mac' => 'required|mac_address',
];

$errorMsgs = [
Expand All @@ -34,24 +35,29 @@ $errorMsgs = [
'mobile_br' => 'Invalid mobile number.', // The show is yours, do as you want!
'landline_br' => 'Invalid landline number.',
'cep' => 'The :attribute must be a valid Brazilian ZIP Code (CEP).',
'mac_address' => 'The :attribute must be a valid MAC address',
];

$validator = \Validator::make($request->all(), $rules, $errorMsgs);
if($validator->fails()){
return response($validator->errors(), 400);
}
```

## To-do List

* [x] Brazilian CPF
* [x] Brazilian CNPJ
* [x] Brazilian Mobile phone with 9 digit
* [x] Brazilian ZIP code (CEP)
* [x] Brazilian Landline phone
* [ ] Mac Address
* [ ] Brazilian states (UF)
* [x] Mac Address


## Release History
* 0.6.0
* Macc Address
* ADD: Mac Address rule (`mac_address`)
* 0.5.0
* Brazilian Landline number
* ADD: Brazilian Landline rule (`landline_br`)
Expand Down
14 changes: 12 additions & 2 deletions src/CustomRulesServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

use Illuminate\Support\ServiceProvider;

use Wemersonrv\CustomRules\Rules\Cpf;
use Wemersonrv\CustomRules\Rules\Cep;
use Wemersonrv\CustomRules\Rules\Cnpj;
use Wemersonrv\CustomRules\Rules\Cpf;
use Wemersonrv\CustomRules\Rules\LandlineBr;
use Wemersonrv\CustomRules\Rules\MacAddress;
use Wemersonrv\CustomRules\Rules\MobileBr;
use Wemersonrv\CustomRules\Rules\Cep;

class CustomRulesServiceProvider extends ServiceProvider
{
Expand Down Expand Up @@ -40,8 +42,16 @@ public function boot()
return (new MobileBr())->passes($attribute, $value);
});

\Validator::extend('landline_br', function($attribute, $value, $parameter, $validator){
return (new LandlineBr())->passes($attribute, $value);
});

\Validator::extend('cep', function($attribute, $value, $parameter, $validator){
return (new Cep())->passes($attribute, $value);
});

\Validator::extend('mac_address', function($attribute, $value, $parameter, $validator){
return (new MacAddress())->passes($attribute, $value);
});
}
}
2 changes: 1 addition & 1 deletion src/Rules/LandlineBr.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,6 @@ public function passes($attribute, $value)
*/
public function message()
{
return 'The :attribute must be a valid Brazilian mobile number.';
return 'The :attribute must be a valid Brazilian landline number.';
}
}
34 changes: 34 additions & 0 deletions src/Rules/MacAddress.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Wemersonrv\CustomRules\Rules;

use Illuminate\Contracts\Validation\Rule;

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

return preg_match(
"/^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$/", $value
);
}

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

0 comments on commit 6b6ae18

Please sign in to comment.