Compare commits

...

6 Commits

Author SHA1 Message Date
Pol Dellaiera
bddb6e615e CS: Autofix. 2022-02-01 09:35:04 +01:00
Pol Dellaiera
a18ea30c8f chill-main work in progress 2022-02-01 09:35:04 +01:00
Pol Dellaiera
2ba240525c Add missing form type wirings. 2022-02-01 09:35:04 +01:00
Pol Dellaiera
5b76338fb0 Add new PhoneNumberType. 2022-02-01 09:35:04 +01:00
Pol Dellaiera
ce8399945a Add new configuration property. 2022-02-01 09:35:04 +01:00
Pol Dellaiera
1605bc5d08 New migration file. 2022-02-01 09:35:03 +01:00
8 changed files with 295 additions and 2 deletions

View File

@@ -41,6 +41,11 @@ class Configuration implements ConfigurationInterface
$rootNode
->children()
->scalarNode('phonenumber_default_country_code')
->cannotBeEmpty()
->isRequired()
->defaultValue('+32')
->end() // end of scalar 'phonenumber_default_country_code'
->scalarNode('installation_name')
->cannotBeEmpty()
->defaultValue('Chill')

View File

@@ -0,0 +1,47 @@
<?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\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

@@ -15,7 +15,6 @@ use Chill\MainBundle\Phonenumber\PhonenumberHelper;
use Chill\PersonBundle\Entity\PersonPhone;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TelType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
@@ -36,7 +35,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,53 @@
<?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\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,89 @@
<?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\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;
/**
* @internal
* @coversNothing
*/
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();
}
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']);
}
protected function getExtensions()
{
// create a type instance with the mocked dependencies
$type = new PhoneNumberType($this->parameterBag);
return [
new PreloadedExtension([$type], []),
];
}
}

View File

@@ -98,3 +98,8 @@ services:
autowire: true
autoconfigure: true
resource: '../Workflow/'
Chill\PersonBundle\Form\Type\:
resource: '../Form/Type'
tags:
- form.type

View File

@@ -27,3 +27,34 @@ services:
$config: "%chill_person.accompanying_period_fields%"
tags:
- { name: form.type }
chill.person.form.type.pick_person:
class: Chill\PersonBundle\Form\Type\PickPersonType
arguments:
- "@chill.person.repository.person"
- "@security.token_storage"
- "@chill.main.security.authorization.helper"
- '@Symfony\Component\Routing\Generator\UrlGeneratorInterface'
- '@Symfony\Component\Translation\TranslatorInterface'
tags:
- { name: form.type }
Chill\PersonBundle\Form\Type\PersonAltNameType:
arguments:
$configHelper: '@Chill\PersonBundle\Config\ConfigPersonAltNamesHelper'
$translatableStringHelper: '@chill.main.helper.translatable_string'
tags:
- { name: form.type }
Chill\PersonBundle\Form\Type\PersonPhoneType:
arguments:
$phonenumberHelper: '@Chill\MainBundle\Phonenumber\PhonenumberHelper'
$em: '@Doctrine\ORM\EntityManagerInterface'
tags:
- { name: form.type }
Chill\PersonBundle\Form\Type\PhoneNumberType:
autowire: true
autoconfigure: true
tags:
- { name: form.type }

View File

@@ -0,0 +1,64 @@
<?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\Migrations\Person;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20210518135515 extends AbstractMigration
{
public function down(Schema $schema): void
{
$this->addSql('UPDATE chill_person_person SET phonenumber = :pn WHERE phonenumber IS NULL', ['pn' => '']);
$this->addSql('UPDATE chill_person_person SET mobilenumber = :pn WHERE mobilenumber IS NULL', ['pn' => '']);
$this->addSql('ALTER TABLE chill_person_person ALTER phonenumber TYPE TEXT');
$this->addSql('ALTER TABLE chill_person_person ALTER phonenumber DROP DEFAULT');
$this->addSql('ALTER TABLE chill_person_person ALTER phonenumber SET NOT NULL');
$this->addSql('COMMENT ON COLUMN chill_person_person.phonenumber IS NULL');
$this->addSql('ALTER TABLE chill_person_phone ALTER phonenumber TYPE TEXT');
$this->addSql('ALTER TABLE chill_person_phone ALTER phonenumber DROP DEFAULT');
$this->addSql('COMMENT ON COLUMN chill_person_phone.phonenumber IS NULL');
$this->addSql('ALTER TABLE chill_person_person ALTER mobilenumber TYPE TEXT');
$this->addSql('ALTER TABLE chill_person_person ALTER mobilenumber DROP DEFAULT');
$this->addSql('COMMENT ON COLUMN chill_person_person.mobilenumber IS NULL');
}
public function getDescription(): string
{
return '';
}
public function up(Schema $schema): void
{
$this->addSql('ALTER TABLE chill_person_person ALTER phonenumber TYPE VARCHAR(35)');
$this->addSql('ALTER TABLE chill_person_person ALTER phonenumber DROP DEFAULT');
$this->addSql('ALTER TABLE chill_person_person ALTER phonenumber DROP NOT NULL');
$this->addSql('COMMENT ON COLUMN chill_person_person.phonenumber IS \'(DC2Type:phone_number)\'');
$this->addSql('ALTER TABLE chill_person_phone ALTER phonenumber TYPE VARCHAR(35)');
$this->addSql('ALTER TABLE chill_person_phone ALTER phonenumber DROP DEFAULT');
$this->addSql('COMMENT ON COLUMN chill_person_phone.phonenumber IS \'(DC2Type:phone_number)\'');
$this->addSql('ALTER TABLE chill_person_person ALTER mobilenumber TYPE VARCHAR(35)');
$this->addSql('ALTER TABLE chill_person_person ALTER mobilenumber DROP DEFAULT');
$this->addSql('COMMENT ON COLUMN chill_person_person.mobilenumber IS \'(DC2Type:phone_number)\'');
$this->addSql('UPDATE chill_person_person SET phonenumber = NULL WHERE phonenumber=:pn', ['pn' => '']);
$this->addSql('UPDATE chill_person_person SET mobilenumber = NULL WHERE mobilenumber=:pn', ['pn' => '']);
}
}