Skip to content

Commit

Permalink
add params option for some validation rules
Browse files Browse the repository at this point in the history
  • Loading branch information
sadegh19b committed Nov 26, 2020
1 parent 50199b6 commit 686cd69
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 5 deletions.
19 changes: 15 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 );
```
Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/PersianValidationServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
55 changes: 55 additions & 0 deletions src/PersianValidators.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]));
}
Expand All @@ -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]);
}
Expand Down Expand Up @@ -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.
*
Expand All @@ -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;
}

Expand Down Expand Up @@ -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);
}

Expand All @@ -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;
}
Expand Down

0 comments on commit 686cd69

Please sign in to comment.