mirror of
https://github.com/nextcloud/ocsms.git
synced 2026-08-24 14:45:43 +00:00
Updated phone formatter (#241)
* Add locale and libphonenumber libraries. * Update country code information Updated existing country codes, fixed incorrect NANP codes for member countries (as well as several UK based ones). Also added in Country Name to ISO country code mapping array for use with libphonenumber. * Convert phone number formatting to use libphonenumber libphonenumber parses phone number more consistantly than the old regex's that mismatched number on a regular basis.
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
namespace libphonenumber\Leniency;
|
||||
|
||||
use libphonenumber\PhoneNumber;
|
||||
use libphonenumber\PhoneNumberUtil;
|
||||
|
||||
abstract class AbstractLeniency
|
||||
{
|
||||
/**
|
||||
* Integer level to compare 'ENUMs'
|
||||
* @var int
|
||||
*/
|
||||
protected static $level;
|
||||
|
||||
/**
|
||||
* Returns true if $number is a verified number according to this leniency
|
||||
*
|
||||
* @param PhoneNumber $number
|
||||
* @param string $candidate
|
||||
* @param PhoneNumberUtil $util
|
||||
* @return bool
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
public static function verify(PhoneNumber $number, $candidate, PhoneNumberUtil $util)
|
||||
{
|
||||
// This can not be called directly
|
||||
throw new \BadMethodCallException;
|
||||
}
|
||||
|
||||
/**
|
||||
* Compare against another Leniency
|
||||
* @param AbstractLeniency $leniency
|
||||
* @return int
|
||||
*/
|
||||
public static function compareTo(AbstractLeniency $leniency)
|
||||
{
|
||||
return static::getLevel() - $leniency::getLevel();
|
||||
}
|
||||
|
||||
protected static function getLevel()
|
||||
{
|
||||
if (static::$level === null) {
|
||||
throw new \RuntimeException('$level should be defined');
|
||||
}
|
||||
|
||||
return static::$level;
|
||||
}
|
||||
|
||||
public function __toString()
|
||||
{
|
||||
return str_replace('libphonenumber\\Leniency\\', '', get_class($this));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace libphonenumber\Leniency;
|
||||
|
||||
use libphonenumber\PhoneNumber;
|
||||
use libphonenumber\PhoneNumberMatcher;
|
||||
use libphonenumber\PhoneNumberUtil;
|
||||
|
||||
class ExactGrouping extends AbstractLeniency
|
||||
{
|
||||
protected static $level = 4;
|
||||
|
||||
/**
|
||||
* Phone numbers accepted are PhoneNumberUtil::isValidNumber() valid and are grouped
|
||||
* in the same way that we would have formatted it, or as a single block. For example,
|
||||
* a US number written as "650 2530000" is not accepted at this leniency level, whereas
|
||||
* "650 253 0000" or "6502530000" are.
|
||||
* Numbers with more than one '/' symbol are also dropped at this level.
|
||||
*
|
||||
* Warning: This level might result in lower coverage especially for regions outside of country
|
||||
* code "+1". If you are not sure about which level to use, email the discussion group
|
||||
* libphonenumber-discuss@googlegroups.com.
|
||||
*
|
||||
* @param PhoneNumber $number
|
||||
* @param string $candidate
|
||||
* @param PhoneNumberUtil $util
|
||||
* @return bool
|
||||
*/
|
||||
public static function verify(PhoneNumber $number, $candidate, PhoneNumberUtil $util)
|
||||
{
|
||||
if (!$util->isValidNumber($number)
|
||||
|| !PhoneNumberMatcher::containsOnlyValidXChars($number, $candidate, $util)
|
||||
|| PhoneNumberMatcher::containsMoreThanOneSlashInNationalNumber($number, $candidate)
|
||||
|| !PhoneNumberMatcher::isNationalPrefixPresentIfRequired($number, $util)
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return PhoneNumberMatcher::checkNumberGroupingIsValid($number, $candidate, $util,
|
||||
function (PhoneNumberUtil $util, PhoneNumber $number, $normalizedCandidate, $expectedNumberGroups) {
|
||||
return PhoneNumberMatcher::allNumberGroupsAreExactlyPresent(
|
||||
$util, $number, $normalizedCandidate, $expectedNumberGroups
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace libphonenumber\Leniency;
|
||||
|
||||
use libphonenumber\PhoneNumber;
|
||||
use libphonenumber\PhoneNumberUtil;
|
||||
|
||||
class Possible extends AbstractLeniency
|
||||
{
|
||||
protected static $level = 1;
|
||||
|
||||
/**
|
||||
* Phone numbers accepted are PhoneNumberUtil::isPossibleNumber(), but not necessarily
|
||||
* PhoneNumberUtil::isValidNumber().
|
||||
*
|
||||
* @param PhoneNumber $number
|
||||
* @param string $candidate
|
||||
* @param PhoneNumberUtil $util
|
||||
* @return bool
|
||||
*/
|
||||
public static function verify(PhoneNumber $number, $candidate, PhoneNumberUtil $util)
|
||||
{
|
||||
return $util->isPossibleNumber($number);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
namespace libphonenumber\Leniency;
|
||||
|
||||
use libphonenumber\PhoneNumber;
|
||||
use libphonenumber\PhoneNumberMatcher;
|
||||
use libphonenumber\PhoneNumberUtil;
|
||||
|
||||
class StrictGrouping extends AbstractLeniency
|
||||
{
|
||||
protected static $level = 3;
|
||||
|
||||
/**
|
||||
* Phone numbers accepted are PhoneNumberUtil::isValidNumber() and are grouped
|
||||
* in a possible way for this locale. For example, a US number written as
|
||||
* "65 02 53 00 00" and "650253 0000" are not accepted at this leniency level, whereas
|
||||
* "650 253 0000", "650 2530000" or "6502530000" are.
|
||||
* Numbers with more than one '/' symbol in the national significant number are also dropped at
|
||||
* this level.
|
||||
*
|
||||
* Warning: This level might result in lower coverage especially for regions outside of country
|
||||
* code "+1". If you are not sure about which level to use, email the discussion group
|
||||
* libphonenumber-discuss@googlegroups.com.
|
||||
*
|
||||
* @param PhoneNumber $number
|
||||
* @param string $candidate
|
||||
* @param PhoneNumberUtil $util
|
||||
* @return bool
|
||||
*/
|
||||
public static function verify(PhoneNumber $number, $candidate, PhoneNumberUtil $util)
|
||||
{
|
||||
if (!$util->isValidNumber($number)
|
||||
|| !PhoneNumberMatcher::containsOnlyValidXChars($number, $candidate, $util)
|
||||
|| PhoneNumberMatcher::containsMoreThanOneSlashInNationalNumber($number, $candidate)
|
||||
|| !PhoneNumberMatcher::isNationalPrefixPresentIfRequired($number, $util)
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return PhoneNumberMatcher::checkNumberGroupingIsValid(
|
||||
$number, $candidate, $util,
|
||||
function (PhoneNumberUtil $util, PhoneNumber $number, $normalizedCandidate, $expectedNumberGroups) {
|
||||
return PhoneNumberMatcher::allNumberGroupsRemainGrouped(
|
||||
$util, $number, $normalizedCandidate, $expectedNumberGroups
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace libphonenumber\Leniency;
|
||||
|
||||
use libphonenumber\PhoneNumber;
|
||||
use libphonenumber\PhoneNumberMatcher;
|
||||
use libphonenumber\PhoneNumberUtil;
|
||||
|
||||
class Valid extends AbstractLeniency
|
||||
{
|
||||
protected static $level = 2;
|
||||
|
||||
/**
|
||||
* Phone numbers accepted are PhoneNumberUtil::isPossibleNumber() and PhoneNumberUtil::isValidNumber().
|
||||
* Numbers written in national format must have their national-prefix present if it is usually written
|
||||
* for a number of this type.
|
||||
*
|
||||
* @param PhoneNumber $number
|
||||
* @param string $candidate
|
||||
* @param PhoneNumberUtil $util
|
||||
* @return bool
|
||||
*/
|
||||
public static function verify(PhoneNumber $number, $candidate, PhoneNumberUtil $util)
|
||||
{
|
||||
if (!$util->isValidNumber($number)
|
||||
|| !PhoneNumberMatcher::containsOnlyValidXChars($number, $candidate, $util)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return PhoneNumberMatcher::isNationalPrefixPresentIfRequired($number, $util);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user