This commit is contained in:
2022-03-02 14:11:47 +01:00
parent f4f488dad1
commit e9ffdb1f03
8 changed files with 192 additions and 175 deletions

View File

@@ -89,16 +89,16 @@ class Configuration implements ConfigurationInterface
->end()
->end() // end of notifications
->arrayNode('phone_helper')
->canBeUnset()
->children()
->scalarNode('twilio_sid')
->defaultNull()
->end()
->scalarNode('twilio_secret')
->defaultNull()
->end()
->scalarNode('default_carrier_code')
->defaultNull()
->canBeUnset()
->children()
->scalarNode('twilio_sid')
->defaultNull()
->end()
->scalarNode('twilio_secret')
->defaultNull()
->end()
->scalarNode('default_carrier_code')
->defaultNull()
->end()
->end()
->end()

View File

@@ -43,10 +43,10 @@ final class PhonenumberHelper implements PhoneNumberHelperInterface
private LoggerInterface $logger;
private Client $twilioClient;
private PhonenumberUtil $phoneNumberUtil;
private Client $twilioClient;
public function __construct(
CacheItemPoolInterface $cacheUserData,
ParameterBagInterface $parameterBag,
@@ -80,7 +80,7 @@ final class PhonenumberHelper implements PhoneNumberHelperInterface
/**
* @param string $phoneNumber A national phone number starting with +
* @return string
*
* @throws NumberParseException
*/
public function format(PhoneNumber $phoneNumber): string

View File

@@ -16,9 +16,6 @@ use Twig\TwigFilter;
class Templating extends AbstractExtension
{
/**
* @var PhonenumberHelper
*/
protected PhonenumberHelper $phonenumberHelper;
public function __construct(PhonenumberHelper $phonenumberHelper)

View File

@@ -1,47 +1,40 @@
<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Chill\MainBundle\Serializer\Normalizer;
use libphonenumber\NumberParseException;
use libphonenumber\PhoneNumber;
use libphonenumber\PhoneNumberUtil;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\Serializer\Exception\CircularReferenceException;
use Symfony\Component\Serializer\Exception\ExceptionInterface;
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
use Symfony\Component\Serializer\Exception\LogicException;
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
class PhonenumberNormalizer implements NormalizerInterface, DenormalizerInterface
{
private PhoneNumberUtil $phoneNumberUtil;
private string $defaultCarrierCode;
private PhoneNumberUtil $phoneNumberUtil;
public function __construct(ParameterBagInterface $parameterBag)
{
$this->defaultCarrierCode = $parameterBag->get('chill_main')['phone_helper']['default_carrier_code'];
$this->phoneNumberUtil = PhoneNumberUtil::getInstance();
}
public function normalize($object, string $format = null, array $context = []): string
{
return $this->phoneNumberUtil->formatOutOfCountryCallingNumber($object, $this->defaultCarrierCode);
}
public function supportsNormalization($data, string $format = null)
{
return $data instanceof PhoneNumber;
}
/**
* {@inheritdoc}
*
* @throws UnexpectedValueException
*/
public function denormalize($data, $class, $format = null, array $context = array())
public function denormalize($data, $class, $format = null, array $context = [])
{
try {
return $this->phoneNumberUtil->parse($data, $this->defaultCarrierCode);
@@ -50,11 +43,18 @@ class PhonenumberNormalizer implements NormalizerInterface, DenormalizerInterfac
}
}
/**
* {@inheritdoc}
*/
public function normalize($object, ?string $format = null, array $context = []): string
{
return $this->phoneNumberUtil->formatOutOfCountryCallingNumber($object, $this->defaultCarrierCode);
}
public function supportsDenormalization($data, $type, $format = null)
{
return $type === 'libphonenumber\PhoneNumber';
return 'libphonenumber\PhoneNumber' === $type;
}
public function supportsNormalization($data, ?string $format = null)
{
return $data instanceof PhoneNumber;
}
}

View File

@@ -12,30 +12,90 @@ declare(strict_types=1);
namespace Chill\MainBundle\Tests\Routing\Loader;
use Chill\MainBundle\Phonenumber\PhonenumberHelper;
use Chill\MainBundle\Phonenumber\PhoneNumberHelperInterface;
use Psr\Log\NullLogger;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
/**
* @internal
* @coversNothing
*/
final class PhonenumberHelperTest extends KernelTestCase
{
/**
* @dataProvider normalizePhonenumbers
*/
public function testNormalizePhonenumbers(string $defaultCarrierCode, string $phoneNumber, string $expected)
public function denormalizePhonenumbers()
{
$subject = new PhonenumberHelper(
new ArrayAdapter(),
new ParameterBag([
'chill_main.phone_helper' => [
'default_carrier_code' => $defaultCarrierCode,
],
]),
new NullLogger()
);
yield [
'BE',
'+3281136917',
'+3281136917',
];
$this->assertEquals($expected, $subject->normalize($phoneNumber));
yield [
'BE',
'+33 6 23 12 45 54',
'+33623124554',
];
}
public function formatPhonenumbers()
{
yield [
'BE',
'+3281136917',
'081 13 69 17',
];
yield [
'FR',
'+33 6 23 12 45 54',
'06 23 12 45 54',
];
yield [
'FR',
'+32 81 13 69 17',
'081 13 69 17',
];
yield [
'BE',
'+33 6 23 12 45 54',
'06 23 12 45 54',
];
}
public function normalizePhonenumbers()
{
yield [
'BE',
'081136917',
'+3281136917',
];
yield [
'BE',
'003281136917',
'+3281136917',
];
yield [
'BE',
'0032478123456',
'+32478123456',
];
yield [
'BE',
'0478123456',
'+32478123456',
];
yield [
'FR',
'0623124554',
'+33623124554',
];
}
/**
@@ -74,75 +134,21 @@ final class PhonenumberHelperTest extends KernelTestCase
$this->assertEquals($expected, $subject->format($phoneNumber));
}
public function formatPhonenumbers() {
yield [
'BE',
'+3281136917',
'081 13 69 17',
];
/**
* @dataProvider normalizePhonenumbers
*/
public function testNormalizePhonenumbers(string $defaultCarrierCode, string $phoneNumber, string $expected)
{
$subject = new PhonenumberHelper(
new ArrayAdapter(),
new ParameterBag([
'chill_main.phone_helper' => [
'default_carrier_code' => $defaultCarrierCode,
],
]),
new NullLogger()
);
yield [
'FR',
'+33 6 23 12 45 54',
'06 23 12 45 54',
];
yield [
'FR',
'+32 81 13 69 17',
'081 13 69 17',
];
yield [
'BE',
'+33 6 23 12 45 54',
'06 23 12 45 54',
];
}
public function normalizePhonenumbers() {
yield [
'BE',
'081136917',
'+3281136917',
];
yield [
'BE',
'003281136917',
'+3281136917',
];
yield [
'BE',
'0032478123456',
'+32478123456',
];
yield [
'BE',
'0478123456',
'+32478123456',
];
yield [
'FR',
'0623124554',
'+33623124554',
];
}
public function denormalizePhonenumbers() {
yield [
'BE',
'+3281136917',
'+3281136917',
];
yield [
'BE',
'+33 6 23 12 45 54',
'+33623124554',
];
$this->assertEquals($expected, $subject->normalize($phoneNumber));
}
}