Merge branch '1.1'

This commit is contained in:
Henrique Moody 2017-10-17 12:38:16 +02:00
commit 20979898c5
No known key found for this signature in database
GPG key ID: 221E9281655813A6
2 changed files with 24 additions and 3 deletions

View file

@ -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;
}
}

View file

@ -93,6 +93,7 @@ class PhoneTest extends \PHPUnit_Framework_TestCase
['5555555'],
['555.5555'],
['555 5555'],
['+1 (555) 555 5555'],
];
}
@ -101,6 +102,8 @@ class PhoneTest extends \PHPUnit_Framework_TestCase
return [
[''],
['123'],
['(11- 97777-7777'],
['-11) 97777-7777'],
['s555-5555'],
['555-555'],
['555555'],
@ -124,6 +127,7 @@ class PhoneTest extends \PHPUnit_Framework_TestCase
['+55()555 5555'],
['03610666-5'],
['text'],
["555\n5555"],
];
}
}