diff --git a/README.md b/README.md index bf60fb8..a8f7522 100644 --- a/README.md +++ b/README.md @@ -137,6 +137,7 @@ return [ | 38 | [ValidUsername](https://github.com/milwad-dev/laravel-validate/blob/1.x/docs/1.x/valid-username.md) | Validate username for ex (milwad) | | 39 | [ValidUuid](https://github.com/milwad-dev/laravel-validate/blob/1.x/docs/1.x/valid-uuid.md) | Validate uuid for ex (123e4567-e89b-12d3-a456-426655440000) | | 40 | [ValidVatId](https://github.com/milwad-dev/laravel-validate/blob/1.x/docs/1.x/valid-vatid.md) | Validate european VAT ID ex (EL123456789123) | +| 41 | [ValidLandlineNumber](https://github.com/milwad-dev/laravel-validate/blob/1.x/docs/1.x/valid-landline-number.md) | Validate landline number | diff --git a/config/laravel-validate.php b/config/laravel-validate.php index 196f2cc..c7989e1 100644 --- a/config/laravel-validate.php +++ b/config/laravel-validate.php @@ -18,6 +18,8 @@ use Milwad\LaravelValidate\Utils\CountryPhoneValidator\SEPhoneValidator; use Milwad\LaravelValidate\Utils\CountryPhoneValidator\TRPhoneValidator; use Milwad\LaravelValidate\Utils\CountryPhoneValidator\ZHPhoneValidator; +use \Milwad\LaravelValidate\Utils\CountryLandlineValidator\IRLandlineValidator; +use \Milwad\LaravelValidate\Utils\CountryLandlineValidator\DELandlineValidator; return [ /* @@ -47,6 +49,17 @@ 'ZH' => ZHPhoneValidator::class, // China ], + /* + * Mapping of country codes to their respective landline number validator classes. + * Each validator enforces country-specific landline number formatting and validation rules. + * + * You can add custom country landline validator. + */ + 'landline-country' => [ + 'DE' => DELandlineValidator::class, // Germany + 'IR' => IRLandlineValidator::class, // Iran + ], + /* * If you want to use rules like 'required|ValidPhone' in your validations, you can change it to true. */ diff --git a/docs/1.x/valid-landline-number.md b/docs/1.x/valid-landline-number.md new file mode 100644 index 0000000..60c6cbd --- /dev/null +++ b/docs/1.x/valid-landline-number.md @@ -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. + + +## Support Countries Landline Number + +- ✅ IRAN +- ✅ GERMANY + diff --git a/src/LaravelValidateServiceProvider.php b/src/LaravelValidateServiceProvider.php index 0c5d87f..569a86f 100644 --- a/src/LaravelValidateServiceProvider.php +++ b/src/LaravelValidateServiceProvider.php @@ -6,6 +6,7 @@ use Illuminate\Support\Facades\Validator; use Illuminate\Support\ServiceProvider; use Illuminate\Support\Str; +use Milwad\LaravelValidate\Utils\CountryLandlineCallback; use Milwad\LaravelValidate\Utils\CountryPhoneCallback; class LaravelValidateServiceProvider extends ServiceProvider @@ -65,6 +66,12 @@ public function boot(): void CountryPhoneCallback::addValidator($code, $country); } + $landlineCountries = config('laravel-validate.landline-country', []); + + foreach ($landlineCountries as $code => $country) { + CountryLandlineCallback::addValidator($code, $country); + } + // Register rules in container if (config('laravel-validate.using_container', false)) { $rules = File::files(__DIR__.'/Rules'); diff --git a/src/Rules/ValidLandlineNumber.php b/src/Rules/ValidLandlineNumber.php new file mode 100644 index 0000000..ca29a0a --- /dev/null +++ b/src/Rules/ValidLandlineNumber.php @@ -0,0 +1,33 @@ +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'); + } +} diff --git a/src/Utils/CountryLandlineCallback.php b/src/Utils/CountryLandlineCallback.php new file mode 100644 index 0000000..a7c7292 --- /dev/null +++ b/src/Utils/CountryLandlineCallback.php @@ -0,0 +1,40 @@ +validate($value); + } else { + throw new \BadMethodCallException("Validator method for '$code' does not exist."); + } + } +} diff --git a/src/Utils/CountryLandlineValidator/CountryLandlineValidator.php b/src/Utils/CountryLandlineValidator/CountryLandlineValidator.php new file mode 100644 index 0000000..d2a3f6c --- /dev/null +++ b/src/Utils/CountryLandlineValidator/CountryLandlineValidator.php @@ -0,0 +1,8 @@ + ':attribute kein gültiger.', 'username' => ':attribute kein gültiger.', 'uuid' => ':attribute kein gültiger.', + 'landline-number' => ':attribute kein gültiger.', ]; diff --git a/src/lang/fa/validate.php b/src/lang/fa/validate.php index 72975fb..c3f1810 100644 --- a/src/lang/fa/validate.php +++ b/src/lang/fa/validate.php @@ -40,4 +40,5 @@ 'url' => 'مقدار :attribute صحیح نمی باشد.', 'username' => 'مقدار :attribute صحیح نمی باشد.', 'uuid' => 'مقدار :attribute صحیح نمی باشد.', + 'landline-number' => 'مقدار :attribute صحیح نمی باشد.', ]; diff --git a/tests/Rules/ValidLandlineNumberTest.php b/tests/Rules/ValidLandlineNumberTest.php new file mode 100644 index 0000000..628dee9 --- /dev/null +++ b/tests/Rules/ValidLandlineNumberTest.php @@ -0,0 +1,79 @@ + [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)], + ]; + $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(); + } +}