Skip to content

Commit c3eb085

Browse files
authored
Merge pull request #1 from vahidkaargar/dev
Dev
2 parents ced68db + 7615da2 commit c3eb085

File tree

12 files changed

+244
-0
lines changed

12 files changed

+244
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ return [
137137
| 38 | [ValidUsername](https://github.com/milwad-dev/laravel-validate/blob/1.x/docs/1.x/valid-username.md) | Validate username for ex (milwad) |
138138
| 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) |
139139
| 40 | [ValidVatId](https://github.com/milwad-dev/laravel-validate/blob/1.x/docs/1.x/valid-vatid.md) | Validate european VAT ID ex (EL123456789123) |
140+
| 41 | [ValidLandlineNumber](https://github.com/milwad-dev/laravel-validate/blob/1.x/docs/1.x/valid-landline-number.md) | Validate landline number |
140141

141142
<a name="support-languages"></a>
142143

config/laravel-validate.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
use Milwad\LaravelValidate\Utils\CountryPhoneValidator\SEPhoneValidator;
1919
use Milwad\LaravelValidate\Utils\CountryPhoneValidator\TRPhoneValidator;
2020
use Milwad\LaravelValidate\Utils\CountryPhoneValidator\ZHPhoneValidator;
21+
use \Milwad\LaravelValidate\Utils\CountryLandlineValidator\IRLandlineValidator;
22+
use \Milwad\LaravelValidate\Utils\CountryLandlineValidator\DELandlineValidator;
2123

2224
return [
2325
/*
@@ -47,6 +49,17 @@
4749
'ZH' => ZHPhoneValidator::class, // China
4850
],
4951

52+
/*
53+
* Mapping of country codes to their respective landline number validator classes.
54+
* Each validator enforces country-specific landline number formatting and validation rules.
55+
*
56+
* You can add custom country landline validator.
57+
*/
58+
'landline-country' => [
59+
'DE' => DELandlineValidator::class, // Germany
60+
'IR' => IRLandlineValidator::class, // Iran
61+
],
62+
5063
/*
5164
* If you want to use rules like 'required|ValidPhone' in your validations, you can change it to true.
5265
*/

docs/1.x/valid-landline-number.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
## ValidLandlineNumber
2+
3+
If you want to validate the landline number, you can use the `ValidLandlineNumber` rule:
4+
5+
```php
6+
use Milwad\LaravelValidate\Rules\ValidLandlineNumber;
7+
8+
return [
9+
'landline-number' => ['required', new ValidLandlineNumber()], // landline-number => 09120000000
10+
];
11+
```
12+
13+
Also `ValidLandlineNumber` have the ability to validate landline number with specific country code:
14+
15+
```php
16+
use Milwad\LaravelValidate\Rules\ValidLandlineNumber;
17+
use Milwad\LaravelValidate\Utils\Country;
18+
19+
return [
20+
'landline-number' => ['required', new ValidLandlineNumber(Country::GERMANY)], // landline-number => 09120000000
21+
];
22+
```
23+
24+
> **Note**
25+
> If you want to know which country's codes are supported by the `ValidLandlineNumber` Rule, you can search your country
26+
> on this [Countries Landline Number](#support-countries-landline-number) list.
27+
28+
<a name="support-countries-landline-number"></a>
29+
## Support Countries Landline Number
30+
31+
- ✅ IRAN
32+
- ✅ GERMANY
33+

src/LaravelValidateServiceProvider.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Support\Facades\Validator;
77
use Illuminate\Support\ServiceProvider;
88
use Illuminate\Support\Str;
9+
use Milwad\LaravelValidate\Utils\CountryLandlineCallback;
910
use Milwad\LaravelValidate\Utils\CountryPhoneCallback;
1011

1112
class LaravelValidateServiceProvider extends ServiceProvider
@@ -65,6 +66,12 @@ public function boot(): void
6566
CountryPhoneCallback::addValidator($code, $country);
6667
}
6768

69+
$landlineCountries = config('laravel-validate.landline-country', []);
70+
71+
foreach ($landlineCountries as $code => $country) {
72+
CountryLandlineCallback::addValidator($code, $country);
73+
}
74+
6875
// Register rules in container
6976
if (config('laravel-validate.using_container', false)) {
7077
$rules = File::files(__DIR__.'/Rules');

src/Rules/ValidLandlineNumber.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Milwad\LaravelValidate\Rules;
4+
5+
use Illuminate\Contracts\Validation\Rule;
6+
use Milwad\LaravelValidate\Utils\CountryLandlineCallback;
7+
8+
class ValidLandlineNumber implements Rule
9+
{
10+
public function __construct(protected ?string $code = null) {}
11+
12+
/**
13+
* Check phone number is valid.
14+
*/
15+
public function passes($attribute, $value): bool
16+
{
17+
if (is_string($this->code)) {
18+
$passes = CountryLandlineCallback::callLandlineValidator($this->code, $value);
19+
20+
return collect($passes)->some(fn ($passe) => $passe);
21+
}
22+
23+
return preg_match('/^(?:\+|00)(?:[1-9]\d{0,2})(?:[ .\-]?\(?\d+\)?)+$/', $value);
24+
}
25+
26+
/**
27+
* Get the validation error message.
28+
*/
29+
public function message(): string
30+
{
31+
return __('validate.landline-number');
32+
}
33+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Milwad\LaravelValidate\Utils;
4+
5+
use Milwad\LaravelValidate\Utils\CountryLandlineValidator\CountryLandlineValidator;
6+
use RuntimeException;
7+
8+
class CountryLandlineCallback
9+
{
10+
/**
11+
* Country Validate classes.
12+
*/
13+
protected static array $validators = [];
14+
15+
/**
16+
* Add new country validator.
17+
*
18+
* @throws \Throwable
19+
*/
20+
public static function addValidator(string $code, string $validator): void
21+
{
22+
if (! new $validator instanceof CountryLandlineValidator) {
23+
throw new RuntimeException('The validator is not instance of CountryLandlineValidator');
24+
}
25+
26+
self::$validators[$code] = $validator;
27+
}
28+
29+
/**
30+
* Call country validate class.
31+
*/
32+
public static function callLandlineValidator(string $code, $value)
33+
{
34+
if (isset(self::$validators[$code])) {
35+
return (new self::$validators[$code])->validate($value);
36+
} else {
37+
throw new \BadMethodCallException("Validator method for '$code' does not exist.");
38+
}
39+
}
40+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Milwad\LaravelValidate\Utils\CountryLandlineValidator;
4+
5+
interface CountryLandlineValidator
6+
{
7+
public function validate($value): bool;
8+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Milwad\LaravelValidate\Utils\CountryLandlineValidator;
4+
5+
class DELandlineValidator implements CountryLandlineValidator
6+
{
7+
/**
8+
* Validate Germany landline numbers.
9+
*/
10+
public function validate($value): bool
11+
{
12+
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);
13+
}
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Milwad\LaravelValidate\Utils\CountryLandlineValidator;
4+
5+
class IRLandlineValidator implements CountryLandlineValidator
6+
{
7+
/**
8+
* Validate Iran landline numbers.
9+
*/
10+
public function validate($value): bool
11+
{
12+
return preg_match('/^(?:98|\+98|0098|0)?[1-8]{1}\d{9}$/', $value);
13+
}
14+
}

src/lang/de/validate.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,5 @@
4040
'url' => ':attribute kein gültiger.',
4141
'username' => ':attribute kein gültiger.',
4242
'uuid' => ':attribute kein gültiger.',
43+
'landline-number' => ':attribute kein gültiger.',
4344
];

0 commit comments

Comments
 (0)