Add new PhoneNumberType.

This commit is contained in:
Pol Dellaiera 2021-06-08 11:38:02 +02:00
parent ce8399945a
commit 5b76338fb0
No known key found for this signature in database
GPG Key ID: D476DFE9C67467CA
4 changed files with 181 additions and 1 deletions

View File

@ -0,0 +1,45 @@
<?php
/**
* 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\PersonBundle\Form\DataTransformer;
use Symfony\Component\Form\DataTransformerInterface;
final class PhoneNumberTypeDataTransformer implements DataTransformerInterface
{
private string $countryCodePrefix;
public function __construct(string $countryCodePrefix)
{
$this->countryCodePrefix = $countryCodePrefix;
}
/**
* @param string $phoneNumber
*/
public function reverseTransform($phoneNumber): string
{
$phoneNumber = preg_replace('/[^0-9+]/', '', $phoneNumber);
if (0 === strpos($phoneNumber, '00', 0)) {
$phoneNumber = substr_replace($phoneNumber, '+', 0, 2);
}
if (0 === strpos($phoneNumber, '0', 0)) {
$phoneNumber = substr_replace($phoneNumber, $this->countryCodePrefix, 0, 1);
}
return $phoneNumber;
}
public function transform($phoneNumber)
{
return $phoneNumber;
}
}

View File

@ -36,7 +36,7 @@ class PersonPhoneType extends AbstractType
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('phonenumber', TelType::class, [
$builder->add('phonenumber', PhoneNumberType::class, [
'label' => 'Other phonenumber',
'required' => true,
]);

View File

@ -0,0 +1,51 @@
<?php
/**
* 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\PersonBundle\Form\Type;
use Chill\PersonBundle\Form\DataTransformer\PhoneNumberTypeDataTransformer;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TelType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
final class PhoneNumberType extends AbstractType
{
private ParameterBagInterface $parameterBag;
public function __construct(ParameterBagInterface $parameterBag)
{
$this->parameterBag = $parameterBag;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->addViewTransformer(
new PhoneNumberTypeDataTransformer($options['default_country_prefix'])
);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver
->setDefined('default_country_prefix')
->setAllowedTypes('default_country_prefix', 'string')
->setDefaults([
// TODO: Is it the best way to do that?
'default_country_prefix' => $this->parameterBag->get('chill_main.phonenumber_default_country_code') ?? '+32',
]);
}
public function getParent(): string
{
return TelType::class;
}
}

View File

@ -0,0 +1,84 @@
<?php
/**
* 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\PersonBundle\Tests\Form\Type;
use Chill\PersonBundle\Form\Type\PhoneNumberType;
use Locale;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\Form\PreloadedExtension;
use Symfony\Component\Form\Test\TypeTestCase;
final class PhoneNumberTypeTest extends TypeTestCase
{
private ParameterBagInterface $parameterBag;
protected function setUp(): void
{
Locale::setDefault('en');
// mock any dependencies
$this->parameterBag = $this->createMock(ParameterBagInterface::class);
// Mandatory
parent::setUp();
}
protected function getExtensions()
{
// create a type instance with the mocked dependencies
$type = new PhoneNumberType($this->parameterBag);
return [
new PreloadedExtension([$type], []),
];
}
public function singleFieldProvider()
{
yield ['abc', [], ''];
yield ['abc', ['default_country_prefix' => '+34'], ''];
yield ['123', [], '123'];
yield ['0,-03f212@%3g4 g5k 6789', [], '+32123456789'];
yield ['0032 123456789', [], '+32123456789'];
yield ['+32 123456789', ['default_country_prefix' => '+34'], '+32123456789'];
yield ['0123456789', ['default_country_prefix' => '+33'], '+33123456789'];
yield ['0486/540660', [], '+32486540660'];
yield ['0486/540.660', ['default_country_prefix' => '+36'], '+36486540660'];
}
/**
* @dataProvider singleFieldProvider
*/
public function testSingleField(string $input, array $options, string $output)
{
$form = $this->factory->create(PhoneNumberType::class, null, $options);
$form->submit($input);
if (method_exists($form, 'getTransformationFailure') && $failure = $form->getTransformationFailure()) {
throw $failure;
}
self::assertTrue($form->isSynchronized());
$view = $form->createView();
self::assertSame($output, $view->vars['value']);
}
}