OCA\OcSms\Lib\PhoneNumberFormatter::format PHP Method

format() public static method

public static format ( $country, $pn )
    public static function format($country, $pn)
    {
        // If no country or country not found into mapper, return original number
        if ($country === false || !array_key_exists($country, CountryCodes::$codes)) {
            return trim($pn);
        }
        $ignrxp = array('#[^\\d\\+\\(\\)\\[\\]\\{\\}]#', '#(.+)([\\(\\[\\{]\\d*[\\)\\]\\}])#', '#[^\\d\\+]#');
        $ignrpl = array('', '$1', '');
        $lpnrpl = CountryCodes::$codes[$country] . '$2';
        // replace with +{countryCode} -xx[x[x]]-123456
        $tpn = trim($pn);
        if (preg_match('#^[\\d\\+\\(\\[\\{].*#', $tpn)) {
            // start with digit, +, (, [ or {
            $fpn = preg_replace($ignrxp, $ignrpl, $tpn);
            // replace everything but digits/+ with ''
            $xpn = preg_replace(PhoneNumberFormatter::$localPrePhoneNumber_rxp, $lpnrpl, $fpn);
            // replace local prenumbers
            $ypn = preg_replace(PhoneNumberFormatter::$intlPhoneNumber_rxp, '+$2', $xpn);
            // format to international coding +x[x[x]].....
        } else {
            $ypn = $tpn;
            // some SMS_adresses are strings
        }
        return $ypn;
    }

Usage Example

Example #1
0
 public function getAllPhoneNumbersForFPN($userId, $phoneNumber, $country)
 {
     $query = \OCP\DB::prepare('SELECT sms_address FROM ' . '*PREFIX*ocsms_smsdatas WHERE user_id = ? AND sms_mailbox IN (?,?)');
     $result = $query->execute(array($userId, 0, 1));
     $phoneList = array();
     while ($row = $result->fetchRow()) {
         $pn = $row["sms_address"];
         $fmtPN = PhoneNumberFormatter::format($country, $pn);
         if (!isset($phoneList[$fmtPN])) {
             $phoneList[$fmtPN] = array();
         }
         if (!isset($phoneList[$fmtPN][$pn])) {
             $phoneList[$fmtPN][$pn] = 0;
         }
         $phoneList[$fmtPN][$pn] += 1;
     }
     $fpn = PhoneNumberFormatter::format($country, $phoneNumber);
     if (isset($phoneList[$fpn])) {
         return $phoneList[$fpn];
     } else {
         return array();
     }
 }
All Usage Examples Of OCA\OcSms\Lib\PhoneNumberFormatter::format
PhoneNumberFormatter