Skip to content

Commit cd5df1a

Browse files
authored
Merge pull request #14 from parth391/master
Fix deprecated rule classes
2 parents 7065a97 + 37c08a9 commit cd5df1a

File tree

7 files changed

+74
-85
lines changed

7 files changed

+74
-85
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
build
22
composer.lock
33
vendor
4-
run.php
4+
run.php
5+
.DS_Store
6+
.phpunit.result.cache

composer.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@
3232
},
3333
"minimum-stability": "stable",
3434
"require": {
35-
"illuminate/contracts": "^5.5|^6.0|^7.0|^8.0|^9.0|^10.0|^11.0|^12.0",
36-
"illuminate/support": "^5.5|^6.0|^7.0|^8.0|^9.0|^10.0|^11.0|^12.0",
37-
"php": "^7.0|^8.0"
35+
"illuminate/contracts": "^10.0|^11.0",
36+
"illuminate/support": "^10.0|^11.0",
37+
"php": "^8.0"
3838
},
3939
"require-dev": {
40-
"phpunit/phpunit": "^6.3|^9.0|^10.5|^11.5.3",
40+
"phpunit/phpunit": "^6.3|^9.0|^10.5|^11.0|^12.0",
4141
"orchestra/testbench": "^3.5|^5.0|^7.0|^9.0|^10.0"
4242
},
43-
"suggest": []
43+
"suggest": {}
4444
}

src/Digits.php

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,27 @@
11
<?php
22
namespace LVR\Phone;
33

4-
use Illuminate\Contracts\Validation\Rule;
4+
use Closure;
55

66
class Digits extends Phone
77
{
88
/**
9-
* Determine if the validation rule passes.
10-
*
11-
* @param string $attribute
12-
* @param mixed $value
13-
*s
14-
* @return bool
9+
* Validation error message
1510
*/
16-
public function passes($attribute, $value)
17-
{
18-
return $this->isDigits($value);
19-
}
11+
protected string $message = ':attribute must be in digits only phone format';
2012

2113
/**
22-
* Get the validation error message.
14+
* Run the validation rule.
2315
*
24-
* @return string
16+
* @param string $attribute
17+
* @param mixed $value
18+
* @param \Closure(string, ?string=): \Illuminate\Translation\PotentiallyTranslatedString $fail
19+
* @return void
2520
*/
26-
public function message()
21+
public function validate(string $attribute, mixed $value, Closure $fail): void
2722
{
28-
return ':attribute must be in digits only phone format';
23+
if (! $this->isDigits($value)) {
24+
$fail($this->message);
25+
}
2926
}
3027
}

src/E123.php

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,27 @@
22

33
namespace LVR\Phone;
44

5+
use Closure;
56

67
class E123 extends Phone
78
{
89
/**
9-
* Determine if the validation rule passes.
10-
*
11-
* @param string $attribute
12-
* @param mixed $value
13-
*
14-
* @return bool
10+
* Validation error message
1511
*/
16-
public function passes($attribute, $value)
17-
{
18-
return $this->isE123($value);
19-
}
12+
protected string $message = ':attribute must be in E.123 phone format';
2013

2114
/**
22-
* Get the validation error message.
15+
* Run the validation rule.
2316
*
24-
* @return string
17+
* @param string $attribute
18+
* @param mixed $value
19+
* @param \Closure(string, ?string=): \Illuminate\Translation\PotentiallyTranslatedString $fail
20+
* @return void
2521
*/
26-
public function message()
22+
public function validate(string $attribute, mixed $value, Closure $fail): void
2723
{
28-
return ':attribute must be in E.123 phone format';
24+
if (! $this->isE123($value)) {
25+
$fail($this->message);
26+
}
2927
}
30-
}
28+
}

src/E164.php

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,27 @@
11
<?php
22
namespace LVR\Phone;
33

4-
use Illuminate\Contracts\Validation\Rule;
4+
use Closure;
55

66
class E164 extends Phone
77
{
88
/**
9-
* Determine if the validation rule passes.
10-
*
11-
* @param string $attribute
12-
* @param mixed $value
13-
*
14-
* @return bool
9+
* Validation error message
1510
*/
16-
public function passes($attribute, $value)
17-
{
18-
return $this->isE164($value);
19-
}
11+
protected string $message = ':attribute must be in E.164 phone format';
2012

2113
/**
22-
* Get the validation error message.
14+
* Run the validation rule.
2315
*
24-
* @return string
16+
* @param string $attribute
17+
* @param mixed $value
18+
* @param \Closure(string, ?string=): \Illuminate\Translation\PotentiallyTranslatedString $fail
19+
* @return void
2520
*/
26-
public function message()
21+
public function validate(string $attribute, mixed $value, Closure $fail): void
2722
{
28-
return ':attribute must be in E.164 phone format';
23+
if (! $this->isE164($value)) {
24+
$fail($this->message);
25+
}
2926
}
30-
}
27+
}

src/NANP.php

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,27 @@
11
<?php
22
namespace LVR\Phone;
33

4-
use Illuminate\Contracts\Validation\Rule;
4+
use Closure;
55

66
class NANP extends Phone
77
{
88
/**
9-
* Determine if the validation rule passes.
10-
*
11-
* @param string $attribute
12-
* @param mixed $value
13-
*s
14-
* @return bool
9+
* Validation error message
1510
*/
16-
public function passes($attribute, $value)
17-
{
18-
return $this->isNANP($value);
19-
}
11+
protected string $message = ':attribute must be in the NANP phone format';
2012

2113
/**
22-
* Get the validation error message.
14+
* Run the validation rule.
2315
*
24-
* @return string
16+
* @param string $attribute
17+
* @param mixed $value
18+
* @param \Closure(string, ?string=): \Illuminate\Translation\PotentiallyTranslatedString $fail
19+
* @return void
2520
*/
26-
public function message()
21+
public function validate(string $attribute, mixed $value, Closure $fail): void
2722
{
28-
return ':attribute must be in the NANP phone format';
23+
if (! $this->isNANP($value)) {
24+
$fail($this->message);
25+
}
2926
}
30-
}
27+
}

src/Phone.php

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,29 @@
11
<?php
22
namespace LVR\Phone;
33

4-
use Illuminate\Contracts\Validation\Rule;
4+
use Closure;
5+
use Illuminate\Contracts\Validation\ValidationRule;
56

6-
class Phone implements Rule
7+
class Phone implements ValidationRule
78
{
89
/**
9-
* Determine if the validation rule passes.
10-
*
11-
* @param string $attribute
12-
* @param mixed $value
13-
*
14-
* @return bool
10+
* Validation error message
1511
*/
16-
public function passes($attribute, $value)
17-
{
18-
return $this->isPhone($value);
19-
}
12+
protected string $message = 'Incorrect phone format for :attribute.';
2013

2114
/**
22-
* Get the validation error message.
15+
* Run the validation rule.
2316
*
24-
* @return string
17+
* @param string $attribute
18+
* @param mixed $value
19+
* @param \Closure(string, ?string=): \Illuminate\Translation\PotentiallyTranslatedString $fail
20+
* @return void
2521
*/
26-
public function message()
22+
public function validate(string $attribute, mixed $value, Closure $fail): void
2723
{
28-
return 'Incorrect phone format for :attribute.';
24+
if (! $this->isPhone($value)) {
25+
$fail($this->message);
26+
}
2927
}
3028

3129
/**
@@ -90,4 +88,4 @@ protected function isNANP($value)
9088
$conditions[] = preg_match("/^(?:\+1|1)?\s?-?\(?\d{3}\)?(\s|-)?\d{3}-\d{4}$/i", $value) > 0;
9189
return (bool) array_product($conditions);
9290
}
93-
}
91+
}

0 commit comments

Comments
 (0)