From 686cd692ab72a7c6b3bffddb8954791c31271a2b Mon Sep 17 00:00:00 2001 From: Sadegh Barzegar Date: Thu, 26 Nov 2020 04:20:33 +0330 Subject: [PATCH] add params option for some validation rules --- readme.md | 19 ++++++-- src/PersianValidationServiceProvider.php | 2 +- src/PersianValidators.php | 55 ++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 5 deletions(-) diff --git a/readme.md b/readme.md index 7cbff9e..cd036df 100644 --- a/readme.md +++ b/readme.md @@ -33,13 +33,24 @@ You can access to validation rules by passing the rules key according blew follo | persian_alpha_eng_num | Persian alphabet and numbers with english numbers |صادق۱۲34 | persian_not_accept | Doesn't accept Persian alphabet and numbers | cant be persian | shamsi_date | Check shamsi (jalali) date with format(Y/m/d) | 1373/3/19 +| shamsi_date:persian | Check shamsi (jalali) date with format(Y/m/d) with persian number | ۱۳۷۳/۳/۱۹ | shamsi_date_between:1300,1400 | Check shamsi (jalali) date with format(Y/m/d) between years | 1373/3/19 +| shamsi_date_between:1300,1400,persian | Check shamsi (jalali) date with format(Y/m/d) between years with persian number | ۱۳۷۳/۳/۱۹ | ir_mobile | Iranian mobile numbers | 00989173456789, +989173456789, 989173456789, 09173456789, 91712345678 +| ir_mobile:zero_code | Iranian mobile numbers with double zero country code | 00989173456789 +| ir_mobile:plus | Iranian mobile numbers with plus country code | +989173456789 +| ir_mobile:code | Iranian mobile numbers with country code | 989173456789 +| ir_mobile:zero | Iranian mobile numbers starts with zero | 09173456789 +| ir_mobile:without_zero | Iranian mobile numbers without first zero | 9173456789 | ir_phone | Iranian phone numbers | 37236445 | ir_phone_code | Iranian phone area code | 077, 021, ... | ir_phone_with_code | Iranian phone number with area code | 07737236445 -| ir_postal_code | Iranian postal code. | 1619735744, 16197-35744 -| ir_card_number | Iranian bank payment card numbers. | 6274129005473742 +| ir_postal_code | Iranian postal code | 1619735744, 16197-35744 +| ir_postal_code:seprate | Iranian postal code sperated | 16197-35744 +| ir_postal_code:without_seprate | Iranian postal code without seprate | 1619735744 +| ir_bank_card_number | Iranian bank payment card numbers | 6274129005473742 +| ir_bank_card_number:seprate | Iranian bank payment card numbers seprate between digits with dash | 6274-1290-0547-3742 +| ir_bank_card_number:space | Iranian bank payment card numbers seprate between digits with space | 6274 1290 0547 3742 | ir_sheba | Iranian Sheba numbers | IR062960000000100324200001 | ir_national_code | Iran melli code | 0013542419 | a_url | Check correct URL | http://google.com, https://www.google.com @@ -151,7 +162,7 @@ Validate Iranian bank payment card numbers: ``` php $input = [ '6274129005473742' ]; -$rules = [ 'ir_card_number' ]; +$rules = [ 'ir_bank_card_number' ]; Validator::make( $input, $rules ); ``` @@ -212,7 +223,7 @@ Validator::make( $request->all(), [ 'phone_code' => 'ir_phone_with_code', // Validate phone number with area code - 'card_number' => 'ir_card_number', // Validate payment card number + 'card_number' => 'ir_bank_card_number', // Validate payment card number 'postal_code' => 'ir_postal_code' // validate iran postal code format diff --git a/src/PersianValidationServiceProvider.php b/src/PersianValidationServiceProvider.php index 8146783..2a2be3b 100644 --- a/src/PersianValidationServiceProvider.php +++ b/src/PersianValidationServiceProvider.php @@ -27,7 +27,7 @@ class PersianValidationServiceProvider extends ServiceProvider 'ir_phone_code' => 'IranianPhoneAreaCode', 'ir_phone_with_code' => 'IranianPhoneWithAreaCode', 'ir_postal_code' => 'IranianPostalCode', - 'ir_card_number' => 'IranianBankCardNumber', + 'ir_bank_card_number' => 'IranianBankCardNumber', 'ir_sheba' => 'IranianBankSheba', 'ir_national_code' => 'IranianNationalCode', 'a_url' => 'CheckUrl', diff --git a/src/PersianValidators.php b/src/PersianValidators.php index 52b07d2..4a50361 100644 --- a/src/PersianValidators.php +++ b/src/PersianValidators.php @@ -89,6 +89,10 @@ public function validatePersianNotAccept($attribute, $value, $parameters) */ public function validateShamsiDate($attribute, $value, $parameters) { + if (isset($parameters[0]) && $parameters[0] == 'persian') { + $value = $this->faToEnNumbers($value); + } + $jdate = explode('/', $value); return (count($jdate) === 3 && $this->isValidjDate($jdate[0], $jdate[1], $jdate[2])); } @@ -103,6 +107,14 @@ public function validateShamsiDate($attribute, $value, $parameters) */ public function validateShamsiDateBetween($attribute, $value, $parameters) { + if (!isset($parameters[0]) && !isset($parameters[1])) { + return false; + } + + if (isset($parameters[2]) && $parameters[2] == 'persian') { + $value = $this->faToEnNumbers($value); + } + $jdate = explode('/', $value); return $this->validateShamsiDate($attribute, $value, $parameters) && ($parameters[0] <= $jdate[0] && $parameters[1] >= $jdate[0]); } @@ -147,6 +159,20 @@ private function isValidjDate($year, $month, $day) { return true; } + /** + * Convert persian numbers to english numbers + * + * @param string $string + * @return string + */ + private function faToEnNumbers($string) + { + $fa_num = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹']; + $en_num = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']; + + return str_replace($fa_num, $en_num, $string); + } + /** * Validate iranian mobile number. * @@ -157,6 +183,18 @@ private function isValidjDate($year, $month, $day) { */ public function validateIranianMobile($attribute, $value, $parameters) { + $paramsPatternMap = [ + 'zero_code' => '/^(00989){1}[0-9]{9}+$/', + 'plus' => '/^(\+989){1}[0-9]{9}+$/', + 'code' => '/^(989){1}[0-9]{9}+$/', + 'zero' => '/^(09){1}[0-9]{9}+$/', + 'without_zero' => '/^(9){1}[0-9]{9}+$/', + ]; + + if (isset($parameters[0]) && in_array($parameters[0], array_keys($paramsPatternMap))) { + return preg_match($paramsPatternMap[$parameters[0]], $value); + } + return (preg_match('/^(((98)|(\+98)|(0098)|0)(9){1}[0-9]{9})+$/', $value) || preg_match('/^(9){1}[0-9]{9}+$/', $value))? true : false; } @@ -211,6 +249,15 @@ public function validateIranianPhoneWithAreaCode($attribute, $value, $parameters */ public function validateIranianPostalCode($attribute, $value, $parameters) { + $paramsPatternMap = [ + 'seprate' => '/\b(?!(\d)\1{3})[13-9]{4}[1346-9]-[013-9]{5}\b/', + 'without_seprate' => '/\b(?!(\d)\1{3})[13-9]{4}[1346-9][013-9]{5}\b/', + ]; + + if (isset($parameters[0]) && in_array($parameters[0], array_keys($paramsPatternMap))) { + return preg_match($paramsPatternMap[$parameters[0]], $value); + } + return preg_match("/\b(?!(\d)\1{3})[13-9]{4}[1346-9]-?[013-9]{5}\b/", $value); } @@ -225,6 +272,14 @@ public function validateIranianPostalCode($attribute, $value, $parameters) */ function validateIranianBankCardNumber($attribute, $value, $parameters) { + if (isset($parameters[0]) && $parameters[0] == 'seprate') { + $value = str_replace('-', '', $value); + } + + if (isset($parameters[0]) && $parameters[0] == 'space') { + $value = str_replace(' ', '', $value); + } + if (!preg_match('/^\d{16}$/', $value)) { return false; }