-
-
Notifications
You must be signed in to change notification settings - Fork 46
[1.x] Validation for landline numbers #108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 12 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
08a7990
Create CountryLandlineValidator interface
vahidkaargar d3847cb
Create CountryLandlineCallback class
vahidkaargar c7f4cfe
Create landline validator for germany
vahidkaargar 5d7fd23
Create landline validator for Iran
vahidkaargar 1178446
Create ValidLandlineNumber.php
vahidkaargar 814e537
Add translation for landline validation
vahidkaargar c7f7dfe
Add landline validators to config
vahidkaargar ed58f14
Add landline validators on service provider
vahidkaargar db17be5
Create test for landline validator
vahidkaargar 704fc4b
Create documentation for landline validator
vahidkaargar 7615da2
Add landline validator documentation to readme.md
vahidkaargar c3eb085
Merge pull request #1 from vahidkaargar/dev
vahidkaargar 954e02a
Support 18 countries for landline validation
vahidkaargar b4b6f19
Add countries to config for landline
vahidkaargar d22d121
Update documents for landline validation
vahidkaargar 64c50ba
Merge pull request #2 from vahidkaargar/dev
vahidkaargar 9356d91
Merge pull request #3 from vahidkaargar/dev
vahidkaargar c5a120d
Update valid-landline-number.md
milwad-dev a97df27
Fix validation for Saudi Arabia landline
vahidkaargar 5dfd2ba
Write test for all validations
vahidkaargar fec3303
Merge pull request #4 from vahidkaargar/dev
vahidkaargar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| ## ValidLandlineNumber | ||
|
|
||
| If you want to validate the landline number, you can use the `ValidLandlineNumber` rule: | ||
|
|
||
| ```php | ||
| use Milwad\LaravelValidate\Rules\ValidLandlineNumber; | ||
|
|
||
| return [ | ||
| 'landline-number' => ['required', new ValidLandlineNumber()], // landline-number => 09120000000 | ||
| ]; | ||
| ``` | ||
|
|
||
| Also `ValidLandlineNumber` have the ability to validate landline number with specific country code: | ||
|
|
||
| ```php | ||
| use Milwad\LaravelValidate\Rules\ValidLandlineNumber; | ||
| use Milwad\LaravelValidate\Utils\Country; | ||
|
|
||
| return [ | ||
| 'landline-number' => ['required', new ValidLandlineNumber(Country::GERMANY)], // landline-number => 09120000000 | ||
| ]; | ||
| ``` | ||
|
|
||
| > **Note** | ||
| > If you want to know which country's codes are supported by the `ValidLandlineNumber` Rule, you can search your country | ||
| > on this [Countries Landline Number](#support-countries-landline-number) list. | ||
|
|
||
| <a name="support-countries-landline-number"></a> | ||
| ## Support Countries Landline Number | ||
|
|
||
| - ✅ IRAN | ||
| - ✅ GERMANY | ||
|
|
||
This file contains hidden or 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
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| <?php | ||
|
|
||
| namespace Milwad\LaravelValidate\Rules; | ||
|
|
||
| use Illuminate\Contracts\Validation\Rule; | ||
| use Milwad\LaravelValidate\Utils\CountryLandlineCallback; | ||
|
|
||
| class ValidLandlineNumber implements Rule | ||
| { | ||
| public function __construct(protected ?string $code = null) {} | ||
|
|
||
| /** | ||
| * Check phone number is valid. | ||
| */ | ||
| public function passes($attribute, $value): bool | ||
| { | ||
| if (is_string($this->code)) { | ||
| $passes = CountryLandlineCallback::callLandlineValidator($this->code, $value); | ||
|
|
||
| return collect($passes)->some(fn ($passe) => $passe); | ||
| } | ||
|
|
||
| return preg_match('/^(?:\+|00)(?:[1-9]\d{0,2})(?:[ .\-]?\(?\d+\)?)+$/', $value); | ||
| } | ||
|
|
||
| /** | ||
| * Get the validation error message. | ||
| */ | ||
| public function message(): string | ||
| { | ||
| return __('validate.landline-number'); | ||
| } | ||
| } |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| <?php | ||
|
|
||
| namespace Milwad\LaravelValidate\Utils; | ||
|
|
||
| use Milwad\LaravelValidate\Utils\CountryLandlineValidator\CountryLandlineValidator; | ||
| use RuntimeException; | ||
|
|
||
| class CountryLandlineCallback | ||
| { | ||
| /** | ||
| * Country Validate classes. | ||
| */ | ||
| protected static array $validators = []; | ||
|
|
||
| /** | ||
| * Add new country validator. | ||
| * | ||
| * @throws \Throwable | ||
| */ | ||
| public static function addValidator(string $code, string $validator): void | ||
| { | ||
| if (! new $validator instanceof CountryLandlineValidator) { | ||
| throw new RuntimeException('The validator is not instance of CountryLandlineValidator'); | ||
| } | ||
|
|
||
| self::$validators[$code] = $validator; | ||
| } | ||
|
|
||
| /** | ||
| * Call country validate class. | ||
| */ | ||
| public static function callLandlineValidator(string $code, $value) | ||
| { | ||
| if (isset(self::$validators[$code])) { | ||
| return (new self::$validators[$code])->validate($value); | ||
| } else { | ||
| throw new \BadMethodCallException("Validator method for '$code' does not exist."); | ||
| } | ||
| } | ||
| } |
8 changes: 8 additions & 0 deletions
8
src/Utils/CountryLandlineValidator/CountryLandlineValidator.php
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| <?php | ||
|
|
||
| namespace Milwad\LaravelValidate\Utils\CountryLandlineValidator; | ||
|
|
||
| interface CountryLandlineValidator | ||
| { | ||
| public function validate($value): bool; | ||
| } |
14 changes: 14 additions & 0 deletions
14
src/Utils/CountryLandlineValidator/DELandlineValidator.php
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| <?php | ||
|
|
||
| namespace Milwad\LaravelValidate\Utils\CountryLandlineValidator; | ||
|
|
||
| class DELandlineValidator implements CountryLandlineValidator | ||
| { | ||
| /** | ||
| * Validate Germany landline numbers. | ||
| */ | ||
| public function validate($value): bool | ||
| { | ||
| return preg_match('/^(?:(?:\+49|0049)[\s\-]?[2-9]\d{1,4}[\s\-]?\d{3,8}|0[2-9]\d{1,4}[\s\-]?\d{3,8})$/', $value); | ||
| } | ||
| } |
14 changes: 14 additions & 0 deletions
14
src/Utils/CountryLandlineValidator/IRLandlineValidator.php
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| <?php | ||
|
|
||
| namespace Milwad\LaravelValidate\Utils\CountryLandlineValidator; | ||
|
|
||
| class IRLandlineValidator implements CountryLandlineValidator | ||
| { | ||
| /** | ||
| * Validate Iran landline numbers. | ||
| */ | ||
| public function validate($value): bool | ||
| { | ||
| return preg_match('/^(?:98|\+98|0098|0)?[1-8]{1}\d{9}$/', $value); | ||
| } | ||
| } |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| <?php | ||
|
|
||
| namespace Milwad\LaravelValidate\Tests\Rules; | ||
|
|
||
| use BadMethodCallException; | ||
| use Milwad\LaravelValidate\Rules\ValidLandlineNumber; | ||
| use Milwad\LaravelValidate\Tests\TestCase; | ||
| use Milwad\LaravelValidate\Utils\Country; | ||
|
|
||
| class ValidLandlineNumberTest extends TestCase | ||
| { | ||
| /** | ||
| * Test landline number is valid. | ||
| */ | ||
| public function test_landline_number_is_valid() | ||
| { | ||
| $rules = [ | ||
| 'landline_number' => [new ValidLandlineNumber], | ||
| 'landline_de' => [new ValidLandlineNumber(Country::GERMANY)], | ||
| ]; | ||
| $data = [ | ||
| 'landline_number' => '+98212223343', | ||
| 'landline_de' => '+49301234567', | ||
| ]; | ||
| $passes = $this->app['validator']->make($data, $rules)->passes(); | ||
|
|
||
| $this->assertTrue($passes); | ||
| } | ||
|
|
||
| /** | ||
| * Test landline number is not valid. | ||
| */ | ||
| public function test_landline_number_is_not_valid() | ||
| { | ||
| $rules = [ | ||
| 'landline_number' => [new ValidLandlineNumber], | ||
| 'landline_ir' => [new ValidLandlineNumber(Country::IRAN)], | ||
| ]; | ||
| $data = [ | ||
| 'landline_number' => '123456789', | ||
| 'landline_ir' => '09120000000', | ||
| ]; | ||
| $passes = $this->app['validator']->make($data, $rules)->passes(); | ||
|
|
||
| $this->assertFalse($passes); | ||
| } | ||
|
|
||
| /** | ||
| * Test all landline number is valid by specific code. | ||
| */ | ||
| public function test_all_landline_number_is_valid_by_specific_code() | ||
| { | ||
| $rules = [ | ||
| 'landline_ir' => [new ValidLandlineNumber(Country::IRAN)], | ||
| 'landline_de' => [new ValidLandlineNumber(Country::GERMANY)], | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please update the tests
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure |
||
| ]; | ||
| $data = [ | ||
| 'landline_ir' => '02132223343', | ||
| 'landline_de' => '+49301234567', | ||
| ]; | ||
| $passes = $this->app['validator']->make($data, $rules)->passes(); | ||
|
|
||
| $this->assertTrue($passes); | ||
| } | ||
|
|
||
| /** | ||
| * Test if landline number validate method is not exists, will be thrown an exception. | ||
| */ | ||
| public function test_if_landline_number_validate_method_is_not_exists() | ||
| { | ||
| $this->expectException(BadMethodCallException::class); | ||
| $this->expectExceptionMessage("Validator method for 'AZ' does not exist."); | ||
|
|
||
| $rules = ['landline' => [new ValidLandlineNumber(Country::AZERBAIJAN)]]; | ||
| $data = ['landline' => '+62812345678']; | ||
|
|
||
| $this->app['validator']->make($data, $rules)->passes(); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.