libphonenumber-for-PHP icon indicating copy to clipboard operation
libphonenumber-for-PHP copied to clipboard

Not outputing PhoneNumberFormat::NATIONAL correctly for some countries

Open michaelmcandrew opened this issue 12 years ago • 1 comments

see also / related: https://github.com/davideme/libphonenumber-for-PHP/issues/7

I'm using php 5.4.6.

I wrote the following test script to exercise the national formatting. It failed for CH and GB but not FR.

It looks like PhoneNumberUtil::chooseFormattingPatternForNumber() can't find a valid formatting pattern in these cases (I did not investigate why).

<?php
use com\google\i18n\phonenumbers\PhoneNumberUtil;
use com\google\i18n\phonenumbers\PhoneNumberFormat;
use com\google\i18n\phonenumbers\NumberParseException;
require_once 'PhoneNumberUtil.php';

$inputs = array(
//  array('4154545454', 'US'), // This breaks things.  Can be fixed with https://github.com/davideme/libphonenumber-for-PHP/pull/15
  array('0454545454', 'FR'),
  array('07454545454', 'GB'),
  array('+41444545454', 'CH')
);

foreach ($inputs as $input){
  testFormatting($input);
}

function testFormatting($input){ 
  $number = $input[0]; 
  $countryCode = $input[1]; 

  $phoneUtil = PhoneNumberUtil::getInstance(); 

  $numberObject = $phoneUtil->parseAndKeepRawInput($number, $countryCode);  
  $nationalNumber = $phoneUtil->format($numberObject, PhoneNumberFormat::NATIONAL); 
  $internationalNumber = $phoneUtil->format($numberObject, PhoneNumberFormat::INTERNATIONAL); 

  var_dump($numberObject); 
  var_dump($number); 
  var_dump($nationalNumber); 
  var_dump($internationalNumber); 
}

Output:

class com\google\i18n\phonenumbers\PhoneNumber#2 (7) {
  private $countryCode =>
  int(33)
  private $nationalNumber =>
  double(454545454)
  private $extension =>
  NULL
  private $italianLeadingZero =>
  NULL
  private $rawInput =>
  string(10) "0454545454"
  private $countryCodeSource =>
  int(3)
  private $preferredDomesticCarrierCode =>
  string(0) ""
}
string(10) "0454545454"
string(14) "04 54 54 54 54"
string(17) "+33 4 54 54 54 54"
class com\google\i18n\phonenumbers\PhoneNumber#2 (7) {
  private $countryCode =>
  int(44)
  private $nationalNumber =>
  double(7454545454)
  private $extension =>
  NULL
  private $italianLeadingZero =>
  NULL
  private $rawInput =>
  string(11) "07454545454"
  private $countryCodeSource =>
  int(3)
  private $preferredDomesticCarrierCode =>
  string(0) ""
}
string(11) "07454545454"
string(10) "7454545454"
string(14) "+44 7454545454"
class com\google\i18n\phonenumbers\PhoneNumber#2 (7) {
  private $countryCode =>
  int(41)
  private $nationalNumber =>
  double(444545454)
  private $extension =>
  NULL
  private $italianLeadingZero =>
  NULL
  private $rawInput =>
  string(12) "+41444545454"
  private $countryCodeSource =>
  int(0)
  private $preferredDomesticCarrierCode =>
  string(0) ""
}
string(12) "+41444545454"
string(9) "444545454"
string(13) "+41 444545454"

In the UK example, I would have expected something like 07454 545454

michaelmcandrew avatar Apr 11 '13 09:04 michaelmcandrew

I had the same problem for Dutch (NL) numbers, and found out why: the whitespace in the PhoneNumberMetaData files' regex patterns cause the regex not to work. Either by removing it in the metadata files or by removing whitespace when you're getting the data you can fix it.

Sadly, I haven't had the time to make such a fix other than a quick hack in my own project.

kokone avatar Apr 11 '13 11:04 kokone