Skip to content

Commit

Permalink
Merge branch '1.0' into 1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
henriquemoody committed Oct 17, 2017
2 parents 631affd + 1457253 commit 22f1f14
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
23 changes: 20 additions & 3 deletions library/Rules/Phone.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,27 @@

namespace Respect\Validation\Rules;

class Phone extends AbstractRule
class Phone extends AbstractRegexRule
{
public function validate($input)
protected function getPregFormat()
{
return !empty($input) && preg_match('/^[+]?([\d]{0,3})?[\(\.\-\s]?(([\d]{1,3})[\)\.\-\s]*)?(([\d]{3,5})[\.\-\s]?([\d]{4})|([\d]{2}[\.\-\s]?){4})$/', $input);
return $this->replaceParams(
'/^\+?({part1})? ?(?(?=\()(\({part2}\) ?{part3})|([. -]?({part2}[. -]*)?{part3}))$/',
[
'part1' => '\d{0,3}',
'part2' => '\d{1,3}',
'part3' => '((\d{3,5})[. -]?(\d{4})|(\d{2}[. -]?){4})',
]
);
}

private function replaceParams($format, array $params)
{
$string = $format;
foreach ($params as $name => $value) {
$string = str_replace('{'.$name.'}', $value, $string);
}

return $string;
}
}
4 changes: 4 additions & 0 deletions tests/unit/Rules/PhoneTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ public function providerForPhone()
['5555555'],
['555.5555'],
['555 5555'],
['+1 (555) 555 5555'],
];
}

Expand All @@ -101,6 +102,8 @@ public function providerForNotPhone()
return [
[''],
['123'],
['(11- 97777-7777'],
['-11) 97777-7777'],
['s555-5555'],
['555-555'],
['555555'],
Expand All @@ -124,6 +127,7 @@ public function providerForNotPhone()
['+55()555 5555'],
['03610666-5'],
['text'],
["555\n5555"],
];
}
}

0 comments on commit 22f1f14

Please sign in to comment.