Skip to content

Commit 25bf12e

Browse files
committed
Fix deprecated rule
1 parent a756671 commit 25bf12e

File tree

5 files changed

+66
-79
lines changed

5 files changed

+66
-79
lines changed

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)