0

I'm using this PHP port of libphonenumber because the other one requires mbstring which my version of PHP doesn't have installed. https://github.com/davideme/libphonenumber-for-PHP

Now, it doesn't matter what I pass in the $country value below. I always get the exception:
Missing or invalid default region

What on earth am I doing wrong here?

I am porting some javascript code to use the PHP libraries in an ExpressionEngine plugin.

Here's the PHP code:

function getNumberParts($country, $phone) {
    try {
            $cleanphone  = $this->cleanPhone($phone);
            $phoneUtil   = PhoneNumberUtil::getInstance();
            $region      = $phoneUtil->getRegionCodeForCountryCode($country);
            $number      = $phoneUtil->parse($cleanphone, $region);

            $e164        = $phoneUtil->format($number, 0);
            $num         = $phoneUtil->parseAndKeepRawInput($e164, "ZZ");
            $ctry        = $phoneUtil->getRegionCodeForNumber($num);
            $NSN         = $phoneUtil->getNationalSignificantNumber($num);
            $intl        = str_replace("tel:+", "", $phoneUtil->format($num, 3));
            $output      = array("Country"=>"", "CountryCode"=>"", "AreaCode"=>"", "Number"=>"", "Intl"=>"", "FullNumber"=>"");
            $areaCodeLen = $phoneUtil->getLengthOfNationalDestinationCode($num);
            $areaCode    = '';
            if ($areaCodeLen > 0) {
                $subscriberNumber = substr($NSN, $areaCodeLen);
                $areaCode = substr($NSN, 0, $areaCodeLen);
            } else {
                $subscriberNumber = $NSN;
            }
            $output["Country"] = $ctry;
            $output["CountryCode"] = $cc;
            $output["AreaCode"] = $areaCode;
            $output["Number"] = $subscriberNumber;
            $output["Intl"] = $intl;
            $output["FullNumber"] = $output["CountryCode"] . '-' + $output["AreaCode"] . '-' + $output["Number"];
            return $output;
    } catch (NumberParseException $e) {
        $this->formatNumber($phone);
    }    
}


function formatNumber($a, $b) {
    try {
        $b = cleanPhone($b);
        $c = PhoneNumberUtil::getInstance();
        $e = $c->parseAndKeepRawInput($b, $a)
        $f = $c->format($e, PhoneNumberFormat::RFC3966);
        return json_encode($f);
    } catch (NumberParseException $e) {
        return $b
    }
}

This is the WORKING javascript function I'm trying to convert that utilizes the javascript port of libphonenumber:

function getNumberParts(country, phone) {
    try {
        phone = cleanPhone(phone);
        var phoneUtil = i18n.phonenumbers.PhoneNumberUtil.getInstance();
        var cc = phoneUtil.getCountryCodeForRegion(country);
        var number = phoneUtil.parseAndKeepRawInput(phone, country)
        var e164 = phoneUtil.format(number, 0);
        var num = phoneUtil.parseAndKeepRawInput(e164, "ZZ");
        var ctry = phoneUtil.getRegionCodeForNumber(num);
        var NSN = phoneUtil.getNationalSignificantNumber(num);
        intl = phoneUtil.format(num, 3)
        intl = intl.replace("tel:+", "");
        var output = { "Country": "", "CountryCode": "", "AreaCode": "", "Number": "", "Intl":"", "FullNumber": "" };
        var areaCodeLen = phoneUtil.getLengthOfNationalDestinationCode(num);
        var areaCode = '';
        intlNumber = "";
        if (areaCodeLen > 0) {
            subscriberNumber = NSN.substring(areaCodeLen);
            areaCode = NSN.substring(0, areaCodeLen);
        } else {
            subscriberNumber = NSN;
        }
        output.Country = ctry;
        output.CountryCode = number.getCountryCode();
        output.AreaCode = areaCode;
        output.Number = subscriberNumber;
        output.Intl = intl;
        output.FullNumber = number.getCountryCode() + '-' + areaCode + '-' + subscriberNumber;
        return output;
    } catch (l) {
        return formatNumber(country, phone);
    }
}

Even using the example code throws the same exception:

$phoneUtil   = PhoneNumberUtil::getInstance();
try {
    $NumberProto = $phoneUtil->parse($cleanphone, "US");
    var_dump($NumberProto);
} catch (NumberParseException $e) {
    echo $e;
}

What IS very strange is that US AND ZZ are NOT in the supportedRegions array.

I found out what is removing it from the supportedRegions... This call:

unset($this->supportedRegions[array_search(self::REGION_CODE_FOR_NON_GEO_ENTITY, $this->supportedRegions)])

Not sure why this is being done. The REGION_CODE_FOR_NON_GEO_ENTITY is set to '001' so it is removing the US entry from the supportedRegions array.

I finally fixed it by taking the code from the init function in the other PHP port and putting it in this one. I didn't notice but this one also used mbstring function mb_substr but I don't need it and modified it to just use substr.

All is working now.

MB34
  • 4,210
  • 12
  • 59
  • 110

3 Answers3

0

Presumably being thrown because the $phoneUtil->getRegionCodeForCountryCode() method, is not returning a suitable region.

What country code are you passing in?

Is this country code in your country code to region list, or the default one in this file?

Luke Cousins
  • 2,068
  • 1
  • 20
  • 38
0

SO has silly limitation on adding comments for low-rep users, so this is an as-yet incomplete answer....

The exception is NOT being thrown in getRegionCodeForCountryCode(). It is being thrown by the next call: parse($cleanphone, $region)

That's not to say the problem isn't in getRegionCodeForCountryCode() and you can narrow it down by echoing the value of $region between the two calls. Echoing the $cleanphone is also helpful so you know what you have going into that call.

messy
  • 224
  • 2
  • 10
  • It is being thrown in the `parseHelper` function (line 1148); I modified the exception message to echo the `$defaultRegion` parameter and it showed it. – MB34 Jul 01 '15 at 02:48
  • But then again, it could be that the `checkRegionForParsing` is failing or it's call to `isValidRegionCode` could also fail. But I AM passing in a valid region code. Will add some logging to the code to find out. – MB34 Jul 01 '15 at 02:54
0

I finally fixed it by taking the code from the init function in the other PHP port and putting it in this one. I didn't notice but this one also used mbstring function mb_substr but I don't need it and modified it to just use substr.

All is working now.

MB34
  • 4,210
  • 12
  • 59
  • 110