Skip to content

Commit

Permalink
Merge pull request #21 from kbosma123/master
Browse files Browse the repository at this point in the history
Have the option to override the default country code on parsing a phonenumber
  • Loading branch information
frankdekker authored Sep 29, 2023
2 parents d81e107 + 8050cb2 commit 82f1975
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"scripts": {
"check": ["@check:phpstan", "@check:phpmd", "@check:phpcs"],
"check:phpstan": "phpstan analyse",
"check:phpmd": "phpmd src,tests text phpmd.xml.dist --suffixes=php",
"check:phpmd": "phpmd src,tests text phpmd.xml.dist --suffixes php",
"check:phpcs": "phpcs src tests",
"fix": "@fix:phpcbf",
"fix:phpcbf": "phpcbf src tests",
Expand Down
10 changes: 6 additions & 4 deletions src/PhoneNumberParseService.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,18 @@ class PhoneNumberParseService
{
private ?PhoneNumberUtil $phoneNumberUtil = null;

public function __construct(private readonly string $countryCode)
public function __construct(private readonly string $defaultCountryCode)
{
}

public function parse(string $phoneNumber): PhoneNumber
public function parse(string $phoneNumber, ?string $countryCode = null): PhoneNumber
{
$this->phoneNumberUtil ??= PhoneNumberUtil::getInstance();

$countryCode ??= $this->defaultCountryCode;

try {
$parsedNumber = $this->phoneNumberUtil->parse($phoneNumber, $this->countryCode, keepRawInput: true);
$parsedNumber = $this->phoneNumberUtil->parse($phoneNumber, $countryCode, keepRawInput: true);
} catch (NumberParseException $e) {
throw new InvalidArgumentException("Unable to parse phoneNumber: " . $phoneNumber, 0, $e);
}
Expand All @@ -32,7 +34,7 @@ public function parse(string $phoneNumber): PhoneNumber
throw new InvalidArgumentException("Number is invalid: " . $phoneNumber);
}

$metaData = $this->phoneNumberUtil->getMetadataForRegion($this->countryCode);
$metaData = $this->phoneNumberUtil->getMetadataForRegion($countryCode);
$prefix = $metaData?->getInternationalPrefix() ?? $metaData?->getPreferredInternationalPrefix();

if (is_numeric($prefix) === false) {
Expand Down
8 changes: 6 additions & 2 deletions tests/Unit/PhoneNumberParseServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,13 @@ public function testParse(
string $nationalNumber,
PhoneNumberTypeEnum $numberType,
string $countryCodeFromObj,
?string $extension = null
?string $extension = null,
?string $overrideCountryCode = null
): void {
$parseService = new PhoneNumberParseService($countryCode);
$result = $parseService->parse($phoneNumber);

$overrideCountryCode ??= $countryCode;
$result = $parseService->parse($phoneNumber, $overrideCountryCode);

static::assertSame($internationalDailCode, $result->getInternationalDialCode());
static::assertSame($countryDialCode, $result->getCountryDialCode());
Expand All @@ -70,6 +73,7 @@ public function testParse(

public function parseProvider(): Generator
{
yield ['BE', '09 34 44 44 32', '00', '33', '934444432', PhoneNumberTypeEnum::VOIP, 'FR', null, 'FR'];
yield ['XX', '+46522180870', '', '46', '522180870', PhoneNumberTypeEnum::FIXED_LINE, 'SE'];
yield ['SE', '+46522180870', '00', '46', '522180870', PhoneNumberTypeEnum::FIXED_LINE, 'SE'];
yield ['SE', '090-230 64 87', '00', '46', '902306487', PhoneNumberTypeEnum::FIXED_LINE, 'SE'];
Expand Down

0 comments on commit 82f1975

Please sign in to comment.