apply phonenumber on locations

This commit is contained in:
Julien Fastré 2022-03-02 15:08:16 +01:00
parent c8c2c4c859
commit eb6ec8a4af
4 changed files with 91 additions and 14 deletions

View File

@ -18,6 +18,7 @@ use Chill\MainBundle\Validation\Constraint\PhonenumberConstraint;
use DateTimeImmutable; use DateTimeImmutable;
use DateTimeInterface; use DateTimeInterface;
use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping as ORM;
use libphonenumber\PhoneNumber;
use Symfony\Component\Serializer\Annotation as Serializer; use Symfony\Component\Serializer\Annotation as Serializer;
use Symfony\Component\Serializer\Annotation\DiscriminatorMap; use Symfony\Component\Serializer\Annotation\DiscriminatorMap;
use Symfony\Component\Validator\Constraints as Assert; use Symfony\Component\Validator\Constraints as Assert;
@ -90,20 +91,18 @@ class Location implements TrackCreationInterface, TrackUpdateInterface
private ?string $name = null; private ?string $name = null;
/** /**
* @ORM\Column(type="string", length=64, nullable=true) * @ORM\Column(type="phone_number")
* @Serializer\Groups({"read", "write", "docgen:read"}) * @Serializer\Groups({"read", "write", "docgen:read"})
* @Assert\Regex(pattern="/^([\+{1}])([0-9\s*]{4,20})$/")
* @PhonenumberConstraint(type="any") * @PhonenumberConstraint(type="any")
*/ */
private ?string $phonenumber1 = null; private ?PhoneNumber $phonenumber1 = null;
/** /**
* @ORM\Column(type="string", length=64, nullable=true) * @ORM\Column(type="phone_number")
* @Serializer\Groups({"read", "write", "docgen:read"}) * @Serializer\Groups({"read", "write", "docgen:read"})
* @Assert\Regex(pattern="/^([\+{1}])([0-9\s*]{4,20})$/")
* @PhonenumberConstraint(type="any") * @PhonenumberConstraint(type="any")
*/ */
private ?string $phonenumber2 = null; private ?PhoneNumber $phonenumber2 = null;
/** /**
* @ORM\Column(type="datetime_immutable", nullable=true) * @ORM\Column(type="datetime_immutable", nullable=true)
@ -162,12 +161,12 @@ class Location implements TrackCreationInterface, TrackUpdateInterface
return $this->name; return $this->name;
} }
public function getPhonenumber1(): ?string public function getPhonenumber1(): ?PhoneNumber
{ {
return $this->phonenumber1; return $this->phonenumber1;
} }
public function getPhonenumber2(): ?string public function getPhonenumber2(): ?PhoneNumber
{ {
return $this->phonenumber2; return $this->phonenumber2;
} }
@ -238,14 +237,14 @@ class Location implements TrackCreationInterface, TrackUpdateInterface
return $this; return $this;
} }
public function setPhonenumber1(?string $phonenumber1): self public function setPhonenumber1(?PhoneNumber $phonenumber1): self
{ {
$this->phonenumber1 = $phonenumber1; $this->phonenumber1 = $phonenumber1;
return $this; return $this;
} }
public function setPhonenumber2(?string $phonenumber2): self public function setPhonenumber2(?PhoneNumber $phonenumber2): self
{ {
$this->phonenumber2 = $phonenumber2; $this->phonenumber2 = $phonenumber2;

View File

@ -12,6 +12,7 @@ declare(strict_types=1);
namespace Chill\MainBundle\Form; namespace Chill\MainBundle\Form;
use Chill\MainBundle\Entity\LocationType as EntityLocationType; use Chill\MainBundle\Entity\LocationType as EntityLocationType;
use Chill\MainBundle\Form\Type\ChillPhoneNumberType;
use Chill\MainBundle\Form\Type\PickAddressType; use Chill\MainBundle\Form\Type\PickAddressType;
use Chill\MainBundle\Templating\TranslatableStringHelper; use Chill\MainBundle\Templating\TranslatableStringHelper;
use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Bridge\Doctrine\Form\Type\EntityType;
@ -46,8 +47,8 @@ final class LocationFormType extends AbstractType
}, },
]) ])
->add('name', TextType::class) ->add('name', TextType::class)
->add('phonenumber1', TextType::class, ['required' => false]) ->add('phonenumber1', ChillPhoneNumberType::class, ['required' => false])
->add('phonenumber2', TextType::class, ['required' => false]) ->add('phonenumber2',ChillPhoneNumberType::class, ['required' => false])
->add('email', TextType::class, ['required' => false]) ->add('email', TextType::class, ['required' => false])
->add('address', PickAddressType::class, [ ->add('address', PickAddressType::class, [
'required' => false, 'required' => false,

View File

@ -18,8 +18,10 @@
{% for entity in entities %} {% for entity in entities %}
<tr> <tr>
<td>{{ entity.name }}</td> <td>{{ entity.name }}</td>
<td>{{ entity.phonenumber1 }}</td> <td>
<td>{{ entity.phonenumber2 }}</td> {{ entity.phonenumber1|chill_format_phonenumber }}
</td>
<td>{{ entity.phonenumber2|chill_format_phonenumber }}</td>
<td>{{ entity.email }}</td> <td>{{ entity.email }}</td>
<td> <td>
{% if entity.address is not null %} {% if entity.address is not null %}

View File

@ -0,0 +1,75 @@
<?php
declare(strict_types=1);
namespace Chill\Migrations\Main;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
use libphonenumber\PhoneNumberUtil;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
final class Version20220302132728 extends AbstractMigration implements ContainerAwareInterface
{
use ContainerAwareTrait;
public function getDescription(): string
{
return 'Upgrade phonenumber on location';
}
public function up(Schema $schema): void
{
$carrier_code = $this->container
->getParameter('chill_main')['phone_helper']['default_carrier_code'];
if (null === $carrier_code) {
throw new \RuntimeException('no carrier code');
}
$this->addSql('ALTER TABLE chill_main_location ALTER phonenumber1 TYPE VARCHAR(35)');
$this->addSql('ALTER TABLE chill_main_location ALTER phonenumber1 DROP DEFAULT');
$this->addSql('ALTER TABLE chill_main_location ALTER phonenumber1 SET NOT NULL');
$this->addSql('ALTER TABLE chill_main_location ALTER phonenumber1 TYPE VARCHAR(35)');
$this->addSql('ALTER TABLE chill_main_location ALTER phonenumber2 TYPE VARCHAR(35)');
$this->addSql('ALTER TABLE chill_main_location ALTER phonenumber2 DROP DEFAULT');
$this->addSql('ALTER TABLE chill_main_location ALTER phonenumber2 SET NOT NULL');
$this->addSql('ALTER TABLE chill_main_location ALTER phonenumber2 TYPE VARCHAR(35)');
$this->addSql('COMMENT ON COLUMN chill_main_location.phonenumber1 IS \'(DC2Type:phone_number)\'');
$this->addSql('COMMENT ON COLUMN chill_main_location.phonenumber2 IS \'(DC2Type:phone_number)\'');
$this->addSql(
'UPDATE chill_main_location SET ' .
$this->buildMigrationPhonenumberClause($carrier_code, 'phonenumber1') .
', ' .
$this->buildMigrationPhoneNumberClause($carrier_code, 'phonenumber2')
);
}
public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE chill_main_location ALTER phonenumber1 TYPE VARCHAR(64)');
$this->addSql('ALTER TABLE chill_main_location ALTER phonenumber1 DROP DEFAULT');
$this->addSql('ALTER TABLE chill_main_location ALTER phonenumber1 DROP NOT NULL');
$this->addSql('ALTER TABLE chill_main_location ALTER phonenumber2 TYPE VARCHAR(64)');
$this->addSql('ALTER TABLE chill_main_location ALTER phonenumber2 DROP DEFAULT');
$this->addSql('ALTER TABLE chill_main_location ALTER phonenumber2 DROP NOT NULL');
$this->addSql('COMMENT ON COLUMN chill_main_location.phonenumber1 IS NULL');
$this->addSql('COMMENT ON COLUMN chill_main_location.phonenumber2 IS NULL');
}
private function buildMigrationPhoneNumberClause(string $defaultCarriercode, string $field): string
{
$util = PhoneNumberUtil::getInstance();
$countryCode = $util->getCountryCodeForRegion($defaultCarriercode);
return sprintf('%s=CASE
WHEN %s = \'\' THEN NULL
WHEN LEFT(%s, 1) = \'0\'
THEN \'%s\' || replace(replace(substr(%s, 2), \'(0)\', \'\'), \' \', \'\')
ELSE replace(replace(%s, \'(0)\', \'\'),\' \', \'\')
END', $field, $field, $field, $countryCode, $field, $field);
}
}