Skip to content

Commit 22f1f14

Browse files
committed
Merge branch '1.0' into 1.1
2 parents 631affd + 1457253 commit 22f1f14

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

library/Rules/Phone.php

+20-3
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,27 @@
1111

1212
namespace Respect\Validation\Rules;
1313

14-
class Phone extends AbstractRule
14+
class Phone extends AbstractRegexRule
1515
{
16-
public function validate($input)
16+
protected function getPregFormat()
1717
{
18-
return !empty($input) && preg_match('/^[+]?([\d]{0,3})?[\(\.\-\s]?(([\d]{1,3})[\)\.\-\s]*)?(([\d]{3,5})[\.\-\s]?([\d]{4})|([\d]{2}[\.\-\s]?){4})$/', $input);
18+
return $this->replaceParams(
19+
'/^\+?({part1})? ?(?(?=\()(\({part2}\) ?{part3})|([. -]?({part2}[. -]*)?{part3}))$/',
20+
[
21+
'part1' => '\d{0,3}',
22+
'part2' => '\d{1,3}',
23+
'part3' => '((\d{3,5})[. -]?(\d{4})|(\d{2}[. -]?){4})',
24+
]
25+
);
26+
}
27+
28+
private function replaceParams($format, array $params)
29+
{
30+
$string = $format;
31+
foreach ($params as $name => $value) {
32+
$string = str_replace('{'.$name.'}', $value, $string);
33+
}
34+
35+
return $string;
1936
}
2037
}

tests/unit/Rules/PhoneTest.php

+4
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ public function providerForPhone()
9393
['5555555'],
9494
['555.5555'],
9595
['555 5555'],
96+
['+1 (555) 555 5555'],
9697
];
9798
}
9899

@@ -101,6 +102,8 @@ public function providerForNotPhone()
101102
return [
102103
[''],
103104
['123'],
105+
['(11- 97777-7777'],
106+
['-11) 97777-7777'],
104107
['s555-5555'],
105108
['555-555'],
106109
['555555'],
@@ -124,6 +127,7 @@ public function providerForNotPhone()
124127
['+55()555 5555'],
125128
['03610666-5'],
126129
['text'],
130+
["555\n5555"],
127131
];
128132
}
129133
}

0 commit comments

Comments
 (0)