diff --git a/.travis.yml b/.travis.yml index 707d88c..1d6df5f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,4 +1,7 @@ language: php + +dist: trusty + php: - 5.5 - 5.6 diff --git a/_config.yml b/_config.yml new file mode 100644 index 0000000..3397c9a --- /dev/null +++ b/_config.yml @@ -0,0 +1 @@ +theme: jekyll-theme-architect \ No newline at end of file diff --git a/composer.json b/composer.json index 62334bb..75545be 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,24 @@ "Anetwork\\Validation\\": "src" } }, + "extra": { + "laravel": { + "providers": [ + "Anetwork\\Validation\\PersianValidationServiceProvider" + ] + } + }, + "require-dev": { + "phpunit/phpunit": "3.7.*" + }, "require": { "illuminate/support": "^5.2" + }, + "extra": { + "laravel": { + "providers": [ + "Anetwork\\Validation\\PersianValidationServiceProvider" + ] + } } } diff --git a/lang/en.php b/lang/en.php new file mode 100644 index 0000000..0550d2a --- /dev/null +++ b/lang/en.php @@ -0,0 +1,24 @@ + 'must be a persian alpahbet.', + 'persian_num' => 'must be a persian number.', + 'persian_alpha_num' => 'must be a persian alpahbet or number.', + 'iran_mobile' => 'must be a iran mobile number.', + 'sheba' => 'must be a sheba number.', + 'melli_code' => 'must be a iran melli code.', + 'is_not_persian' => 'could not be contain persian alpahbet or number.', + 'limited_array' => 'must ba a array and contain values you define not more.', + 'unsigned_num' => 'must be an unsigned number.', + 'alpha_space' => 'must be alphabet and space.', + 'a_url' => 'url is not correct.', + 'a_domain' => 'domain is not correct.', + 'more' => 'must be more than parameter.', + 'less' => 'must be less than parameter.', + 'iran_phone' => 'must be a iran phone number.', + 'card_number' => 'must be a valid payment card number.', + 'address' => 'must be a correct address.', + 'iran_postal_code' => 'must be a iran postal code.', + 'package_name' => 'must be a valid package name.', + 'float_num' => 'must be float or integer number.' +]; diff --git a/lang/fa.php b/lang/fa.php new file mode 100644 index 0000000..eb73fb0 --- /dev/null +++ b/lang/fa.php @@ -0,0 +1,27 @@ + 'حروف وارد شده باید فارسی باشد.', + 'persian_num' => 'عدد وارد شده باید فارسی باشد.', + 'persian_alpha_num' => 'حروف و عدد وارد شده باید فارسی باشد.', + 'iran_mobile' => 'شماره همراه قابل قبول نیست.', + 'sheba' => 'شماره شبا قابل قبول نیست.', + 'melli_code' => 'کد ملی قابل قبول نیست.', + 'is_not_persian' => 'حروف غیر لاتین قابل قبول نیست.', + 'limited_array' => 'فيلد مورد نظر قبل قبول نيست.', + 'unsigned_num' => 'عدد مورد نظر قابل قبول نیست.', + 'alpha_space' => 'باید شامل حروف و فاصله باشد.', + 'a_url' => 'آدرس قابل قبول نیست.', + 'a_domain' => 'دامنه قابل قبول نیست.', + 'more' => 'فیلد باید بزرگتر باشد.', + 'less' => 'فیلد باید کوچیکتر باشد.', + 'iran_phone' => 'شماره تلفن قابل قبول نیست.', + 'iran_phone_with_area_code' => 'شماره تلفن قابل قبول نیست.', + 'card_number' => 'شماره کارت قابل قبول نیست.', + 'address' => 'آدرس وارد شده قابل قبول نیست.', + 'iran_postal_code' => 'کدپستی وارد شده قابل قبول نیست.', + 'package_name' => 'نام پکیج صحیح نیست.', + 'float_num' => 'عدد وارد شده باید اعشاری یا صحیح باشد.', + +]; diff --git a/readme.md b/readme.md index b7b9d7b..1eecfb4 100644 --- a/readme.md +++ b/readme.md @@ -1,5 +1,8 @@ [![Build Status](https://travis-ci.org/anetwork/validation.svg?branch=master)](https://travis-ci.org/anetwork/validation) [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/anetwork/validation/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/anetwork/validation/?branch=master) +[![Latest Stable Version](https://poser.pugx.org/anetwork/validation/v/stable)](https://packagist.org/packages/anetwork/validation) +[![Total Downloads](https://poser.pugx.org/anetwork/validation/downloads)](https://packagist.org/packages/anetwork/validation) +[![License](https://poser.pugx.org/anetwork/validation/license)](https://github.com/anetwork/validation/blob/master/LICENSE.md) # Laravel Persian Validation @@ -22,13 +25,16 @@ Via Composer $ composer require Anetwork/Validation ``` -##Config +## Config Add the following provider to providers part of config/app.php ``` php Anetwork\Validation\PersianValidationServiceProvider::class ``` +## vendor:publish +You can run vendor:publish command to have custom lang file of package on this path ( resources/lang/validation ) + ## Usage You can access to validation rules by passing the rules key according blew following table: @@ -38,22 +44,23 @@ You can access to validation rules by passing the rules key according blew follo | persian_alpha | Persian alphabet | | persian_num | Persian numbers | | persian_alpha_num | Persian alphabet and numbers | -| iran_mobile | Iran mobile number | -| sheba_number | Iran Sheba number | -| melli_code | Iran Melli code | -| is_not_persian | Doesn't accept persain alphabet and number | -| limited_array | Checked variable is array and array must be lesser and equal than parameter | -| unsigned_num | Checked variable is integer and unsigned | -| alpha_space | Accept alphabet and space | -| a_url | Checked correct url | -| a_domain | Checked correct domain | -| more | Checked value be max and not equal too| -| less | Checked value be min and not equal too | -| iran_phone | Iran phone number | - - - - +| iran_mobile | Iran mobile numbers | +| sheba | Iran Sheba numbers | +| melli_code | Iran melli code | +| is_not_persian | Doesn't accept Persian alphabet and numbers | +| limited_array | Check variable is array and array must be lesser and equal than parameter | +| unsigned_num | Check variable is unsigned numbers | +| alpha_space | Accept Persian, English and ... alphabet, space character| +| a_url | Check correct URL | +| a_domain | Check correct Domain | +| more | Check value be max and not equal too| +| less | Check value be min and not equal too | +| iran_phone | Iran phone numbers | +| card_number | Payment card numbers | +| address | Accept Persian, English and ... alphabet, Persian and English numbers and some special characters| +| iran_postal_code | Iran postal code | +| package_name | Check APK package name | +| float_num | Check float and integer unsigned number | ### Persian Alpha Accept Persian language alphabet according to standard Persian, this is the way you can use this validation rule: @@ -100,7 +107,7 @@ Validator::make( $input, $rules ); ``` ### Sheba number -Validate iranian bank sheba number: +Validate Iran bank sheba numbers: ``` $input = [ 'IR062960000000100324200001' ]; @@ -111,7 +118,7 @@ Validator::make( $input, $rules ); ``` ### Iran national code -Validate iran's national code (melli-code): +Validate Iran national code (melli-code): ``` $input = [ '3240175800' ]; @@ -121,37 +128,77 @@ $rules = [ 'melli_code' ]; Validator::make( $input, $rules ); ``` +### Payment card number +Validate Iran payment card numbers: + +``` +$input = [ '6274129005473742' ]; + +$rules = [ 'card_number' ]; + +Validator::make( $input, $rules ); +``` + +### Iran postal code +Validate Iran postal code: + +``` +$input = [ '167197-35744' ]; + +$rules = [ 'iran_postal_code' ]; + +Validator::make( $input, $rules ); + + +$input = [ '16719735744' ]; + +$rules = [ 'iran_postal_code' ]; + +Validator::make( $input, $rules ); + +``` + ## More Here is full list of Anetwork validation rules usage: ``` php Validator::make( $request->all(), [ - 'name' => 'persian_alpha|unique|max:25', // Validate Persian alphabet, unique and max to 25 characters + 'name' => 'persian_alpha|unique|max:25', // Validate Persian alphabet, unique and max to 25 characters + + 'age' => 'persian_num|required', // Validate Persian numbers and check it's required + + 'city' => 'persian_alpha_num|min:10', // Validate persian alphabet & numbers at least 10 digit accepted + + 'mobile' => 'iran_mobile', // Validate mobile number + + 'sheba_number' => 'sheba', // Validate sheba number of bank account + + 'melli_code' => 'melli_code', // Validate melli code number - 'age' => 'persian_num|required', // Validate Persian numbers and check it's required + 'latin_name' => 'is_not_persian', // Validate alphabet and doesn't contain Persian alphabet or number - 'address' => 'persian_alpha_num|min:10', // Validate persian alphabet & numbers at least 10 digit accepted + 'your_array' => 'limited_array:2', // Validate your array variable and must be contian 2 member or lesser - 'mobile' => 'iran_mobile', // Validate mobile number + 'url' => 'a_url', // Validate url - 'sheba_number' => 'sheba', // Validate sheba number of bank account + 'domain' => 'a_domain', // Validate domain - 'melli_code' => 'melli_code', // Validate melli code number + 'number' => 'more:10', // Validate value be more than parameter - 'latin_name' => 'is_not_persian' // Validate latin name doesn't contain persian alphabet or number + 'number' => 'less:10', // Validate value be less than parameter - 'your_array' => 'limited_array:2' // Validate your array variable and must be contian 2 member or lesser + 'phone' => 'iran_phone', // Validate phone number - 'url' => 'a_url' // Validate url + 'card_number' => 'card_number', // Validate payment card number - 'domain' => 'a_domain' // Validate domain + 'address' => 'address' // validate Persian, English and ... alphabet, Persian and English numbers and some special characters - 'more' => 'more:10' // Validate value be more than parameter + 'postal_code' => 'iran_postal_code' // validate iran postal code format - 'less' => 'less:10' // Validate value be less than parameter + 'package_name' => 'package_name' // validate APK package name - 'phone' => 'iran_phone' // Validate phone number + 'number' => 'float_num' // validate float and integer unsigned number ]); ``` diff --git a/src/Config.php b/src/Config.php new file mode 100644 index 0000000..4738559 --- /dev/null +++ b/src/Config.php @@ -0,0 +1,9 @@ + 5.3, +]; diff --git a/src/PersianValidation.php b/src/PersianValidation.php deleted file mode 100644 index 645e274..0000000 --- a/src/PersianValidation.php +++ /dev/null @@ -1,339 +0,0 @@ - - * @since May 21, 2016 - */ -class PersianValidation -{ - //variable of class - protected $status; - - /** - * validate persian alphabet and space - * @param $attribute $value - * @author Shahrokh Niakan - * @since May 21, 2016 - * @return boolean - */ - public function Alpha($attribute, $value) - { - - $this->status = preg_match("/^[\x{600}-\x{6FF}\s]+$/u", $value); - - return ( $this->status ? true : false ); - - } - - /** - * validate persian number - * @param $attribute $value - * @author Shahrokh Niakan - * @since May 21, 2016 - * @return boolean - */ - public function Num($attribute, $value) - { - - $this->status = preg_match('/^[\x{6F0}-\x{6F9}]+$/u', $value); - - return ( $this->status ? true : false ); - - } - - /** - * validate persian alphabet, number and space - * @param $attribute $value - * @author Shahrokh Niakan - * @since May 21, 2016 - * @return boolean - */ - public function AlphaNum($attribute, $value) - { - - $this->status = preg_match('/^[\x{600}-\x{6FF}\s]+$/u', $value); - - return ( $this->status ? true : false ); - - } - - /** - * validate mobile number - * @param $attribute $value - * @author Shahrokh Niakan - * @since May 21, 2016 - * @return boolean - */ - public function IranMobile($attribute, $value) - { - - $this->status = preg_match('/^(((98)|(\+98)|(0098)|0)(90|91|92|93){1}[0-9]{8})+$/', $value); - - return ( $this->status ? true : false ); - - } - - /** - * validate sheba number - * @param $attribute $value - * @author Shahrokh Niakan - * @since May 21, 2016 - * @return boolean - */ - public function Sheba($attribute, $value) - { - - $ibanReplaceValues = array(); - - if (!empty($value)) { - $value = preg_replace('/[\W_]+/', '', strtoupper($value)); - - if (( 4 > strlen($value) || strlen($value) > 34 ) || ( is_numeric($value [ 0 ]) || is_numeric($value [ 1 ]) ) || ( ! is_numeric($value [ 2 ]) || ! is_numeric($value [ 3 ]) )) { - return false; - } - - $ibanReplaceChars = range('A', 'Z'); - - foreach (range(10, 35) as $tempvalue) { - $ibanReplaceValues[] = strval($tempvalue); - } - - - $tmpIBAN = substr($value, 4) . substr($value, 0, 4); - - $tmpIBAN = str_replace($ibanReplaceChars, $ibanReplaceValues, $tmpIBAN); - - $tmpValue = intval(substr($tmpIBAN, 0, 1)); - - for ($i = 1; $i < strlen($tmpIBAN); $i++) { - $tmpValue *= 10; - - $tmpValue += intval(substr($tmpIBAN, $i, 1)); - - $tmpValue %= 97; - } - - if ($tmpValue != 1) { - return false; - } - - return true; - } - - return false; - - } - - /** - * validate meliCode number - * @param $attribute $value - * @author Shahrokh Niakan - * @since May 21, 2016 - * @return boolean - */ - public function MelliCode($attribute, $value) - { - - $sub = 0; - - if (!preg_match('/^\d{8,10}$/', $value)) { - return false; - } - - - if (strlen($value) == 8) { - $value = '00' . $value; - } elseif (strlen($value) == 9) { - $value = '0' . $value; - } - - for ($i = 0; $i <= 8; $i++) { - $sub = $sub + ( $value[$i] * ( 10 - $i ) ); - } - - if (( $sub % 11 ) < 2) { - $control = ( $sub % 11 ); - } else { - $control = 11 - ( $sub % 11 ); - } - - if ($value[9] == $control) { - return true; - } else { - return false; - } - - } - - /** - * validate string that is not contain persian alphabet and number - * @param $attribute $value - * @author Shahrokh Niakan - * @since June 13, 2016 - * @return boolean - */ - public function IsNotPersian($attribute, $value) - { - - if (is_string($value)) { - - $this->status = preg_match("/[\x{600}-\x{6FF}]/u", $value); - - return ( $this->status ? false : true ); - - } - - return false; - - } - - /** - * validate array with custom count of array - * @param $attribute $value - * @param array $parameters - * @author Shahrokh Niakan - * @since June 13, 2016 - * @return boolean - */ - public function LimitedArray($attribute, $value, $parameters) - { - - if (is_array($value)) { - - if (isset($parameters[0])) { - - return ( (count($value) <= $parameters[0]) ? true : false ); - - } else { - - return true; - - } - - } - - return false; - - } - - /** - * validate number to be unsigned - * @param $attribute $value - * @author Shahrokh Niakan - * @since July 22, 2016 - * @return boolean - */ - public function UnSignedNum($attribute, $value) { - - $this->status = preg_match('/^\d+$/', $value); - - return ( $this->status ? true : false ); - - } - - /** - * validate alphabet and spaces - * @param $attribute $value - * @author Shahrokh Niakan - * @since Agu 3, 2016 - * @return boolean - */ - public function AlphaSpace($attribute, $value) - { - - $this->status = preg_match("/^[\pL\s\-]+$/u", $value); - - return ( $this->status ? true : false ); - - } - - /** - * validate Url - * @param $attribute $value - * @author Shahrokh Niakan - * @since Agu 17, 2016 - * @return boolean - */ - public function Url($attribute, $value) - { - - $this->status = preg_match("/^HTTP|http(s)?:\/\/(www\.)?[A-Za-z0-9]+([\-\.]{1,2}[A-Za-z0-9]+)*\.[A-Za-z]{2,40}(:[0-9]{1,40})?(\/.*)?+$/", $value); - - return ( $this->status ? true : false ); - - } - - /** - * validate Domain - * @param $attribute $value - * @author Shahrokh Niakan - * @since Agu 17, 2016 - * @return boolean - */ - public function Domain($attribute, $value) - { - - $this->status = preg_match("/((www\.)?[A-Za-z0-9]+([\-\.]{1,2}[A-Za-z0-9]+)*\.[A-Za-z]{2,40}(:[0-9]{1,40})?(\/.*)?)/", $value); - - return ( $this->status ? true : false ); - - } - - /** - * value must be more than parameters - * @param $attribute $value $parameters - * @author Shahrokh Niakan - * @since Agu 24, 2016 - * @return boolean - */ - public function More($attribute, $value, $parameters) - { - - if ( isset( $parameters[0] ) ) { - - return ( $value > $parameters[0] ? true : false ); - - } - - return false; - - } - - /** - * value must be less than parameters - * @param $attribute $value $parameters - * @author Shahrokh Niakan - * @since Agu 24, 2016 - * @return boolean - */ - public function Less($attribute, $value, $parameters) - { - - if ( isset( $parameters[0] ) ) { - - return ( $value < $parameters[0] ? true : false ); - - } - - return false; - - } - - /** - * iran phone number - * @param $attribute $value - * @author Shahrokh Niakan - * @since Agu 24, 2016 - * @return boolean - */ - public function IranPhone($attribute, $value) - { - - $this->status = preg_match('/^((0)([1-9]{2})([0-9]{8}))+$/', $value) ; - - return ( $this->status ? true : false ); - - } - -} diff --git a/src/PersianValidationServiceProvider.php b/src/PersianValidationServiceProvider.php index 8b00253..7d3facd 100644 --- a/src/PersianValidationServiceProvider.php +++ b/src/PersianValidationServiceProvider.php @@ -4,6 +4,7 @@ use Illuminate\Support\ServiceProvider; use Validator; +use App; /** * @author Shahrokh Niakan @@ -12,211 +13,70 @@ class PersianValidationServiceProvider extends ServiceProvider { - // variable of class - protected $new_message; - + /** + * @var array + */ + private $validationRules = [ + 'persian_alpha' => 'Alpha', + 'persian_num' => 'Num', + 'persian_alpha_num' => 'AlphaNum', + 'iran_mobile' => 'IranMobile', + 'sheba' => 'Sheba', + 'melli_code' => 'MelliCode', + 'is_not_persian' => 'IsNotPersian', + 'limited_array' => 'LimitedArray', + 'unsigned_num' => 'UnSignedNum', + 'alpha_space' => 'AlphaSpace', + 'a_url' => 'Url', + 'a_domain' => 'Domain', + 'more' => 'More', + 'less' => 'Less', + 'iran_phone' => 'IranPhone', + 'iran_phone_with_area_code' => 'IranPhoneWithAreaCode', + 'card_number' => 'CardNumber', + 'address' => 'Address', + 'iran_postal_code' => 'IranPostalCode', + 'package_name' => 'PackageName', + ]; /** - * create custom validation rules and messages - * @author Shahrokh Niakan - * @since May 25, 2016 - * @return void - */ + * create custom validation rules and messages + * @author Shahrokh Niakan + * @since May 25, 2016 + * @return void + */ public function boot() { + // publish lang file to resources/lang/validation + $this->publishes([ + __DIR__ . '/../lang/' . App::getLocale() . '.php' => resource_path('lang/validation/' . App::getLocale() . '.php'), + ]); - // create custom rule for persian alphabet - Validator::extend('persian_alpha', 'PersianValidation@Alpha'); - - // create custom message for persian alphabet - Validator::replacer('persian_alpha', function ($message, $attribute) { - - $this->new_message = "The $attribute must be a persian alphabet."; - - return str_replace($message, $this->new_message, $message); - - }); - - // create custom rule for persian number - Validator::extend('persian_num', 'PersianValidation@Num'); - - // create custom message for persian number - Validator::replacer('persian_num', function ($message, $attribute) { - - $this->new_message = "The $attribute must be a persian number."; - - return str_replace($message, $this->new_message, $message); - - }); - - // create custom rule for persian alphabet and number - Validator::extend('persian_alpha_num', 'PersianValidation@AlphaNum'); - - // create custom message for persian alphabet and number - Validator::replacer('persian_alpha_num', function ($message, $attribute) { - - $this->new_message = "The $attribute must be a persian alpahbet or number."; - - return str_replace($message, $this->new_message, $message); - - }); - - // create custom rule for mobile - Validator::extend('iran_mobile', 'PersianValidation@IranMobile'); - - // create custom message for mobile - Validator::replacer('iran_mobile', function ($message, $attribute) { - - $this->new_message = "The $attribute must be a iran mobile number."; - - return str_replace($message, $this->new_message, $message); - - }); - - // create custom rule for sheba number - Validator::extend('sheba', 'PersianValidation@Sheba'); - - // create custom message for sheba number - Validator::replacer('sheba', function ($message, $attribute) { - - $this->new_message = "The $attribute must be a sheba number."; - - return str_replace($message, $this->new_message, $message); - - }); - - // create custom rule for melliCode - Validator::extend('melli_code', 'PersianValidation@MelliCode'); - - // create custom message for melliCode - Validator::replacer('melli_code', function ($message, $attribute) { - - $this->new_message = "The $attribute must be a iran melli code."; - - return str_replace($message, $this->new_message, $message); - - }); - - // create custom rule for NotPersian - Validator::extend('is_not_persian', 'PersianValidation@IsNotPersian'); - - // create custom message for NotPersian - Validator::replacer('is_not_persian', function ($message, $attribute) { - - $this->new_message = "The $attribute could not be contain persian alpahbet or number."; - - return str_replace($message, $this->new_message, $message); - - }); - - // create custom rule for LimitArray - Validator::extend('limited_array', 'PersianValidation@LimitedArray'); - - // create custom message for LimitArray - Validator::replacer('limited_array', function ($message, $attribute) { - - $this->new_message = "The $attribute must ba a array and contain values you define not more."; - - return str_replace($message, $this->new_message, $message); - - }); - - // create custom rule for unsigned_num - Validator::extend('unsigned_num', 'PersianValidation@UnSignedNum'); - - // create custom message for unsigned_num - Validator::replacer('unsigned_num', function ($message, $attribute) { - - $this->new_message = "The $attribute must be an integer and unsigned."; - - return str_replace($message, $this->new_message, $message); - - }); - - // create custom rule for unsigned_num - Validator::extend('alpha_space', 'PersianValidation@AlphaSpace'); - - // create custom message for unsigned_num - Validator::replacer('alpha_space', function ($message, $attribute) { - - $this->new_message = "The $attribute must be alphabet."; - - return str_replace($message, $this->new_message, $message); - - }); - - // create custom rule for url - Validator::extend('a_url', 'PersianValidation@Url'); - - // create custom message for unsigned_num - Validator::replacer('a_url', function ($message, $attribute) { - - $this->new_message = "The $attribute is not correct url."; - - return str_replace($message, $this->new_message, $message); - - }); - - // create custom rule for domain - Validator::extend('a_domain', 'PersianValidation@Domain'); - - // create custom message for unsigned_num - Validator::replacer('a_domain', function ($message, $attribute) { - - $this->new_message = "The $attribute is not correct domain."; - - return str_replace($message, $this->new_message, $message); - - }); - - // create custom rule for domain - Validator::extend('more', 'PersianValidation@More'); - - // create custom message for unsigned_num - Validator::replacer('more', function ($message, $attribute) { - - $this->new_message = "The $attribute must be more than parameter."; - - return str_replace($message, $this->new_message, $message); - - }); - - // create custom rule for domain - Validator::extend('less', 'PersianValidation@Less'); - - // create custom message for unsigned_num - Validator::replacer('less', function ($message, $attribute) { - - $this->new_message = "The $attribute must be less than parameter."; - - return str_replace($message, $this->new_message, $message); - - }); - - // create custom rule for domain - Validator::extend('iran_phone', 'PersianValidation@IranPhone'); - - // create custom message for unsigned_num - Validator::replacer('iran_phone', function ($message, $attribute) { - - $this->new_message = "The $attribute must be a iran phone number."; + foreach($this->validationRules as $name => $method) + { + Validator::extend($name, 'ValidationRules@'.$method); - return str_replace($message, $this->new_message, $message); + Validator::replacer($name, 'ValidationMessages@Msg'); + } - }); + // create custom rule for PackageName + Validator::extend('float_num', 'ValidationRules@FloatNum'); + // create custom message for PackageName + Validator::replacer('float_num', 'ValidationMessages@Msg'); } /** - * register PersianValidation service - * @author Shahrokh Niakan - * @since May 31, 2016 - * @return void - */ + * register PersianValidation service + * @author Shahrokh Niakan + * @since May 31, 2016 + * @return void + */ public function register() { + $this->app->bind('ValidationRules', 'Anetwork\Validation\ValidationRules'); - $this->app->bind('PersianValidation', 'Anetwork\Validation\PersianValidation'); + $this->app->bind('ValidationMessages', 'Anetwork\Validation\ValidationMessages'); } } diff --git a/src/ValidationMessages.php b/src/ValidationMessages.php new file mode 100644 index 0000000..c4736cd --- /dev/null +++ b/src/ValidationMessages.php @@ -0,0 +1,84 @@ + + * @since Sep 11, 2016 + */ +class ValidationMessages +{ + /** + * @var string + */ + protected $lang; + + /** + * @var array + */ + protected $config; + + /** + * @var array + */ + protected static $messages; + + /** + * @var array + */ + protected static $app; + + /** + * @author Shahrokh Niakan + * @since Sep 21, 2016 + */ + public function __construct() + { + $this->lang = App::getLocale(); + + if(! file_exists(resource_path('lang/validation/' . $this->lang . '.php'))){ + $this->config = include __DIR__ . '/../lang/' . $this->lang . '.php'; + } else { + $this->config = include resource_path('lang/validation/' . $this->lang . '.php'); + } + } + + /** + * set user custom messeages + * @param $validator + * @author Shahrokh Niakan + * @since Jun 6, 2017 + */ + public static function setCustomMessages( $validator ) + { + self::$app = include __DIR__ . '/Config.php'; + + if ( $validator ) { + if ( round(App::version(), 1) > self::$app['version'] ) { + self::$messages = $validator->customMessages; + } else { + self::$messages = $validator->getCustomMessages(); + } + } + } + + /** + * get validations message + * @param $message + * @param $attribute + * @param $rule + * @author Shahrokh Niakan + * @since Jun 10, 2017 + * @return string + */ + public function Msg($message, $attribute, $rule) + { + if ( isset( self::$messages[$rule] ) ) { + return str_replace($message, self::$messages[$rule], $message); + } + + return str_replace($message, $this->config[ $rule ], $message); + } + +} diff --git a/src/ValidationRules.php b/src/ValidationRules.php new file mode 100644 index 0000000..515dbef --- /dev/null +++ b/src/ValidationRules.php @@ -0,0 +1,515 @@ + + * @since May 21, 2016 + */ +class ValidationRules +{ + /** + * @var boolean + */ + protected $status; + + /** + * validate persian alphabet and space + * @param $attribute + * @param $value + * @param $parameters + * @param $validator + * @author Shahrokh Niakan + * @since May 21, 2016 + * @return boolean + */ + public function Alpha($attribute, $value, $parameters, $validator) + { + ValidationMessages::setCustomMessages( $validator ); + + $this->status = (bool) preg_match("/^[\x{600}-\x{6FF}\x{200c}\x{064b}\x{064d}\x{064c}\x{064e}\x{064f}\x{0650}\x{0651}\s]+$/u", $value); + + return $this->status ; + + } + + /** + * validate persian number + * @param $attribute + * @param $value + * @param $parameters + * @param $validator + * @author Shahrokh Niakan + * @since May 21, 2016 + * @return boolean + */ + public function Num($attribute, $value, $parameters, $validator) + { + ValidationMessages::setCustomMessages( $validator ); + + $this->status = (bool) preg_match('/^[\x{6F0}-\x{6F9}]+$/u', $value); + + return $this->status ; + } + + /** + * validate persian alphabet, number and space + * @param $attribute + * @param $value + * @param $parameters + * @param $validator + * @author Shahrokh Niakan + * @since May 21, 2016 + * @return boolean + */ + public function AlphaNum($attribute, $value, $parameters, $validator) + { + ValidationMessages::setCustomMessages( $validator ); + + $this->status = (bool) preg_match('/^[\x{600}-\x{6FF}\x{200c}\x{064b}\x{064d}\x{064c}\x{064e}\x{064f}\x{0650}\x{0651}\s]+$/u', $value); + + return $this->status; + } + + /** + * validate mobile number + * @param $attribute + * @param $value + * @param $parameters + * @param $validator + * @author Shahrokh Niakan + * @since May 21, 2016 + * @return boolean + */ + public function IranMobile($attribute, $value, $parameters, $validator) + { + ValidationMessages::setCustomMessages( $validator ); + + if ((bool) preg_match('/^(((98)|(\+98)|(0098)|0)(9){1}[0-9]{9})+$/', $value) || (bool) preg_match('/^(9){1}[0-9]{9}+$/', $value)) + return true; + + return false; + } + + /** + * validate sheba number + * @param $attribute + * @param $value + * @param $parameters + * @param $validator + * @author Shahrokh Niakan + * @since May 21, 2016 + * @return boolean + */ + public function Sheba($attribute, $value, $parameters, $validator) + { + ValidationMessages::setCustomMessages( $validator ); + + $ibanReplaceValues = array(); + + if (!empty($value)) { + $value = preg_replace('/[\W_]+/', '', strtoupper($value)); + + if (( 4 > strlen($value) || strlen($value) > 34 ) || ( is_numeric($value [ 0 ]) || is_numeric($value [ 1 ]) ) || ( ! is_numeric($value [ 2 ]) || ! is_numeric($value [ 3 ]) )) { + return false; + } + + $ibanReplaceChars = range('A', 'Z'); + + foreach (range(10, 35) as $tempvalue) { + $ibanReplaceValues[] = strval($tempvalue); + } + + + $tmpIBAN = substr($value, 4) . substr($value, 0, 4); + + $tmpIBAN = str_replace($ibanReplaceChars, $ibanReplaceValues, $tmpIBAN); + + $tmpValue = intval(substr($tmpIBAN, 0, 1)); + + for ($i = 1; $i < strlen($tmpIBAN); $i++) { + $tmpValue *= 10; + + $tmpValue += intval(substr($tmpIBAN, $i, 1)); + + $tmpValue %= 97; + } + + if ($tmpValue != 1) { + return false; + } + + return true; + } + + return false; + } + + /** + * validate meliCode number + * @param $attribute + * @param $value + * @param $parameters + * @param $validator + * @author Shahrokh Niakan + * @since May 21, 2016 + * @return boolean + */ + public function MelliCode($attribute, $value, $parameters, $validator) + { + ValidationMessages::setCustomMessages( $validator ); + + if (!preg_match('/^\d{8,10}$/', $value) || preg_match('/^[0]{10}|[1]{10}|[2]{10}|[3]{10}|[4]{10}|[5]{10}|[6]{10}|[7]{10}|[8]{10}|[9]{10}$/', $value)) { + return false; + } + + $sub = 0; + + if (strlen($value) == 8) { + $value = '00' . $value; + } elseif (strlen($value) == 9) { + $value = '0' . $value; + } + + for ($i = 0; $i <= 8; $i++) { + $sub = $sub + ( $value[$i] * ( 10 - $i ) ); + } + + if (( $sub % 11 ) < 2) { + $control = ( $sub % 11 ); + } else { + $control = 11 - ( $sub % 11 ); + } + + if ($value[9] == $control) { + return true; + } else { + return false; + } + + } + + /** + * validate string that is not contain persian alphabet and number + * @param $attribute + * @param $value + * @param $parameters + * @param $validator + * @author Shahrokh Niakan + * @since June 13, 2016 + * @return boolean + */ + public function IsNotPersian($attribute, $value, $parameters, $validator) + { + ValidationMessages::setCustomMessages( $validator ); + + if (is_string($value)) { + + $this->status = (bool) preg_match("/[\x{600}-\x{6FF}]/u", $value); + + return !$this->status; + + } + + return false; + } + + /** + * validate array with custom count of array + * @param $attribute + * @param $value + * @param $parameters + * @param $validator + * @author Shahrokh Niakan + * @since June 13, 2016 + * @return boolean + */ + public function LimitedArray($attribute, $value, $parameters, $validator) + { + ValidationMessages::setCustomMessages( $validator ); + + if (is_array($value)) { + + if (isset($parameters[0])) { + + return ( (count($value) <= $parameters[0]) ? true : false ); + + } else { + + return true; + + } + + } + + return false; + } + + /** + * validate number to be unsigned + * @param $attribute + * @param $value + * @param $parameters + * @param $validator + * @author Shahrokh Niakan + * @since July 22, 2016 + * @return boolean + */ + public function UnSignedNum($attribute, $value, $parameters, $validator) + { + ValidationMessages::setCustomMessages( $validator ); + + $this->status = (bool) preg_match('/^\d+$/', $value); + + return $this->status; + } + + /** + * validate alphabet and spaces + * @param $attribute + * @param $value + * @param $parameters + * @param $validator + * @author Shahrokh Niakan + * @since Agu 3, 2016 + * @return boolean + */ + public function AlphaSpace($attribute, $value, $parameters, $validator) + { + ValidationMessages::setCustomMessages( $validator ); + + $this->status = (bool) preg_match("/^[\pL\s\x{200c}\x{064b}\x{064d}\x{064c}\x{064e}\x{064f}\x{0650}\x{0651}]+$/u", $value); + + return $this->status; + } + + /** + * validate Url + * @param $attribute + * @param $value + * @param $parameters + * @param $validator + * @author Shahrokh Niakan + * @since Agu 17, 2016 + * @return boolean + */ + public function Url($attribute, $value, $parameters, $validator) + { + ValidationMessages::setCustomMessages( $validator ); + + $this->status = (bool) preg_match("/^(HTTP|http(s)?:\/\/(www\.)?[A-Za-z0-9]+([\-\.]{1,2}[A-Za-z0-9]+)*\.[A-Za-z]{2,40}(:[0-9]{1,40})?(\/.*)?)$/", $value); + + return $this->status; + } + + /** + * validate Domain + * @param $attribute + * @param $value + * @param $parameters + * @param $validator + * @author Shahrokh Niakan + * @since Agu 17, 2016 + * @return boolean + */ + public function Domain($attribute, $value, $parameters, $validator) + { + ValidationMessages::setCustomMessages( $validator ); + + $this->status = (bool) preg_match("/^((www\.)?(\*\.)?[A-Za-z0-9]+([\-\.]{1,2}[A-Za-z0-9]+)*\.[A-Za-z]{2,40}(:[0-9]{1,40})?(\/.*)?)$/", $value); + + return $this->status; + } + + /** + * value must be more than parameters + * @param $attribute + * @param $value + * @param $parameters + * @param $validator + * @author Shahrokh Niakan + * @since Agu 24, 2016 + * @return boolean + */ + public function More($attribute, $value, $parameters, $validator) + { + ValidationMessages::setCustomMessages( $validator ); + + if ( isset( $parameters[0] ) ) { + + return ( $value > $parameters[0] ? true : false ); + + } + + return false; + } + + /** + * value must be less than parameters + * @param $attribute + * @param $value + * @param $parameters + * @param $validator + * @author Shahrokh Niakan + * @since Agu 24, 2016 + * @return boolean + */ + public function Less($attribute, $value, $parameters, $validator) + { + ValidationMessages::setCustomMessages( $validator ); + + if ( isset( $parameters[0] ) ) { + + return ( $value < $parameters[0] ? true : false ); + + } + + return false; + } + + /** + * iran phone number + * @param $attribute + * @param $value + * @param $parameters + * @param $validator + * @author Shahrokh Niakan + * @since Agu 24, 2016 + * @return boolean + */ + public function IranPhone($attribute, $value, $parameters, $validator) + { + ValidationMessages::setCustomMessages( $validator ); + + $this->status = (bool) preg_match('/^[2-9][0-9]{7}+$/', $value) ; + + return $this->status; + } + + /** + * iran phone number with area code + * @param $attribute + * @param $value + * @param $parameters + * @param $validator + * @author Amir Hosseini + * @since Jan 28, 2019 + * @return boolean + */ + public function IranPhoneWithAreaCode($attribute, $value, $parameters, $validator) + { + ValidationMessages::setCustomMessages( $validator ); + + $this->status = (bool) preg_match('/^(0[1-9]{2})[2-9][0-9]{7}+$/', $value) ; + + return $this->status; + } + + /** + * payment card number validation + * depending on 'http://www.aliarash.com/article/creditcart/credit-debit-cart.htm' article + * + * @param $attribute + * @param $value + * @param $parameters + * @param $validator + * @author Mojtaba Anisi + * @since Oct 1, 2016 + * @return boolean + */ + function CardNumber($attribute, $value, $parameters, $validator) + { + ValidationMessages::setCustomMessages( $validator ); + + if (!preg_match('/^\d{16}$/', $value)) { + return false; + } + + $sum = 0; + + for ($position = 1; $position <= 16; $position++){ + $temp = $value[$position - 1]; + $temp = $position % 2 === 0 ? $temp : $temp * 2; + $temp = $temp > 9 ? $temp - 9 : $temp; + + $sum += $temp; + } + + return (bool)($sum % 10 === 0); + } + + /** + * validate alphabet, number and some special characters + * @param $attribute + * @param $value + * @param $parameters + * @param $validator + * @author Shahrokh Niakan + * @since Oct 7, 2016 + * @return boolean + */ + public function Address($attribute, $value, $parameters, $validator) + { + ValidationMessages::setCustomMessages( $validator ); + + $this->status = (bool) preg_match("/^[\pL\s\d\-\/\,\،\.\\\\\x{200c}\x{064b}\x{064d}\x{064c}\x{064e}\x{064f}\x{0650}\x{0651}\x{6F0}-\x{6F9}]+$/u", $value); + + return $this->status; + } + + /** + * validate Iran postal code format + * @param $attribute + * @param $value + * @param $parameters + * @param $validator + * @author Shahrokh Niakan + * @since Apr 5, 2017 + * @return boolean + */ + public function IranPostalCode($attribute, $value, $parameters, $validator) + { + ValidationMessages::setCustomMessages( $validator ); + + $this->status = (bool) preg_match("/^(\d{5}-?\d{5})$/", $value); + + return $this->status; + } + + /** + * validate package name of apk + * @param $attribute + * @param $value + * @author Shahrokh Niakan + * @since May 31, 2017 + * @return boolean + */ + public function PackageName($attribute, $value, $parameters, $validator) + { + ValidationMessages::setCustomMessages( $validator ); + + $this->status = (bool) preg_match("/^([a-zA-Z]{1}[a-zA-Z\d_]*\.)+[a-zA-Z][a-zA-Z\d_]*$/", $value); + + return $this->status; + } + + /** + * validate float number + * @param $attribute + * @param $value + * @author Shahrokh Niakan + * @since Jul 07, 2017 + * @return boolean + */ + public function FloatNum($attribute, $value, $parameters, $validator) + { + ValidationMessages::setCustomMessages( $validator ); + + $this->status = (bool) preg_match("/^([0-9]*[\.]?[0-9]*)$/", $value); + + return $this->status; + + } + +} diff --git a/tests/PersianValidationTest.php b/tests/PersianValidationTest.php index ef1b8a0..78a21ea 100644 --- a/tests/PersianValidationTest.php +++ b/tests/PersianValidationTest.php @@ -1,6 +1,6 @@ + * @since May 28, 2016 + * @return void + */ public function __construct() { - - $this->PersianValidation = new PersianValidation(); + $this->PersianValidation = new ValidationRules(); } /** @@ -34,19 +56,23 @@ public function testAlpha() $this->value = "Shahrokh"; - $this->assertEquals(false, $this->PersianValidation->Alpha($this->attribute, $this->value)); + $this->assertEquals(false, $this->PersianValidation->Alpha($this->attribute, $this->value, $this->parameters, $this->validator)); $this->value = "شاهرخ"; - $this->assertEquals(true, $this->PersianValidation->Alpha($this->attribute, $this->value)); + $this->assertEquals(true, $this->PersianValidation->Alpha($this->attribute, $this->value, $this->parameters, $this->validator)); $this->value = "1111 شاهرخ"; - $this->assertEquals(false, $this->PersianValidation->Alpha($this->attribute, $this->value)); + $this->assertEquals(false, $this->PersianValidation->Alpha($this->attribute, $this->value, $this->parameters, $this->validator)); $this->value = "شاهرخ نیاکان"; - $this->assertEquals(true, $this->PersianValidation->Alpha($this->attribute, $this->value)); + $this->assertEquals(true, $this->PersianValidation->Alpha($this->attribute, $this->value, $this->parameters, $this->validator)); + + $this->value = "وَحِیُدّ‌الٍمٌاًسی"; + + $this->assertEquals(true, $this->PersianValidation->Alpha($this->attribute, $this->value, $this->parameters, $this->validator)); } @@ -61,15 +87,15 @@ public function testNum() $this->value = "1234"; - $this->assertEquals(false, $this->PersianValidation->Num($this->attribute, $this->value)); + $this->assertEquals(false, $this->PersianValidation->Num($this->attribute, $this->value, $this->parameters, $this->validator)); $this->value = "۱۲۳۴"; - $this->assertEquals(true, $this->PersianValidation->Num($this->attribute, $this->value)); + $this->assertEquals(true, $this->PersianValidation->Num($this->attribute, $this->value, $this->parameters, $this->validator)); $this->value = "۱۲۳123"; - $this->assertEquals(false, $this->PersianValidation->Num($this->attribute, $this->value)); + $this->assertEquals(false, $this->PersianValidation->Num($this->attribute, $this->value, $this->parameters, $this->validator)); } @@ -84,32 +110,35 @@ public function testAlpha_Num() $this->value = "Shahrokh1234"; - $this->assertEquals(false, $this->PersianValidation->AlphaNum($this->attribute, $this->value)); + $this->assertEquals(false, $this->PersianValidation->AlphaNum($this->attribute, $this->value, $this->parameters, $this->validator)); $this->value = "1111شاهرخ"; - $this->assertEquals(false, $this->PersianValidation->AlphaNum($this->attribute, $this->value)); + $this->assertEquals(false, $this->PersianValidation->AlphaNum($this->attribute, $this->value, $this->parameters, $this->validator)); $this->value = "1111شاهرخ۱۲۳۴"; - $this->assertEquals(false, $this->PersianValidation->AlphaNum($this->attribute, $this->value)); + $this->assertEquals(false, $this->PersianValidation->AlphaNum($this->attribute, $this->value, $this->parameters, $this->validator)); $this->value = "شاهرخ"; - $this->assertEquals(true, $this->PersianValidation->AlphaNum($this->attribute, $this->value)); + $this->assertEquals(true, $this->PersianValidation->AlphaNum($this->attribute, $this->value, $this->parameters, $this->validator)); $this->value = "۱۲۳۴"; - $this->assertEquals(true, $this->PersianValidation->AlphaNum($this->attribute, $this->value)); + $this->assertEquals(true, $this->PersianValidation->AlphaNum($this->attribute, $this->value, $this->parameters, $this->validator)); $this->value = "Shahrokh۱۲۳۴شاهرخ"; - $this->assertEquals(false, $this->PersianValidation->AlphaNum($this->attribute, $this->value)); + $this->assertEquals(false, $this->PersianValidation->AlphaNum($this->attribute, $this->value, $this->parameters, $this->validator)); $this->value = "۱۲۳۴ شاهرخ"; - $this->assertEquals(true, $this->PersianValidation->AlphaNum($this->attribute, $this->value)); + $this->assertEquals(true, $this->PersianValidation->AlphaNum($this->attribute, $this->value, $this->parameters, $this->validator)); + + $this->value = "وَحِیُدّ‌الٍمٌاًسی"; + $this->assertEquals(true, $this->PersianValidation->AlphaNum($this->attribute, $this->value, $this->parameters, $this->validator)); } @@ -122,29 +151,29 @@ public function testAlpha_Num() public function testIranMobile() { - $this->value = "+989380105725"; + $this->value = "+989355214655"; - $this->assertEquals(true, $this->PersianValidation->IranMobile($this->attribute, $this->value)); + $this->assertEquals(true, $this->PersianValidation->IranMobile($this->attribute, $this->value, $this->parameters, $this->validator)); - $this->value = "09380105725"; + $this->value = "989355214655"; - $this->assertEquals(true, $this->PersianValidation->IranMobile($this->attribute, $this->value)); + $this->assertEquals(true, $this->PersianValidation->IranMobile($this->attribute, $this->value, $this->parameters, $this->validator)); - $this->value = "989123583439"; + $this->value = "00989355214655"; - $this->assertEquals(true, $this->PersianValidation->IranMobile($this->attribute, $this->value)); + $this->assertEquals(true, $this->PersianValidation->IranMobile($this->attribute, $this->value, $this->parameters, $this->validator)); - $this->value = "9380105725"; + $this->value = "09355214655"; - $this->assertEquals(false, $this->PersianValidation->IranMobile($this->attribute, $this->value)); + $this->assertEquals(true, $this->PersianValidation->IranMobile($this->attribute, $this->value, $this->parameters, $this->validator)); - $this->value = "09023583439"; + $this->value = "09901464762"; - $this->assertEquals(true, $this->PersianValidation->IranMobile($this->attribute, $this->value)); + $this->assertEquals(true, $this->PersianValidation->IranMobile($this->attribute, $this->value, $this->parameters, $this->validator)); - $this->value = "09313583439"; + $this->value = "9901464762"; - $this->assertEquals(true, $this->PersianValidation->IranMobile($this->attribute, $this->value)); + $this->assertEquals(true, $this->PersianValidation->IranMobile($this->attribute, $this->value, $this->parameters, $this->validator)); } @@ -159,15 +188,15 @@ public function testSheba() $this->value = "IR062960000000100324200001"; - $this->assertEquals(true, $this->PersianValidation->Sheba($this->attribute, $this->value)); + $this->assertEquals(true, $this->PersianValidation->Sheba($this->attribute, $this->value, $this->parameters, $this->validator)); $this->value = "IR06296000000010032420000"; - $this->assertEquals(false, $this->PersianValidation->Sheba($this->attribute, $this->value)); + $this->assertEquals(false, $this->PersianValidation->Sheba($this->attribute, $this->value, $this->parameters, $this->validator)); $this->value = "00062960000000100324200001"; - $this->assertEquals(false, $this->PersianValidation->Sheba($this->attribute, $this->value)); + $this->assertEquals(false, $this->PersianValidation->Sheba($this->attribute, $this->value, $this->parameters, $this->validator)); } @@ -181,35 +210,75 @@ public function testMelliCode() { $this->value = "0013542419"; - $this->assertEquals(true, $this->PersianValidation->MelliCode($this->attribute, $this->value)); + $this->assertEquals(true, $this->PersianValidation->MelliCode($this->attribute, $this->value, $this->parameters, $this->validator)); $this->value = "3240175800"; - $this->assertEquals(true, $this->PersianValidation->MelliCode($this->attribute, $this->value)); + $this->assertEquals(true, $this->PersianValidation->MelliCode($this->attribute, $this->value, $this->parameters, $this->validator)); $this->value = "3240164175"; - $this->assertEquals(true, $this->PersianValidation->MelliCode($this->attribute, $this->value)); + $this->assertEquals(true, $this->PersianValidation->MelliCode($this->attribute, $this->value, $this->parameters, $this->validator)); $this->value = "3370075024"; - $this->assertEquals(true, $this->PersianValidation->MelliCode($this->attribute, $this->value)); + $this->assertEquals(true, $this->PersianValidation->MelliCode($this->attribute, $this->value, $this->parameters, $this->validator)); $this->value = "0010532129"; - $this->assertEquals(true, $this->PersianValidation->MelliCode($this->attribute, $this->value)); + $this->assertEquals(true, $this->PersianValidation->MelliCode($this->attribute, $this->value, $this->parameters, $this->validator)); $this->value = "0860170470"; - $this->assertEquals(true, $this->PersianValidation->MelliCode($this->attribute, $this->value)); + $this->assertEquals(true, $this->PersianValidation->MelliCode($this->attribute, $this->value, $this->parameters, $this->validator)); $this->value = "324011122"; - $this->assertEquals(false, $this->PersianValidation->MelliCode($this->attribute, $this->value)); + $this->assertEquals(false, $this->PersianValidation->MelliCode($this->attribute, $this->value, $this->parameters, $this->validator)); $this->value = "3213213"; - $this->assertEquals(false, $this->PersianValidation->MelliCode($this->attribute, $this->value)); + $this->assertEquals(false, $this->PersianValidation->MelliCode($this->attribute, $this->value, $this->parameters, $this->validator)); + + $this->value = "0000000000"; + + $this->assertEquals(false, $this->PersianValidation->MelliCode($this->attribute, $this->value, $this->parameters, $this->validator)); + + $this->value = "1111111111"; + + $this->assertEquals(false, $this->PersianValidation->MelliCode($this->attribute, $this->value, $this->parameters, $this->validator)); + + $this->value = "2222222222"; + + $this->assertEquals(false, $this->PersianValidation->MelliCode($this->attribute, $this->value, $this->parameters, $this->validator)); + + $this->value = "3333333333"; + + $this->assertEquals(false, $this->PersianValidation->MelliCode($this->attribute, $this->value, $this->parameters, $this->validator)); + + $this->value = "4444444444"; + + $this->assertEquals(false, $this->PersianValidation->MelliCode($this->attribute, $this->value, $this->parameters, $this->validator)); + + $this->value = "5555555555"; + + $this->assertEquals(false, $this->PersianValidation->MelliCode($this->attribute, $this->value, $this->parameters, $this->validator)); + + $this->value = "6666666666"; + + $this->assertEquals(false, $this->PersianValidation->MelliCode($this->attribute, $this->value, $this->parameters, $this->validator)); + + $this->value = "7777777777"; + + $this->assertEquals(false, $this->PersianValidation->MelliCode($this->attribute, $this->value, $this->parameters, $this->validator)); + + $this->value = "8888888888"; + + $this->assertEquals(false, $this->PersianValidation->MelliCode($this->attribute, $this->value, $this->parameters, $this->validator)); + + $this->value = "9999999999"; + + $this->assertEquals(false, $this->PersianValidation->MelliCode($this->attribute, $this->value, $this->parameters, $this->validator)); } @@ -224,27 +293,27 @@ public function testIsNotPersian() $this->value = "شاهرخ۱۲۳۴"; - $this->assertEquals(false, $this->PersianValidation->IsNotPersian($this->attribute, $this->value)); + $this->assertEquals(false, $this->PersianValidation->IsNotPersian($this->attribute, $this->value, $this->parameters, $this->validator)); $this->value = "shahrokh"; - $this->assertEquals(true, $this->PersianValidation->IsNotPersian($this->attribute, $this->value)); + $this->assertEquals(true, $this->PersianValidation->IsNotPersian($this->attribute, $this->value, $this->parameters, $this->validator)); $this->value = "Shahrokhشاهرخ۱۲۳۴"; - $this->assertEquals(false, $this->PersianValidation->IsNotPersian($this->attribute, $this->value)); + $this->assertEquals(false, $this->PersianValidation->IsNotPersian($this->attribute, $this->value, $this->parameters, $this->validator)); $this->value = "shahrokhw3289834(!!!%$$(@_)_)_"; - $this->assertEquals(true, $this->PersianValidation->IsNotPersian($this->attribute, $this->value)); + $this->assertEquals(true, $this->PersianValidation->IsNotPersian($this->attribute, $this->value, $this->parameters, $this->validator)); $this->value = 1213131313131; - $this->assertEquals(false, $this->PersianValidation->IsNotPersian($this->attribute, $this->value)); + $this->assertEquals(false, $this->PersianValidation->IsNotPersian($this->attribute, $this->value, $this->parameters, $this->validator)); $this->value = ["Shahrokh"]; - $this->assertEquals(false, $this->PersianValidation->IsNotPersian($this->attribute, $this->value)); + $this->assertEquals(false, $this->PersianValidation->IsNotPersian($this->attribute, $this->value, $this->parameters, $this->validator)); } @@ -258,27 +327,27 @@ public function testLimitedArray() { $this->value = []; - $this->assertEquals(true, $this->PersianValidation->LimitedArray($this->attribute, $this->value, $this->parameters)); + $this->assertEquals(true, $this->PersianValidation->LimitedArray($this->attribute, $this->value, $this->parameters, $this->validator)); $this->value = []; $this->parameters[0] = 1; - $this->assertEquals(true, $this->PersianValidation->LimitedArray($this->attribute, $this->value, $this->parameters)); + $this->assertEquals(true, $this->PersianValidation->LimitedArray($this->attribute, $this->value, $this->parameters, $this->validator)); $this->value = ["a"]; $this->parameters[0] = 2; - $this->assertEquals(true, $this->PersianValidation->LimitedArray($this->attribute, $this->value, $this->parameters)); + $this->assertEquals(true, $this->PersianValidation->LimitedArray($this->attribute, $this->value, $this->parameters, $this->validator)); $this->value = ["a", "b"]; $this->parameters[0] = 2; - $this->assertEquals(true, $this->PersianValidation->LimitedArray($this->attribute, $this->value, $this->parameters)); + $this->assertEquals(true, $this->PersianValidation->LimitedArray($this->attribute, $this->value, $this->parameters, $this->validator)); $this->value = ["a", "b", "c"]; $this->parameters[0] = 2; - $this->assertEquals(false, $this->PersianValidation->LimitedArray($this->attribute, $this->value, $this->parameters)); + $this->assertEquals(false, $this->PersianValidation->LimitedArray($this->attribute, $this->value, $this->parameters, $this->validator)); } @@ -293,15 +362,15 @@ public function testUnSignedNum() $this->value = 11; - $this->assertEquals(true, $this->PersianValidation->UnSignedNum($this->attribute, $this->value)); + $this->assertEquals(true, $this->PersianValidation->UnSignedNum($this->attribute, $this->value, $this->parameters, $this->validator)); $this->value = -11; - $this->assertEquals(false, $this->PersianValidation->UnSignedNum($this->attribute, $this->value)); + $this->assertEquals(false, $this->PersianValidation->UnSignedNum($this->attribute, $this->value, $this->parameters, $this->validator)); $this->value = 11.22; - $this->assertEquals(false, $this->PersianValidation->UnSignedNum($this->attribute, $this->value)); + $this->assertEquals(false, $this->PersianValidation->UnSignedNum($this->attribute, $this->value, $this->parameters, $this->validator)); } @@ -316,11 +385,15 @@ public function testAlphaSpace() $this->value = "shahrokh niakan"; - $this->assertEquals(true, $this->PersianValidation->AlphaSpace($this->attribute, $this->value)); + $this->assertEquals(true, $this->PersianValidation->AlphaSpace($this->attribute, $this->value, $this->parameters, $this->validator, $this->parameters, $this->validator)); $this->value = "shahrokh 121"; - $this->assertEquals(false, $this->PersianValidation->AlphaSpace($this->attribute, $this->value)); + $this->assertEquals(false, $this->PersianValidation->AlphaSpace($this->attribute, $this->value, $this->parameters, $this->validator, $this->parameters, $this->validator)); + + $this->value = "وَحِیُدّ‌الٍمٌاًسی"; + + $this->assertEquals(true, $this->PersianValidation->AlphaSpace($this->attribute, $this->value, $this->parameters, $this->validator, $this->parameters, $this->validator)); } @@ -335,11 +408,11 @@ public function testUrl() $this->value = "http://hello.com"; - $this->assertEquals(true, $this->PersianValidation->Url($this->attribute, $this->value)); + $this->assertEquals(true, $this->PersianValidation->Url($this->attribute, $this->value, $this->parameters, $this->validator)); $this->value = "http/df;fdl"; - $this->assertEquals(false, $this->PersianValidation->Url($this->attribute, $this->value)); + $this->assertEquals(false, $this->PersianValidation->Url($this->attribute, $this->value, $this->parameters, $this->validator)); } @@ -352,17 +425,29 @@ public function testUrl() public function testDomain() { - $this->value = "xn--pgba0a.com"; + $this->value = "www.adele.com"; + + $this->assertEquals(true, $this->PersianValidation->Domain($this->attribute, $this->value, $this->parameters, $this->validator)); + + $this->value = "xn--pgba0a.com"; - $this->assertEquals(true, $this->PersianValidation->Domain($this->attribute, $this->value)); + $this->assertEquals(true, $this->PersianValidation->Domain($this->attribute, $this->value, $this->parameters, $this->validator)); $this->value = "iran-go.ir"; - $this->assertEquals(true, $this->PersianValidation->Domain($this->attribute, $this->value)); + $this->assertEquals(true, $this->PersianValidation->Domain($this->attribute, $this->value, $this->parameters, $this->validator)); $this->value = "dshgf---df.w"; - $this->assertEquals(false, $this->PersianValidation->Domain($this->attribute, $this->value)); + $this->assertEquals(false, $this->PersianValidation->Domain($this->attribute, $this->value, $this->parameters, $this->validator)); + + $this->value = "www.ad#le.com"; + + $this->assertEquals(false, $this->PersianValidation->Domain($this->attribute, $this->value, $this->parameters, $this->validator)); + + $this->value = "www.adele.co,m"; + + $this->assertEquals(false, $this->PersianValidation->Domain($this->attribute, $this->value, $this->parameters, $this->validator)); } @@ -379,15 +464,15 @@ public function testMore() $this->parameters[0] = 9; - $this->assertEquals(true, $this->PersianValidation->More($this->attribute, $this->value, $this->parameters)); + $this->assertEquals(true, $this->PersianValidation->More($this->attribute, $this->value, $this->parameters, $this->validator)); $this->parameters[0] = 11; - $this->assertEquals(false, $this->PersianValidation->More($this->attribute, $this->value, $this->parameters)); + $this->assertEquals(false, $this->PersianValidation->More($this->attribute, $this->value, $this->parameters, $this->validator)); $this->parameters[0] = 10; - $this->assertEquals(false, $this->PersianValidation->More($this->attribute, $this->value, $this->parameters)); + $this->assertEquals(false, $this->PersianValidation->More($this->attribute, $this->value, $this->parameters, $this->validator)); } @@ -404,20 +489,20 @@ public function testLess() $this->parameters[0] = 11; - $this->assertEquals(true, $this->PersianValidation->Less($this->attribute, $this->value, $this->parameters)); + $this->assertEquals(true, $this->PersianValidation->Less($this->attribute, $this->value, $this->parameters, $this->validator)); $this->parameters[0] = 9; - $this->assertEquals(false, $this->PersianValidation->Less($this->attribute, $this->value, $this->parameters)); + $this->assertEquals(false, $this->PersianValidation->Less($this->attribute, $this->value, $this->parameters, $this->validator)); $this->parameters[0] = 10; - $this->assertEquals(false, $this->PersianValidation->Less($this->attribute, $this->value, $this->parameters)); + $this->assertEquals(false, $this->PersianValidation->Less($this->attribute, $this->value, $this->parameters, $this->validator)); } /** - * iran phone number + * unit test of iran phone number * @author Shahrokh Niakan * @since Agu 24, 2016 * @return void @@ -425,17 +510,199 @@ public function testLess() public function testIranPhone() { - $this->value = '08337236555'; + $this->value = '07236445'; + + $this->assertEquals(false, $this->PersianValidation->IranPhone($this->attribute, $this->value, $this->parameters, $this->validator)); + + $this->value = '7236445'; + + $this->assertEquals(false, $this->PersianValidation->IranPhone($this->attribute, $this->value, $this->parameters, $this->validator)); + + $this->value = '17236445'; + + $this->assertEquals(false, $this->PersianValidation->IranPhone($this->attribute, $this->value, $this->parameters, $this->validator)); + + $this->value = '37236445'; + + $this->assertEquals(true, $this->PersianValidation->IranPhone($this->attribute, $this->value, $this->parameters, $this->validator)); + + } + + /** + * unit test of iran phone number with the area code + * @author Amir Hosseini + * @since Jan 28, 2019 + * @return void + */ + public function testIranPhoneWithAreaCode() + { + + $this->value = '07236445'; + + $this->assertEquals(false, $this->PersianValidation->IranPhoneWithAreaCode($this->attribute, $this->value, $this->parameters, $this->validator)); - $this->assertEquals(true, $this->PersianValidation->IranPhone($this->attribute, $this->value)); + $this->value = '7236445'; - $this->value = '21372365544'; + $this->assertEquals(false, $this->PersianValidation->IranPhoneWithAreaCode($this->attribute, $this->value, $this->parameters, $this->validator)); - $this->assertEquals(false, $this->PersianValidation->IranPhone($this->attribute, $this->value)); + $this->value = '17236445'; - $this->value = '021372365541'; + $this->assertEquals(false, $this->PersianValidation->IranPhoneWithAreaCode($this->attribute, $this->value, $this->parameters, $this->validator)); - $this->assertEquals(false, $this->PersianValidation->IranPhone($this->attribute, $this->value)); + $this->value = '37236445'; + + $this->assertEquals(false, $this->PersianValidation->IranPhoneWithAreaCode($this->attribute, $this->value, $this->parameters, $this->validator)); + + $this->value = '02137236445'; + + $this->assertEquals(true, $this->PersianValidation->IranPhoneWithAreaCode($this->attribute, $this->value, $this->parameters, $this->validator)); } + + /** + * unit test of payment card number + * @author Mojtaba Anisi + * @since Oct 2, 2016 + * @return void + */ + public function testCardNumber() + { + $this->value = '6274-1290-0547-3742'; + + $this->assertEquals(false, $this->PersianValidation->CardNumber($this->attribute, $this->value, $this->parameters, $this->validator)); + + $this->value = '6274129107473842'; + + $this->assertEquals(false, $this->PersianValidation->CardNumber($this->attribute, $this->value, $this->parameters, $this->validator)); + + $this->value = '6274 1290 0547 3742'; + + $this->assertEquals(false, $this->PersianValidation->CardNumber($this->attribute, $this->value, $this->parameters, $this->validator)); + + $this->value = '627412900742'; + + $this->assertEquals(false, $this->PersianValidation->CardNumber($this->attribute, $this->value, $this->parameters, $this->validator)); + + $this->value = '62741290054737423252'; + + $this->assertEquals(false, $this->PersianValidation->CardNumber($this->attribute, $this->value, $this->parameters, $this->validator)); + + $this->value = '6274129005473742'; + + $this->assertEquals(true, $this->PersianValidation->CardNumber($this->attribute, $this->value, $this->parameters, $this->validator)); + } + + /** + * unit test of alpha and special characters + * @author Shahrokh Niakan + * @since Oct 7, 2016 + * @return void + */ + public function testAdress() + { + + $this->value = "Iran, Tehran - pardis"; + + $this->assertEquals(true, $this->PersianValidation->Address($this->attribute, $this->value, $this->parameters, $this->validator)); + + $this->value = "ایران، تهران - پردیس"; + + $this->assertEquals(true, $this->PersianValidation->Address($this->attribute, $this->value, $this->parameters, $this->validator)); + + $this->value = "Iran / Tehran / pardis / 16"; + + $this->assertEquals(true, $this->PersianValidation->Address($this->attribute, $this->value, $this->parameters, $this->validator)); + + $this->value = "ایران \ تهران \ پردیس \ ۱۶"; + + $this->assertEquals(true, $this->PersianValidation->Address($this->attribute, $this->value, $this->parameters, $this->validator)); + + $this->value = "Iran, Tehran & pardis"; + + $this->assertEquals(false, $this->PersianValidation->Address($this->attribute, $this->value, $this->parameters, $this->validator)); + + } + + /** + * unit test of iran postal code + * @author Shahrokh Niakan + * @since Apr 5, 2017 + * @return void + */ + public function testIranPostalCode() + { + + $this->value = "1619735744"; + + $this->assertEquals(true, $this->PersianValidation->IranPostalCode($this->attribute, $this->value, $this->parameters, $this->validator)); + + $this->value = "16197-35744"; + + $this->assertEquals(true, $this->PersianValidation->IranPostalCode($this->attribute, $this->value, $this->parameters, $this->validator)); + + $this->value = "116197-35744"; + + $this->assertEquals(false, $this->PersianValidation->IranPostalCode($this->attribute, $this->value, $this->parameters, $this->validator)); + + $this->value = "11619735744"; + + $this->assertEquals(false, $this->PersianValidation->IranPostalCode($this->attribute, $this->value, $this->parameters, $this->validator)); + + } + + + /** + * unit test of apk packge name + * @author Shahrokh Niakan + * @since May 31, 2017 + * @return void + */ + public function testPackageName() + { + + $this->value = "com.adele"; + + $this->assertEquals(true, $this->PersianValidation->PackageName($this->attribute, $this->value, $this->parameters, $this->validator)); + + $this->value = "com.adele.adele"; + + $this->assertEquals(true, $this->PersianValidation->PackageName($this->attribute, $this->value, $this->parameters, $this->validator)); + + $this->value = "com."; + + $this->assertEquals(false, $this->PersianValidation->PackageName($this->attribute, $this->value, $this->parameters, $this->validator)); + + $this->value = "com.adele."; + + $this->assertEquals(false, $this->PersianValidation->PackageName($this->attribute, $this->value, $this->parameters, $this->validator)); + + } + + /** + * unit test ot float number + * @author Shahrokh Niakan + * @since Jul 07, 2017 + * @return void + */ + public function testFloatNum() + { + + $this->value = 0; + + $this->assertEquals(true, $this->PersianValidation->FloatNum($this->attribute, $this->value, $this->parameters, $this->validator)); + + $this->value = 1.5; + + $this->assertEquals(true, $this->PersianValidation->FloatNum($this->attribute, $this->value, $this->parameters, $this->validator)); + + $this->value = .5; + + $this->assertEquals(true, $this->PersianValidation->FloatNum($this->attribute, $this->value, $this->parameters, $this->validator)); + + $this->value = -1; + + $this->assertEquals(false, $this->PersianValidation->FloatNum($this->attribute, $this->value, $this->parameters, $this->validator)); + + } + }