fix: Use odolbeau/phone-number-bundle for formatting phone number type fields.

There is no need to use the bundle, we could have used the library instead. However, this idea is to switch to that bundle at some point.
This commit is contained in:
Pol Dellaiera
2022-02-01 16:20:45 +01:00
parent 822f0aa737
commit 4822acb6fb
7 changed files with 112 additions and 53 deletions

View File

@@ -15,8 +15,12 @@ use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\ServerException;
use libphonenumber\PhoneNumber;
use libphonenumber\PhoneNumberFormat;
use libphonenumber\PhoneNumberUtil;
use Psr\Cache\CacheItemPoolInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use function array_key_exists;
use function in_array;
@@ -24,40 +28,30 @@ use function json_decode;
use function preg_replace;
use function strlen;
/**
* Helper to some task linked to phonenumber.
*
* Currently, only Twilio is supported (https://www.twilio.com/lookup). A method
* allow to check if the helper is configured for validation. This should be used
* before doing some validation.
*/
class PhonenumberHelper
final class PhonenumberHelper implements PhoneNumberHelperInterface
{
public const FORMAT_URI = 'https://lookups.twilio.com/v1/PhoneNumbers/%s';
public const LOOKUP_URI = 'https://lookups.twilio.com/v1/PhoneNumbers/%s';
protected CacheItemPoolInterface $cachePool;
private CacheItemPoolInterface $cachePool;
/**
* TRUE if the client is properly configured.
*/
protected bool $isConfigured = false;
private array $config;
protected LoggerInterface $logger;
private bool $isConfigured = false;
/**
* Twilio client.
*/
protected Client $twilioClient;
private LoggerInterface $logger;
private Client $twilioClient;
public function __construct(
CacheItemPoolInterface $cachePool,
$config,
CacheItemPoolInterface $cacheUserData,
ParameterBagInterface $parameterBag,
LoggerInterface $logger
) {
$this->logger = $logger;
$this->cachePool = $cachePool;
$this->cachePool = $cacheUserData;
$this->config = $config = $parameterBag->get('chill_main.phone_helper');
if (
array_key_exists('twilio_sid', $config)
@@ -74,9 +68,17 @@ class PhonenumberHelper
}
}
public function format($phonenumber)
public function denormalize(string $phoneNumber): string
{
return $this->performTwilioFormat($phonenumber);
$phoneUtil = PhoneNumberUtil::getInstance();
$phoneNumber = $phoneUtil->parse($phoneNumber);
return $phoneUtil->format($phoneNumber, PhoneNumberFormat::NATIONAL);
}
public function format(string $phonenumber): string
{
return $this->normalize($phonenumber);
}
/**
@@ -157,7 +159,15 @@ class PhonenumberHelper
return 'mobile' === $validation;
}
protected function performTwilioFormat($phonenumber)
public function normalize(string $phoneNumber): string
{
$phoneUtil = PhoneNumberUtil::getInstance();
$phoneNumber = $phoneUtil->parse($phoneNumber, $this->config['default_carrier_code']);
return $phoneUtil->formatNationalNumberWithPreferredCarrierCode($phoneNumber, $this->config['default_carrier_code']);
}
private function performTwilioFormat($phonenumber)
{
if (false === $this->isPhonenumberValidationConfigured()) {
return $phonenumber;
@@ -218,7 +228,7 @@ class PhonenumberHelper
return $format;
}
protected function performTwilioLookup($phonenumber)
private function performTwilioLookup($phonenumber)
{
if (false === $this->isPhonenumberValidationConfigured()) {
return null;