Skip to content

Commit

Permalink
Use libphonenumber to properly format phone numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
hanzi committed Aug 11, 2020
1 parent 6f6932a commit 62a8c9d
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 47 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"symfony/console": "^4.3|^5.0",
"guzzlehttp/guzzle": "^7.0",
"ringcentral/psr7": "^1.2",
"sabre/vobject": "^4.0"
"sabre/vobject": "^4.0",
"giggsey/libphonenumber-for-php": "^8.12"
},
"autoload": {
"psr-4": {
Expand Down
18 changes: 8 additions & 10 deletions config.example.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@
],

'conversions' => [
/**
* Two-letter country code where this FRITZ!Box is located.
*
* Phone numbers from this country will be formatted as a national number (i.e. no country code)
* whereas numbers from other countries will use the international format appropriate for this country.
*/
'country' => 'DE',

'vip' => [
'category' => [
'vip1'
Expand Down Expand Up @@ -98,15 +106,5 @@
'WORK' => 'work',
'HOME' => 'home'
],
/**
* 'phoneReplaceCharacters' conversions are processed consecutively. Order decides!
*/
'phoneReplaceCharacters' => [
'+49' => '', // router is usually operated in 'DE; '0049' could also be part of a phone number
'(' => '',
')' => '',
'/' => '',
'-' => ''
]
]
];
17 changes: 11 additions & 6 deletions src/FritzBox/Converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Andig\FritzBox;

use Andig;
use libphonenumber\NumberParseException;
use libphonenumber\PhoneNumberUtil;
use \SimpleXMLElement;

class Converter
Expand Down Expand Up @@ -79,13 +81,16 @@ public function convertPhonenumber($number)
if (filter_var($number, FILTER_VALIDATE_EMAIL) || substr($number, 0, 2) == '**') {
return $number;
}
if (count($this->config['phoneReplaceCharacters'])) {
$number = str_replace("\xc2\xa0", "\x20", $number);
$number = strtr($number, $this->config['phoneReplaceCharacters']);
$number = trim(preg_replace('/\s+/', ' ', $number));
}

return $number;
$numberUtil = PhoneNumberUtil::getInstance();
try {
$countryCode = $this->config['defaultCountry'];
$parsedNumber = $numberUtil->parse($number, $countryCode);
return $numberUtil->formatOutOfCountryCallingNumber($parsedNumber, $countryCode);
} catch (NumberParseException $exception) {
// If parsing the phone number fails, it's probably garbage anyway. Just pass it on verbatim.
return $number;
}
}


Expand Down
56 changes: 34 additions & 22 deletions tests/ConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,7 @@ private function defaultConfig(): array
'CELL' => 'mobile',
'FAX' => 'fax_work'
],
'phoneReplaceCharacters' => [
'+49' => '',
'(' => '',
')' => '',
'/' => '',
'@' => '',
'-' => ''
],
'defaultCountry' => 'DE',
'realName' => [],
],
];
Expand Down Expand Up @@ -211,24 +204,43 @@ public function testPhoneWithoutType()
$this->assertEquals('other', (string)$numberType['type']);
}

public function testPhonenumberConversionType()
public function testSipNumbersDoNotGetChanged()
{
unset($this->contact->TEL);
$this->contact->add('TEL', '[email protected]', ['type' => 'work']);
$this->contact->add('TEL', '(0511)12345/678-890', ['type' => 'home']);
$this->checkConversionResult('[email protected]', '[email protected]');
}

$res = $this->converter->convert($this->contact);
$this->assertCount(1, $res);
public function testNationalNumbersGetNoCountryCode()
{
$this->checkConversionResult('+49(0511)12345/678-890', '0511 12345678890');
}

$contact = $res[0];
$this->assertCount(2, $contact->telephony->children());
public function testInternationalNumbersGetCountryCode()
{
$this->checkConversionResult('+1234567890', '00 1 234567890');
}

public function testGarbageNumbersAreNotChanged()
{
$this->checkConversionResult('garbage', 'garbage');
}

public function testInteralNumbersAreNotChanged()
{
$this->checkConversionResult('**123', '**123');
}

private function checkConversionResult($number, $expectedResult)
{
unset($this->contact->TEL);
$this->contact->add('TEL', $number, ['type' => 'other']);

$result = $this->converter->convert($this->contact);
$this->assertCount(1, $result);

// no number conversion
$number = $contact->telephony->children()[0];
$this->assertEquals('[email protected]', (string)$number);
$resultingContact = $result[0];
$this->assertCount(1, $resultingContact->telephony->children());

// number conversion
$number = $contact->telephony->children()[1];
$this->assertEquals('051112345678890', (string)$number);
$convertedNumber = (string)$resultingContact->telephony->children()[0];
$this->assertEquals($expectedResult, $convertedNumber);
}
}
9 changes: 1 addition & 8 deletions tests/RestorerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,7 @@ private function defaultConfig(): array
'CELL' => 'mobile',
'FAX' => 'fax_work',
],
'phoneReplaceCharacters' => [
'+49' => '',
'(' => '',
')' => '',
'@' => '',
'/' => '',
'-' => '',
],
'defaultCountry' => 'DE'
],
];
}
Expand Down

0 comments on commit 62a8c9d

Please sign in to comment.