| 1 |
<?php |
| 2 |
|
| 3 |
namespace libphonenumber; |
| 4 |
|
| 5 |
/** |
| 6 |
* Class RegexBasedMatcher |
| 7 |
* @package libphonenumber |
| 8 |
* @internal |
| 9 |
*/ |
| 10 |
class RegexBasedMatcher implements MatcherAPIInterface |
| 11 |
{ |
| 12 |
public static function create() |
| 13 |
{ |
| 14 |
return new static(); |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Returns whether the given national number (a string containing only decimal digits) matches |
| 19 |
* the national number pattern defined in the given {@code PhoneNumberDesc} message. |
| 20 |
* |
| 21 |
* @param string $number |
| 22 |
* @param PhoneNumberDesc $numberDesc |
| 23 |
* @param boolean $allowPrefixMatch |
| 24 |
* @return boolean |
| 25 |
*/ |
| 26 |
public function matchNationalNumber($number, PhoneNumberDesc $numberDesc, $allowPrefixMatch) |
| 27 |
{ |
| 28 |
$nationalNumberPattern = $numberDesc->getNationalNumberPattern(); |
| 29 |
|
| 30 |
// We don't want to consider it a prefix match when matching non-empty input against an empty |
| 31 |
// pattern |
| 32 |
|
| 33 |
if (strlen($nationalNumberPattern) === 0) { |
| 34 |
return false; |
| 35 |
} |
| 36 |
|
| 37 |
return $this->match($number, $nationalNumberPattern, $allowPrefixMatch); |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* @param string $number |
| 42 |
* @param string $pattern |
| 43 |
* @param $allowPrefixMatch |
| 44 |
* @return bool |
| 45 |
*/ |
| 46 |
private function match($number, $pattern, $allowPrefixMatch) |
| 47 |
{ |
| 48 |
$matcher = new Matcher($pattern, $number); |
| 49 |
|
| 50 |
if (!$matcher->lookingAt()) { |
| 51 |
return false; |
| 52 |
} |
| 53 |
|
| 54 |
return $matcher->matches() ? true : $allowPrefixMatch; |
| 55 |
} |
| 56 |
} |
| 57 |
|