Skip to content

Commit 813a2ef

Browse files
authored
update giggsey/libphonenumber-for-php-lite (#38)
1 parent cc24924 commit 813a2ef

File tree

6 files changed

+43
-16
lines changed

6 files changed

+43
-16
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"require": {
1515
"php": ">=8.1",
1616
"ext-intl": "*",
17-
"giggsey/libphonenumber-for-php-lite": "^8.13.11",
17+
"giggsey/libphonenumber-for-php-lite": "^9.0.12",
1818
"moneyphp/money": "^3.3 || ^4.0"
1919
},
2020
"autoload": {

src/PhoneNumber/PhoneNumberFormatOptions.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33

44
namespace DR\Internationalization\PhoneNumber;
55

6-
use libphonenumber\PhoneNumberFormat;
7-
86
/**
97
* @phpstan-type Format self::FORMAT_*
108
*/
@@ -13,22 +11,22 @@ class PhoneNumberFormatOptions
1311
/**
1412
* Formats the NL phoneNumber "101234567" as "+31101234567"
1513
*/
16-
public const FORMAT_E164 = PhoneNumberFormat::E164;
14+
public const FORMAT_E164 = 0;
1715

1816
/**
1917
* Formats the NL phoneNumber "101234567" as "+31 10 123 4567"
2018
*/
21-
public const FORMAT_INTERNATIONAL = PhoneNumberFormat::INTERNATIONAL;
19+
public const FORMAT_INTERNATIONAL = 1;
2220

2321
/**
2422
* Formats the NL phoneNumber "101234567" as "010 123 4567"
2523
*/
26-
public const FORMAT_NATIONAL = PhoneNumberFormat::NATIONAL;
24+
public const FORMAT_NATIONAL = 2;
2725

2826
/**
2927
* Formats the NL phoneNumber "101234567" as "tel:+31-10-123-4567"
3028
*/
31-
public const FORMAT_RFC3966 = PhoneNumberFormat::RFC3966;
29+
public const FORMAT_RFC3966 = 3;
3230

3331
/**
3432
* Formats the NL phoneNumber "101234567" as "0031101234567"

src/PhoneNumber/PhoneNumberTypeEnum.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,20 @@ enum PhoneNumberTypeEnum: string
1616
case PAGER = 'PAGER';
1717
case UAN = 'UAN';
1818
case UNKNOWN = 'UNKNOWN';
19+
20+
/**
21+
* @deprecated Unsupported, will be removed in version 2.0
22+
*/
1923
case EMERGENCY = 'EMERGENCY';
2024
case VOICEMAIL = 'VOICEMAIL';
25+
26+
/**
27+
* @deprecated Unsupported, will be removed in version 2.0
28+
*/
2129
case SHORT_CODE = 'SHORT_CODE';
30+
31+
/**
32+
* @deprecated Unsupported, will be removed in version 2.0
33+
*/
2234
case STANDARD_RATE = 'STANDARD_RATE';
2335
}

src/PhoneNumberFormatService.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use DR\Internationalization\PhoneNumber\PhoneNumberFormatOptions;
88
use InvalidArgumentException;
99
use libphonenumber\NumberParseException;
10+
use libphonenumber\PhoneNumberFormat;
1011
use libphonenumber\PhoneNumberUtil;
1112

1213
class PhoneNumberFormatService
@@ -43,12 +44,12 @@ public function format(string|PhoneNumber $phoneNumber, PhoneNumberFormatOptions
4344
$metaData = $this->phoneNumberUtil->getMetadataForRegion((string)$countryCode);
4445
$prefix = $metaData?->getInternationalPrefix() ?? $metaData?->getPreferredInternationalPrefix();
4546
if (is_numeric($prefix)) {
46-
return $prefix . ltrim($this->phoneNumberUtil->format($parsedNumber, PhoneNumberFormatOptions::FORMAT_E164), '+');
47+
return $prefix . ltrim($this->phoneNumberUtil->format($parsedNumber, PhoneNumberFormat::E164), '+');
4748
}
4849

49-
return $this->phoneNumberUtil->format($parsedNumber, PhoneNumberFormatOptions::FORMAT_E164);
50+
return $this->phoneNumberUtil->format($parsedNumber, PhoneNumberFormat::E164);
5051
}
5152

52-
return $this->phoneNumberUtil->format($parsedNumber, $format);
53+
return $this->phoneNumberUtil->format($parsedNumber, PhoneNumberFormat::from($format));
5354
}
5455
}

src/PhoneNumberParseService.php

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function parse(string $phoneNumber, ?string $countryCode = null): PhoneNu
2525
$countryCode ??= $this->defaultCountryCode;
2626

2727
try {
28-
$parsedNumber = $this->phoneNumberUtil->parse($phoneNumber, $countryCode, keepRawInput: true);
28+
$parsedNumber = $this->phoneNumberUtil->parse($phoneNumber, $countryCode, null, true);
2929
} catch (NumberParseException $e) {
3030
throw new InvalidArgumentException("Unable to parse phoneNumber: " . $phoneNumber, 0, $e);
3131
}
@@ -48,16 +48,27 @@ public function parse(string $phoneNumber, ?string $countryCode = null): PhoneNu
4848
(string)$parsedNumber->getNationalNumber(),
4949
(string)$parsedNumber->getRawInput(),
5050
(string)$this->phoneNumberUtil->getRegionCodeForNumber($parsedNumber),
51-
$this->getNumberType($this->phoneNumberUtil->getNumberType($parsedNumber)),
51+
$this->convertNumberType($this->phoneNumberUtil->getNumberType($parsedNumber)),
5252
$parsedNumber,
5353
$parsedNumber->getExtension()
5454
);
5555
}
5656

57-
private function getNumberType(int $numberType): PhoneNumberTypeEnum
57+
private function convertNumberType(PhoneNumberType $numberType): PhoneNumberTypeEnum
5858
{
59-
$numberType = PhoneNumberType::values()[$numberType];
60-
61-
return PhoneNumberTypeEnum::tryFrom($numberType) ?? PhoneNumberTypeEnum::UNKNOWN;
59+
return match ($numberType) {
60+
PhoneNumberType::FIXED_LINE => PhoneNumberTypeEnum::FIXED_LINE,
61+
PhoneNumberType::MOBILE => PhoneNumberTypeEnum::MOBILE,
62+
PhoneNumberType::FIXED_LINE_OR_MOBILE => PhoneNumberTypeEnum::FIXED_LINE_OR_MOBILE,
63+
PhoneNumberType::TOLL_FREE => PhoneNumberTypeEnum::TOLL_FREE,
64+
PhoneNumberType::PREMIUM_RATE => PhoneNumberTypeEnum::PREMIUM_RATE,
65+
PhoneNumberType::SHARED_COST => PhoneNumberTypeEnum::SHARED_COST,
66+
PhoneNumberType::VOIP => PhoneNumberTypeEnum::VOIP,
67+
PhoneNumberType::PERSONAL_NUMBER => PhoneNumberTypeEnum::PERSONAL_NUMBER,
68+
PhoneNumberType::PAGER => PhoneNumberTypeEnum::PAGER,
69+
PhoneNumberType::UAN => PhoneNumberTypeEnum::UAN,
70+
PhoneNumberType::VOICEMAIL => PhoneNumberTypeEnum::VOICEMAIL,
71+
default => PhoneNumberTypeEnum::UNKNOWN,
72+
};
6273
}
6374
}

tests/Unit/PhoneNumberParseServiceTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,5 +73,10 @@ public static function parseProvider(): Generator
7373
yield ['NL', '0294-787123', '00', '31', '294787123', PhoneNumberTypeEnum::FIXED_LINE, 'NL'];
7474

7575
yield ['FR', '01 50 12 08 32', '00', '33', '150120832', PhoneNumberTypeEnum::FIXED_LINE, 'FR'];
76+
77+
yield ['SE', '+46 -75 123 45 67', '00', '46', '751234567', PhoneNumberTypeEnum::PERSONAL_NUMBER, 'SE'];
78+
yield ['SE', '+46 -74 012 34 56', '00', '46', '740123456', PhoneNumberTypeEnum::PAGER, 'SE'];
79+
yield ['SE', '+46 -10 234 56 78', '00', '46', '102345678', PhoneNumberTypeEnum::UAN, 'SE'];
80+
yield ['SE', '+46 -25 412 34 56 789', '00', '46', '254123456789', PhoneNumberTypeEnum::VOICEMAIL, 'SE'];
7681
}
7782
}

0 commit comments

Comments
 (0)