Skip to content

Commit 8274925

Browse files
committed
Merge branch '1.x' of https://github.com/milwad-dev/laravel-validate into upgrade-dependencies
2 parents 800c502 + 65701df commit 8274925

8 files changed

+222
-4
lines changed

README.md

+76
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ composer require milwad/laravel-validate
4040

4141
# Publish
4242

43+
If you want to publish config file, you can run below command on your terminal:
44+
45+
```shell
46+
php artisan vendor:publish --tag="laravel-validate-config"
47+
```
48+
4349
If you want to publish a lang file for a custom validation message you can run this command in the terminal:
4450

4551
```shell
@@ -54,6 +60,24 @@ php artisan vendor:publish --tag="validate-lang-en"
5460

5561
If you don't know about langs name you can see [Support Languages](#support-languages) section.
5662

63+
# Configurations
64+
65+
If you may use rules with string like `ValidPhone`, you need to change the config option to `true`:
66+
67+
```php
68+
/*
69+
* If you want to use rules like 'required|ValidPhone' in your validations, you can change it to true.
70+
*/
71+
'using_container' => false,
72+
```
73+
74+
If `using_container` is set to true, you might have rules like this:
75+
76+
```php
77+
'phone_number' => 'required|ValidPhone',
78+
```
79+
And `ValidPhone` would be a class that is resolved via the service container to check the validity of the phone number.
80+
5781
# Usage
5882

5983
You can use `Laravel-Validate Rules` very simply. You can use the `new` keyword before the rule name.
@@ -141,6 +165,58 @@ Also, you can make <a href="https://github.com/milwad-dev/laravel-validate/pulls
141165
- [x] Ukrainian (uk)
142166
- [x] Chinese (zh_CN)
143167

168+
# Adding Custom Phone Country Validator
169+
170+
If you need to add a custom phone number validator for a specific country, follow the steps below.
171+
172+
### 1. Create Your Custom Validator Class
173+
174+
First, you need to create a custom validator class that implements the `Milwad\LaravelValidate\Utils\CountryPhoneValidator` contract. This contract ensures that your custom validator adheres to the required structure and functionality.
175+
176+
```php
177+
namespace App\Validators;
178+
179+
use Milwad\LaravelValidate\Utils\CountryPhoneValidator;
180+
181+
class CustomPhoneValidator implements CountryPhoneValidator
182+
{
183+
/**
184+
* Validate the phone number for the custom country.
185+
*/
186+
public function validate(string $phoneNumber): bool
187+
{
188+
// Implement the phone number validation logic for your country
189+
// Example: Check if the phone number matches a specific pattern
190+
return preg_match('/^\+1234\d{10}$/', $phoneNumber);
191+
}
192+
}
193+
```
194+
195+
### 2. Add the Validator to the Configuration File
196+
197+
Once you've created the custom validator class, add it to the configuration file (`config/laravel-validate.php`) under the `'phone-country'` array.
198+
199+
For example, if you're adding a validator for the country `XY`:
200+
201+
```php
202+
'phone-country' => [
203+
// Existing validators...
204+
'XY' => \App\Validators\CustomPhoneValidator::class, // Custom country
205+
],
206+
```
207+
208+
This tells the system to use your custom validator for phone numbers from country `XY`.
209+
210+
### 3. Validation Usage
211+
212+
Once your custom validator is set up, you can use it in your application like any other validator:
213+
214+
```php
215+
return [
216+
'phone_ir' => [new ValidPhoneNumber('XY')],
217+
];
218+
```
219+
144220
# License
145221

146222
* This package is created and modified by <a href="https://github.com/milwad-dev" target="_blank">Milwad Khosravi</a> for Laravel over more than 9 and has been released under the MIT License.

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

+2
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ return [
99
'number' => ['required', new ValidEvenNumber()], // number => 1024
1010
];
1111
```
12+
13+
> Consider installing the `gmp` extension to significantly enhance performance when working with large numbers. It can greatly optimize your calculations and improve efficiency.

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

+2
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ return [
99
'number' => ['required', new ValidOddNumber()], // number => 4321
1010
];
1111
```
12+
13+
> Consider installing the `gmp` extension to significantly enhance performance when working with large numbers. It can greatly optimize your calculations and improve efficiency.

src/Rules/ValidEvenNumber.php

+19-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,25 @@ class ValidEvenNumber implements Rule
1111
*/
1212
public function passes($attribute, $value): bool
1313
{
14-
return preg_match('/^\d*[02468]$/', $value);
14+
if (! is_numeric($value)) {
15+
return false;
16+
}
17+
18+
$number = strval($value);
19+
$number = explode('.', $number);
20+
21+
// Check if there is a decimal part and it's not zero
22+
if (isset($number[1]) && $number[1] != 0) {
23+
return false;
24+
}
25+
26+
$number = $number[0];
27+
28+
if (extension_loaded('gmp')) {
29+
return gmp_cmp(gmp_mod($number, '2'), '0') === 0;
30+
}
31+
32+
return $number % 2 === 0;
1533
}
1634

1735
/**

src/Rules/ValidOddNumber.php

+19-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,25 @@ class ValidOddNumber implements Rule
1111
*/
1212
public function passes($attribute, $value): bool
1313
{
14-
return preg_match('/^\d*[13579]$/', $value);
14+
if (! is_numeric($value)) {
15+
return false;
16+
}
17+
18+
$number = strval($value);
19+
$number = explode('.', $number);
20+
21+
// Check if there is a decimal part and it's not zero
22+
if (isset($number[1]) && $number[1] != 0) {
23+
return false;
24+
}
25+
26+
$number = $number[0];
27+
28+
if (extension_loaded('gmp')) {
29+
return gmp_cmp(gmp_mod($number, '2'), '0') !== 0;
30+
}
31+
32+
return $number % 2 !== 0;
1533
}
1634

1735
/**

tests/Rules/ValidEvenNumberTest.php

+48
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,52 @@ public function test_check_number_is_not_even()
3030

3131
$this->assertFalse($passes);
3232
}
33+
34+
/**
35+
* Test float number is even.
36+
*
37+
* @test
38+
*
39+
* @return void
40+
*/
41+
public function check_float_number_is_even()
42+
{
43+
$rules = ['even_number' => [new ValidEvenNumber]];
44+
$data = ['even_number' => '754.00'];
45+
$passes = $this->app['validator']->make($data, $rules)->passes();
46+
47+
$this->assertTrue($passes);
48+
}
49+
50+
/**
51+
* Test float number is not even.
52+
*
53+
* @test
54+
*
55+
* @return void
56+
*/
57+
public function check_float_number_is_not_even()
58+
{
59+
$rules = ['even_number' => [new ValidEvenNumber]];
60+
$data = ['even_number' => '333.13'];
61+
$passes = $this->app['validator']->make($data, $rules)->passes();
62+
63+
$this->assertFalse($passes);
64+
}
65+
66+
/**
67+
* Test non-numeric values.
68+
*
69+
* @test
70+
*
71+
* @return void
72+
*/
73+
public function check_non_numeric_values()
74+
{
75+
$rules = ['even_number' => [new ValidEvenNumber]];
76+
$data = ['even_number' => 'abc'];
77+
$passes = $this->app['validator']->make($data, $rules)->passes();
78+
79+
$this->assertFalse($passes);
80+
}
3381
}

tests/Rules/ValidOddNumberTest.php

+54
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,58 @@ public function test_number_is_not_odd()
3030

3131
$this->assertFalse($passes);
3232
}
33+
34+
/**
35+
* Test float number is odd.
36+
*
37+
* @test
38+
*
39+
* @return void
40+
*/
41+
public function check_float_number_is_odd()
42+
{
43+
$rules = ['odd_number' => [new ValidOddNumber]];
44+
$data = ['odd_number' => '753.00'];
45+
$passes = $this->app['validator']->make($data, $rules)->passes();
46+
47+
$this->assertTrue($passes);
48+
}
49+
50+
/**
51+
* Test float number is not odd.
52+
*
53+
* @test
54+
*
55+
* @return void
56+
*/
57+
public function check_float_number_is_not_odd()
58+
{
59+
$rules = ['odd_number' => [new ValidOddNumber]];
60+
$data = ['odd_number' => '333.14'];
61+
$passes = $this->app['validator']->make($data, $rules)->passes();
62+
63+
$this->assertFalse($passes);
64+
65+
$rules = ['odd_number' => [new ValidOddNumber]];
66+
$data = ['odd_number' => '322.00'];
67+
$passes = $this->app['validator']->make($data, $rules)->passes();
68+
69+
$this->assertFalse($passes);
70+
}
71+
72+
/**
73+
* Test non-numeric values.
74+
*
75+
* @test
76+
*
77+
* @return void
78+
*/
79+
public function check_non_numeric_values()
80+
{
81+
$rules = ['odd_number' => [new ValidOddNumber]];
82+
$data = ['odd_number' => 'abc'];
83+
$passes = $this->app['validator']->make($data, $rules)->passes();
84+
85+
$this->assertFalse($passes);
86+
}
3387
}

tests/Rules/ValidPhoneNumberTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ public function test_phone_number_is_not_valid()
3131
{
3232
$rules = [
3333
'phone_number' => [new ValidPhoneNumber],
34-
'phone_bj' => [new ValidPhoneNumber(Country::IRAN)],
34+
'phone_ir' => [new ValidPhoneNumber(Country::IRAN)],
3535
];
3636
$data = [
3737
'phone_number' => '123456789',
38-
'phone_bj' => '09120000000',
38+
'phone_ir' => '09120000000',
3939
];
4040
$passes = $this->app['validator']->make($data, $rules)->passes();
4141

0 commit comments

Comments
 (0)