Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use libphonenumber to properly format phone numbers #216

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,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['country'];
$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' => '',
'(' => '',
')' => '',
'/' => '',
'@' => '',
'-' => ''
],
'country' => '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', 'foo@sip.de', ['type' => 'work']);
$this->contact->add('TEL', '(0511)12345/678-890', ['type' => 'home']);
$this->checkConversionResult('foo@sip.de', 'foo@sip.de');
}

$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('foo@sip.de', (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' => '',
'(' => '',
')' => '',
'@' => '',
'/' => '',
'-' => '',
],
'country' => 'DE'
],
];
}
Expand Down