Skip to content

Commit

Permalink
Merge pull request #1 from saeedsajadi/laravel-7
Browse files Browse the repository at this point in the history
phone and mobile validation
  • Loading branch information
saeedsajadi committed Jan 31, 2021
2 parents 99c2448 + 349b618 commit a6fb9c4
Show file tree
Hide file tree
Showing 9 changed files with 151 additions and 162 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ The field under validation must be a valid date in ISO 8601 format.

The field under validation must be a valid national code.

### ir_mobile

The field under validation must be a valid iranian mobile number.

### ir_phone

The field under validation must be a valid iranian phone number.

### zipcode

The field under validation must be a valid zip code.
Expand All @@ -69,7 +77,9 @@ Or add your custom messages directly to the validator like [described in the doc
* Validator::isIsbn - Checks if given value is valid International Standard Book Number (ISBN).
* Validator::isIsodate - Checks if given value is date in ISO 8601 format.
* Validator::isNationalcode - checks if given value is valid national code.
* Validator::isIrMobile - checks if given value is valid Iranian mobile number.
* Validator::isIrPhone - checks if given value is valid Iranian phone number.
* Validator::isZipcode - checks if given value is valid zip cide.
## License

Intervention Validation Class is licensed under the [MIT License](http://opensource.org/licenses/MIT).
Intervention Validation Class is licensed under the [MIT License](http://opensource.org/licenses/MIT).
10 changes: 7 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
"melli",
"code melli",
"code meli",
"mobile",
"phone",
"zip code",
"credit card",
"کد",
Expand All @@ -27,6 +29,8 @@
"کد ملی",
"شماره ملی",
"شماره کارت",
"موبایل",
"تلفن ثابت",
"کد پستی",
"شبا",
"بانک"
Expand All @@ -40,9 +44,9 @@
}
],
"require": {
"php": ">=5.3.0",
"illuminate/support": "~4|~5",
"illuminate/validation": "~4|~5"
"php": "^7.2.5",
"illuminate/support": "^7.0",
"illuminate/validation": "^7.0"
},
"autoload": {
"psr-4": {
Expand Down
79 changes: 43 additions & 36 deletions src/Validation/ValidationServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,71 +2,78 @@

namespace SmartTwists\IranianInformationValidation;

use Illuminate\Support\Str;
use Illuminate\Support\ServiceProvider;

class ValidationServiceProvider extends ServiceProvider
{
/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = false;

/**
* Actual provider
*
* @var \Illuminate\Support\ServiceProvider
*/
protected $provider;

/**
* Create a new service provider instance.
* Bootstrap the application events.
*
* @param \Illuminate\Contracts\Foundation\Application $app
* @return void
*/
public function __construct($app)
public function boot()
{
parent::__construct($app);
// load translation files
$this->loadTranslationsFrom(
__DIR__ . '/../lang',
'validation'
);

// registering intervention validator extension
$this->app['validator']->resolver(function($translator, $data, $rules, $messages, $customAttributes) {

$this->provider = $this->getProvider();
// set the validation error messages
foreach (get_class_methods('SmartTwists\IranianInformationValidation\Validator') as $method) {
$key = $this->getTranslationKeyFromMethodName($method);
$messages[$key] = $this->getErrorMessage($translator, $messages, $key);
}

return new ValidatorExtension($translator, $data, $rules, $messages, $customAttributes);
});
}

/**
* Bootstrap the application events.
* Register the service provider.
*
* @return void
*/
public function boot()
public function register()
{
$this->provider->boot();
# code...
}

/**
* Register the service provider.
* Return the matching error message for the key
*
* @return void
* @param string $key
* @return string
*/
public function register()
private function getErrorMessage($translator, $messages, $key)
{
$this->provider->register();
// return error messages passed directly to the validator
if (isset($messages[$key])) {
return $messages[$key];
}

// return error message from validation translation file
if ($translator->has("validation.{$key}")) {
return $translator->get("validation.{$key}");
}

// return packages default message
return $translator->get("validation::validation.{$key}");
}

/**
* Return ServiceProvider according to Laravel version
* Return translation key for correspondent method name
*
* @return \ProviderInterface
* @param string $name
* @return string
*/
private function getProvider()
private function getTranslationKeyFromMethodName($name)
{
$app = $this->app;
$version = intval($app::VERSION);
$provider = sprintf(
'SmartTwists\IranianInformationValidation\ValidationServiceProviderLaravel%d', $version
);

return new $provider($app);
return Str::snake(substr($name, 2));
}

/**
Expand Down
43 changes: 0 additions & 43 deletions src/Validation/ValidationServiceProviderLaravel4.php

This file was deleted.

77 changes: 0 additions & 77 deletions src/Validation/ValidationServiceProviderLaravel5.php

This file was deleted.

66 changes: 66 additions & 0 deletions src/Validation/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,72 @@ public static function isNationalcode($value)
return false;
}

/**
* IR mobile validation
*
* @access Public
* @var String
* @return bool
*/
public static function isIrMobile($value)
{
return ((!!preg_match('/(09)[0-9]{9}/', $value)) && (strlen($value) == 11));
}

/**
* IR phone validation
*
* @access Public
* @var String
* @return bool
*/
public static function isIrPhone($value)
{
if(strlen($value) != 11 || preg_match('/(0)[0-9]{10}/', $value) == false){
return false;
}

$iran_provinces_code = [
'021',
'026',
'025',
'086',
'024',
'023',
'081',
'028',
'031',
'044',
'011',
'074',
'083',
'051',
'045',
'017',
'041',
'054',
'087',
'071',
'066',
'034',
'056',
'013',
'077',
'076',
'061',
'038',
'058',
'035',
'084',
];

if(in_array(substr($value,0, 3), $iran_provinces_code)) {
return true;
}

return false;
}

/**
* Get Portion of String Specified
*
Expand Down
20 changes: 20 additions & 0 deletions src/Validation/ValidatorExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,24 @@ public function validateNationalCode($attribute, $value, $parameters)
{
return Validator::isNationalcode($value);
}

/**
* Provides 'ir_mobile' validation rule for Laravel
*
* @return bool
*/
public function validateIRMobile($attribute, $value, $parameters)
{
return Validator::isIrMobile($value);
}

/**
* Provides 'ir_phone' validation rule for Laravel
*
* @return bool
*/
public function validateIRPhone($attribute, $value, $parameters)
{
return Validator::isIrPhone($value);
}
}
Loading

0 comments on commit a6fb9c4

Please sign in to comment.