-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d50db25
commit 7bf43ab
Showing
4 changed files
with
63 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters