From c0ec64e4be5be8f964c379de91eb30fb9d3f2b18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Wed, 2 Mar 2022 21:02:32 +0100 Subject: [PATCH] add leadiing country code while seraching numbers --- .../Utils/ExtractPhonenumberFromPattern.php | 20 +++++++++++++- .../ExtractPhonenumberFromPatternTest.php | 27 +++++++++++++------ 2 files changed, 38 insertions(+), 9 deletions(-) diff --git a/src/Bundle/ChillMainBundle/Search/Utils/ExtractPhonenumberFromPattern.php b/src/Bundle/ChillMainBundle/Search/Utils/ExtractPhonenumberFromPattern.php index b6286fa35..de205d6fd 100644 --- a/src/Bundle/ChillMainBundle/Search/Utils/ExtractPhonenumberFromPattern.php +++ b/src/Bundle/ChillMainBundle/Search/Utils/ExtractPhonenumberFromPattern.php @@ -11,8 +11,10 @@ declare(strict_types=1); namespace Chill\MainBundle\Search\Utils; +use libphonenumber\PhoneNumberUtil; use LogicException; +use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; use function count; use function implode; use function preg_match; @@ -24,6 +26,13 @@ class ExtractPhonenumberFromPattern { private const PATTERN = '([\\+]{0,1}[0-9\\ ]{5,})'; + private string $defaultCarrierCode; + + public function __construct(ParameterBagInterface $parameterBag) + { + $this->defaultCarrierCode = $parameterBag->get('chill_main')['phone_helper']['default_carrier_code']; + } + public function extractPhonenumber(string $subject): SearchExtractionResult { $matches = []; @@ -35,11 +44,20 @@ class ExtractPhonenumberFromPattern foreach (str_split(trim($matches[0])) as $key => $char) { switch ($char) { + case '+': + if (0 === $key) { + $phonenumber[] = $char; + } else { + throw new LogicException('should not match not alnum character'); + } + break; + case '0': $length++; if (0 === $key) { - $phonenumber[] = '+32'; + $util = PhoneNumberUtil::getInstance(); + $phonenumber[] = '+'.$util->getCountryCodeForRegion($this->defaultCarrierCode); } else { $phonenumber[] = $char; } diff --git a/src/Bundle/ChillMainBundle/Tests/Search/Utils/ExtractPhonenumberFromPatternTest.php b/src/Bundle/ChillMainBundle/Tests/Search/Utils/ExtractPhonenumberFromPatternTest.php index f792c0a04..87371a968 100644 --- a/src/Bundle/ChillMainBundle/Tests/Search/Utils/ExtractPhonenumberFromPatternTest.php +++ b/src/Bundle/ChillMainBundle/Tests/Search/Utils/ExtractPhonenumberFromPatternTest.php @@ -13,6 +13,7 @@ namespace Search\Utils; use Chill\MainBundle\Search\Utils\ExtractPhonenumberFromPattern; use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; +use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag; /** * @internal @@ -22,17 +23,25 @@ final class ExtractPhonenumberFromPatternTest extends KernelTestCase { public function provideData() { - yield ['Diallo', 0, [], 'Diallo', 'no phonenumber']; + yield ['BE', 'Diallo', 0, [], 'Diallo', 'no phonenumber']; - yield ['Diallo 15/06/2021', 0, [], 'Diallo 15/06/2021', 'no phonenumber and a date']; + yield ['BE', 'Diallo 15/06/2021', 0, [], 'Diallo 15/06/2021', 'no phonenumber and a date']; - yield ['Diallo 0486 123 456', 1, ['+32486123456'], 'Diallo', 'a phonenumber and a name']; + yield ['BE', 'Diallo 0486 123 456', 1, ['+32486123456'], 'Diallo', 'a phonenumber and a name']; - yield ['Diallo 123 456', 1, ['123456'], 'Diallo', 'a number and a name, without leadiing 0']; + yield ['BE', 'Diallo 123 456', 1, ['123456'], 'Diallo', 'a number and a name, without leadiing 0']; - yield ['123 456', 1, ['123456'], '', 'only phonenumber']; + yield ['BE', '123 456', 1, ['123456'], '', 'only phonenumber']; - yield ['0123 456', 1, ['+32123456'], '', 'only phonenumber with a leading 0']; + yield ['BE', '0123 456', 1, ['+32123456'], '', 'only phonenumber with a leading 0']; + + yield ['FR', '123 456', 1, ['123456'], '', 'only phonenumber']; + + yield ['FR', '0123 456', 1, ['+33123456'], '', 'only phonenumber with a leading 0']; + + yield ['FR', 'Diallo 0486 123 456', 1, ['+33486123456'], 'Diallo', 'a phonenumber and a name']; + + yield ['FR', 'Diallo +32486 123 456', 1, ['+32486123456'], 'Diallo', 'a phonenumber and a name']; } /** @@ -44,9 +53,11 @@ final class ExtractPhonenumberFromPatternTest extends KernelTestCase * @param mixed $filteredSubject * @param mixed $msg */ - public function testExtract($subject, $expectedCount, $expected, $filteredSubject, $msg) + public function testExtract(string $defaultCarrierCode, $subject, $expectedCount, $expected, $filteredSubject, $msg) { - $extractor = new ExtractPhonenumberFromPattern(); + $extractor = new ExtractPhonenumberFromPattern(new ParameterBag(['chill_main' => [ + 'phone_helper' => ['default_carrier_code' => $defaultCarrierCode] + ]])); $result = $extractor->extractPhonenumber($subject); $this->assertCount($expectedCount, $result->getFound());