Skip to content

Commit 67b53ad

Browse files
committed
Integrating the ITU-T E.123 standard
1 parent addae10 commit 67b53ad

File tree

4 files changed

+55
-1
lines changed

4 files changed

+55
-1
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ composer require laravel-validation-rules/phone
3030

3131
```php
3232
use LVR\Phone\Phone;
33+
use LVR\Phone\E123;
3334
use LVR\Phone\E164;
3435
use LVR\Phone\NANP;
3536
use LVR\Phone\Digits;
@@ -39,6 +40,9 @@ $request->validate(['test' => '15556667777'], ['test' => new Phone]); // Pass!
3940
$request->validate(['test' => '+15556667777'], ['test' => new Phone]); // Pass!
4041
$request->validate(['test' => '+1 (555) 666-7777'], ['test' => new Phone]); // Pass!
4142

43+
// Test for E123
44+
$request->validate(['test' => '+22 555 666 7777'], ['test' => new E123]); // Pass!
45+
4246
// Test for E164
4347
$request->validate(['test' => '+15556667777'], ['test' => new E164]); // Pass!
4448

src/E123.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace LVR\Phone;
4+
5+
6+
class E123 extends Phone
7+
{
8+
/**
9+
* Determine if the validation rule passes.
10+
*
11+
* @param string $attribute
12+
* @param mixed $value
13+
*
14+
* @return bool
15+
*/
16+
public function passes($attribute, $value)
17+
{
18+
return $this->isE123($value);
19+
}
20+
21+
/**
22+
* Get the validation error message.
23+
*
24+
* @return string
25+
*/
26+
public function message()
27+
{
28+
return ':attribute must be in E.123 phone format';
29+
}
30+
}

src/Phone.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function message()
3636
*/
3737
protected function isPhone($value)
3838
{
39-
return $this->isE164($value) || $this->isNANP($value) || $this->isDigits($value);
39+
return $this->isE123($value) || $this->isE164($value) || $this->isNANP($value) || $this->isDigits($value);
4040
}
4141

4242
/**
@@ -53,6 +53,16 @@ protected function isDigits($value)
5353
return (bool) array_product($conditions);
5454
}
5555

56+
/**
57+
* Format example +22 555 555 1234, (607) 555 1234, (022607) 555 1234
58+
* @param $value
59+
* @return bool
60+
*/
61+
protected function isE123($value)
62+
{
63+
return preg_match('/^(?:\((\+?\d+)?\)|\+?\d+) ?\d*(-?\d{2,3} ?){0,4}$/', $value) === 1;
64+
}
65+
5666
/**
5767
* Format example +15555555555
5868
* @param string $value The phone number to check

tests/ValidatorTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Exception;
66
use LVR\Phone\Digits;
7+
use LVR\Phone\E123;
78
use LVR\Phone\E164;
89
use LVR\Phone\NANP;
910
use LVR\Phone\Phone;
@@ -21,6 +22,7 @@ public function testValidatorPhone()
2122
$this->assertEquals(true, $this->validate('+15556667777', new Phone));
2223
$this->assertEquals(true, $this->validate('(555) 666-7777', new Phone));
2324
$this->assertEquals(true, $this->validate('5556667777', new Phone));
25+
$this->assertEquals(true, $this->validate('+22 555 666 7777', new Phone));
2426
}
2527

2628
public function testValidatorPhoneDigits()
@@ -31,6 +33,14 @@ public function testValidatorPhoneDigits()
3133
$this->assertEquals(true, $this->validate('15556667777', new Digits));
3234
}
3335

36+
public function testValidatorPhoneE123()
37+
{
38+
$this->assertEquals(true, $this->validate('(607) 555 1234', new E123));
39+
$this->assertEquals(true, $this->validate('(022607) 555 1234', new E123));
40+
$this->assertEquals(true, $this->validate('+22 555 555 1234', new E123));
41+
$this->assertEquals(false, $this->validate('+22 555 555 1234 ext 567', new E123));
42+
}
43+
3444
public function testValidatorPhoneE164()
3545
{
3646
$this->assertEquals(true, $this->validate('+15556660000', new E164));

0 commit comments

Comments
 (0)