respect-validation/library/Rules/Phone.php

40 lines
980 B
PHP
Raw Normal View History

2014-07-12 21:20:43 +02:00
<?php
2015-06-08 16:47:14 +02:00
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the "LICENSE.md"
* file that was distributed with this source code.
*/
declare(strict_types=1);
2014-07-12 21:20:43 +02:00
namespace Respect\Validation\Rules;
class Phone extends AbstractRegexRule
2014-07-12 21:20:43 +02:00
{
protected function getPregFormat()
2014-07-12 21:20:43 +02:00
{
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;
2014-07-12 21:20:43 +02:00
}
}