-
-
Save sohag-pro/8a959d552035ef834fc7fdcfd5610282 to your computer and use it in GitHub Desktop.
A RIB validation rule, could be used with Laravel ^5.5 or separately
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
<?php | |
namespace App\Rules; | |
use Illuminate\Contracts\Validation\Rule; | |
/** | |
* RIB validation (valide in morocco only) | |
* | |
* @author med-ezzairi | |
* | |
*/ | |
class RIB implements Rule | |
{ | |
private $lastError = ''; | |
/** | |
* Create a new rule instance. | |
* | |
* @return void | |
*/ | |
public function __construct() | |
{ | |
// | |
} | |
/** | |
* Determine if the validation rule passes. | |
* | |
* @param string $attribute | |
* @param mixed $value | |
* @return bool | |
*/ | |
public function passes($attribute, $value) | |
{ | |
return $this->checkRIB( $value ); | |
} | |
/** | |
* Get the validation error message. | |
* | |
* @return string | |
*/ | |
public function message() | |
{ | |
//return 'The :attribute must be something here to describe the eror.'; | |
return trans('validation.rib_format'); | |
} | |
/** | |
* Verify the integriry of RIB | |
* | |
* @param string $rib | |
* @return boolean | |
*/ | |
private function checkRIB( $RIB = null ) | |
{ | |
$matchedInfo = null; | |
preg_match( '/^(\d{3})(\d{3})(\d{4})(\d{5})(\d{5})(\d{2})(\d{2})$/', $RIB, $matchedInfo ); | |
if( empty( $matchedInfo ) ){ | |
$this->lastError = "Format du RIB est invalide"; | |
return false; | |
} | |
$banque = $matchedInfo[1]; | |
$city = $matchedInfo[2]; | |
$poste = $matchedInfo[3]; | |
$identity = $matchedInfo[4]; | |
$account = $matchedInfo[5]; | |
$rang = $matchedInfo[6]; | |
$key = $matchedInfo[7]; | |
//-- | |
$C1 = (($banque * 1000 + $city) % 97); | |
$C2 = (($C1 * 10000 + $poste) % 97); | |
$C3 = (($C2 * 100000 + $identity) % 97); | |
$C4 = (($C3 * 100000 + $account) % 97); | |
$C5 = (($C4 * 100 + $rang) % 97); | |
$C6 = (($C5 * 100) % 97); | |
$new_key = 97 - $C6; | |
if( $new_key == $key ){ | |
return true; | |
} | |
$this->lastError = "RIB invalide"; | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment