diff --git a/src/Bundle/ChillMainBundle/Entity/Location.php b/src/Bundle/ChillMainBundle/Entity/Location.php index bff3ff37b..437ac1714 100644 --- a/src/Bundle/ChillMainBundle/Entity/Location.php +++ b/src/Bundle/ChillMainBundle/Entity/Location.php @@ -18,6 +18,7 @@ use Chill\MainBundle\Validation\Constraint\PhonenumberConstraint; use DateTimeImmutable; use DateTimeInterface; use Doctrine\ORM\Mapping as ORM; +use libphonenumber\PhoneNumber; use Symfony\Component\Serializer\Annotation as Serializer; use Symfony\Component\Serializer\Annotation\DiscriminatorMap; use Symfony\Component\Validator\Constraints as Assert; @@ -90,20 +91,18 @@ class Location implements TrackCreationInterface, TrackUpdateInterface private ?string $name = null; /** - * @ORM\Column(type="string", length=64, nullable=true) + * @ORM\Column(type="phone_number") * @Serializer\Groups({"read", "write", "docgen:read"}) - * @Assert\Regex(pattern="/^([\+{1}])([0-9\s*]{4,20})$/") * @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"}) - * @Assert\Regex(pattern="/^([\+{1}])([0-9\s*]{4,20})$/") * @PhonenumberConstraint(type="any") */ - private ?string $phonenumber2 = null; + private ?PhoneNumber $phonenumber2 = null; /** * @ORM\Column(type="datetime_immutable", nullable=true) @@ -162,12 +161,12 @@ class Location implements TrackCreationInterface, TrackUpdateInterface return $this->name; } - public function getPhonenumber1(): ?string + public function getPhonenumber1(): ?PhoneNumber { return $this->phonenumber1; } - public function getPhonenumber2(): ?string + public function getPhonenumber2(): ?PhoneNumber { return $this->phonenumber2; } @@ -238,14 +237,14 @@ class Location implements TrackCreationInterface, TrackUpdateInterface return $this; } - public function setPhonenumber1(?string $phonenumber1): self + public function setPhonenumber1(?PhoneNumber $phonenumber1): self { $this->phonenumber1 = $phonenumber1; return $this; } - public function setPhonenumber2(?string $phonenumber2): self + public function setPhonenumber2(?PhoneNumber $phonenumber2): self { $this->phonenumber2 = $phonenumber2; diff --git a/src/Bundle/ChillMainBundle/Form/LocationFormType.php b/src/Bundle/ChillMainBundle/Form/LocationFormType.php index 713705de3..de10e88a5 100644 --- a/src/Bundle/ChillMainBundle/Form/LocationFormType.php +++ b/src/Bundle/ChillMainBundle/Form/LocationFormType.php @@ -12,6 +12,7 @@ declare(strict_types=1); namespace Chill\MainBundle\Form; use Chill\MainBundle\Entity\LocationType as EntityLocationType; +use Chill\MainBundle\Form\Type\ChillPhoneNumberType; use Chill\MainBundle\Form\Type\PickAddressType; use Chill\MainBundle\Templating\TranslatableStringHelper; use Symfony\Bridge\Doctrine\Form\Type\EntityType; @@ -46,8 +47,8 @@ final class LocationFormType extends AbstractType }, ]) ->add('name', TextType::class) - ->add('phonenumber1', TextType::class, ['required' => false]) - ->add('phonenumber2', TextType::class, ['required' => false]) + ->add('phonenumber1', ChillPhoneNumberType::class, ['required' => false]) + ->add('phonenumber2',ChillPhoneNumberType::class, ['required' => false]) ->add('email', TextType::class, ['required' => false]) ->add('address', PickAddressType::class, [ 'required' => false, diff --git a/src/Bundle/ChillMainBundle/Resources/views/Location/index.html.twig b/src/Bundle/ChillMainBundle/Resources/views/Location/index.html.twig index bc39a9275..a04ae73a9 100644 --- a/src/Bundle/ChillMainBundle/Resources/views/Location/index.html.twig +++ b/src/Bundle/ChillMainBundle/Resources/views/Location/index.html.twig @@ -18,8 +18,10 @@ {% for entity in entities %} {{ entity.name }} - {{ entity.phonenumber1 }} - {{ entity.phonenumber2 }} + + {{ entity.phonenumber1|chill_format_phonenumber }} + + {{ entity.phonenumber2|chill_format_phonenumber }} {{ entity.email }} {% if entity.address is not null %} diff --git a/src/Bundle/ChillMainBundle/migrations/Version20220302132728.php b/src/Bundle/ChillMainBundle/migrations/Version20220302132728.php new file mode 100644 index 000000000..ca01f6e5c --- /dev/null +++ b/src/Bundle/ChillMainBundle/migrations/Version20220302132728.php @@ -0,0 +1,75 @@ +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); + } +}