mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-13 22:04:23 +00:00
Add phone number parsing functionality
Added a new method 'parse' in the PhonenumberHelper class in ChillMainBundle to sanitize and parse phone numbers. This method specifically handles phone numbers that start with '00', '+' or '0'. Associated unit tests for this new method were also added in PhonenumberHelperTest.php.
This commit is contained in:
parent
76c076a5f3
commit
c81828e04f
@ -76,6 +76,24 @@ final class PhonenumberHelper implements PhoneNumberHelperInterface
|
||||
->formatOutOfCountryCallingNumber($phoneNumber, $this->config['default_carrier_code']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws NumberParseException
|
||||
*/
|
||||
public function parse(string $phoneNumber): PhoneNumber
|
||||
{
|
||||
$sanitizedPhoneNumber = $phoneNumber;
|
||||
|
||||
if (str_starts_with($sanitizedPhoneNumber, '00')) {
|
||||
$sanitizedPhoneNumber = '+'.substr($sanitizedPhoneNumber, 2, null);
|
||||
}
|
||||
|
||||
if (!str_starts_with($sanitizedPhoneNumber, '+') && !str_starts_with($sanitizedPhoneNumber, '0')) {
|
||||
$sanitizedPhoneNumber = '+'.$sanitizedPhoneNumber;
|
||||
}
|
||||
|
||||
return $this->phoneNumberUtil->parse($sanitizedPhoneNumber, $this->config['default_carrier_code']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get type (mobile, landline, ...) for phone number.
|
||||
*/
|
||||
|
@ -12,6 +12,7 @@ declare(strict_types=1);
|
||||
namespace Chill\MainBundle\Tests\Phonenumber;
|
||||
|
||||
use Chill\MainBundle\Phonenumber\PhonenumberHelper;
|
||||
use libphonenumber\PhoneNumber;
|
||||
use libphonenumber\PhoneNumberUtil;
|
||||
use Psr\Log\NullLogger;
|
||||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||||
@ -52,12 +53,36 @@ final class PhonenumberHelperTest extends KernelTestCase
|
||||
];
|
||||
}
|
||||
|
||||
public static function providePhoneNumbersToParse(): iterable
|
||||
{
|
||||
$util = PhoneNumberUtil::getInstance();
|
||||
|
||||
yield [
|
||||
'FR',
|
||||
'+32486544999',
|
||||
$util->parse('+32486544999', 'FR'),
|
||||
];
|
||||
|
||||
yield [
|
||||
'FR',
|
||||
'32486544999',
|
||||
$util->parse('+32486544999', 'FR'),
|
||||
];
|
||||
|
||||
yield [
|
||||
'FR',
|
||||
'0228858040',
|
||||
$util->parse('+33228858040', 'FR'),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider formatPhonenumbers
|
||||
*/
|
||||
public function testFormatPhonenumbers(string $defaultCarrierCode, string $phoneNumber, string $expected)
|
||||
{
|
||||
$util = PhoneNumberUtil::getInstance();
|
||||
|
||||
$subject = new PhonenumberHelper(
|
||||
new ArrayAdapter(),
|
||||
new ParameterBag([
|
||||
@ -70,4 +95,24 @@ final class PhonenumberHelperTest extends KernelTestCase
|
||||
|
||||
$this->assertEquals($expected, $subject->format($util->parse($phoneNumber)));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providePhoneNumbersToParse
|
||||
*/
|
||||
public function testParsePhonenumbers(string $defaultCarrierCode, string $phoneNumber, PhoneNumber $expected): void
|
||||
{
|
||||
$subject = new PhonenumberHelper(
|
||||
new ArrayAdapter(),
|
||||
new ParameterBag([
|
||||
'chill_main.phone_helper' => [
|
||||
'default_carrier_code' => $defaultCarrierCode,
|
||||
],
|
||||
]),
|
||||
new NullLogger()
|
||||
);
|
||||
|
||||
$actual = $subject->parse($phoneNumber);
|
||||
|
||||
self::assertTrue($expected->equals($actual));
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user