From 6a3c8017f0a1f5e0271e710c8905203b54fb0d4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Wed, 2 Mar 2022 15:50:01 +0100 Subject: [PATCH] upgrade phonenumber on 3party: migrations --- .../DataFixtures/ORM/LoadThirdParty.php | 12 +++- .../Entity/ThirdParty.php | 13 ++-- .../migrations/Version20220302143821.php | 62 +++++++++++++++++++ 3 files changed, 77 insertions(+), 10 deletions(-) create mode 100644 src/Bundle/ChillThirdPartyBundle/migrations/Version20220302143821.php diff --git a/src/Bundle/ChillThirdPartyBundle/DataFixtures/ORM/LoadThirdParty.php b/src/Bundle/ChillThirdPartyBundle/DataFixtures/ORM/LoadThirdParty.php index a1015686f..905461ca5 100644 --- a/src/Bundle/ChillThirdPartyBundle/DataFixtures/ORM/LoadThirdParty.php +++ b/src/Bundle/ChillThirdPartyBundle/DataFixtures/ORM/LoadThirdParty.php @@ -21,6 +21,7 @@ use Doctrine\Bundle\FixturesBundle\Fixture; use Doctrine\Common\DataFixtures\DependentFixtureInterface; use Doctrine\Persistence\ObjectManager; use Iterator; +use libphonenumber\PhoneNumberUtil; use Nelmio\Alice\Loader\NativeLoader; use Nelmio\Alice\ObjectSet; @@ -29,6 +30,13 @@ use function count; class LoadThirdParty extends Fixture implements DependentFixtureInterface { + private PhoneNumberUtil $phoneNumberUtil; + + public function __construct() + { + $this->phoneNumberUtil = PhoneNumberUtil::getInstance(); + } + public function getDependencies() { return [ @@ -66,7 +74,7 @@ class LoadThirdParty extends Fixture implements DependentFixtureInterface Address::class => [ 'address1' => [ 'name' => '', - 'telephone' => '', + 'telephone' => $this->phoneNumberUtil->getExampleNumber('FR'), 'email' => '', 'comment' => '', ], @@ -116,7 +124,7 @@ class LoadThirdParty extends Fixture implements DependentFixtureInterface ThirdParty::class => [ 'thirdparty{1..75}' => [ 'name' => '', - 'telephone' => '', + 'telephone' => $this->phoneNumberUtil->getExampleNumber('FR'), 'email' => '', 'comment' => '', 'address' => '@address', diff --git a/src/Bundle/ChillThirdPartyBundle/Entity/ThirdParty.php b/src/Bundle/ChillThirdPartyBundle/Entity/ThirdParty.php index 25ef1be65..fa49dac3a 100644 --- a/src/Bundle/ChillThirdPartyBundle/Entity/ThirdParty.php +++ b/src/Bundle/ChillThirdPartyBundle/Entity/ThirdParty.php @@ -24,6 +24,7 @@ use DateTimeInterface; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; +use libphonenumber\PhoneNumber; use Symfony\Component\Serializer\Annotation\Context; use Symfony\Component\Serializer\Annotation\DiscriminatorMap; use Symfony\Component\Serializer\Annotation\Groups; @@ -253,14 +254,14 @@ class ThirdParty implements TrackCreationInterface, TrackUpdateInterface private ?ThirdPartyProfession $profession = null; /** - * @ORM\Column(name="telephone", type="string", length=64, nullable=true) + * @ORM\Column(name="telephone", type="phone_number", nullable=true) * @Assert\Regex("/^([\+{1}])([0-9\s*]{4,20})$/", * message="Invalid phone number: it should begin with the international prefix starting with ""+"", hold only digits and be smaller than 20 characters. Ex: +33123456789" * ) * @PhonenumberConstraint(type="any") * @Groups({"read", "write", "docgen:read", "docgen:read:3party:parent"}) */ - private ?string $telephone = null; + private ?PhoneNumber $telephone = null; /** * @ORM\Column(name="types", type="json", nullable=true) @@ -502,7 +503,7 @@ class ThirdParty implements TrackCreationInterface, TrackUpdateInterface /** * Get telephone. */ - public function getTelephone(): ?string + public function getTelephone(): ?PhoneNumber { return $this->telephone; } @@ -821,12 +822,8 @@ class ThirdParty implements TrackCreationInterface, TrackUpdateInterface /** * Set telephone. - * - * @param string|null $telephone - * - * @return ThirdParty */ - public function setTelephone($telephone = null) + public function setTelephone(?PhoneNumber $telephone = null): self { $this->telephone = $telephone; diff --git a/src/Bundle/ChillThirdPartyBundle/migrations/Version20220302143821.php b/src/Bundle/ChillThirdPartyBundle/migrations/Version20220302143821.php new file mode 100644 index 000000000..783cf8806 --- /dev/null +++ b/src/Bundle/ChillThirdPartyBundle/migrations/Version20220302143821.php @@ -0,0 +1,62 @@ +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); + } + + 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_3party.third_party ALTER telephone TYPE VARCHAR(35)'); + $this->addSql('ALTER TABLE chill_3party.third_party ALTER telephone DROP DEFAULT'); + $this->addSql('ALTER TABLE chill_3party.third_party ALTER telephone TYPE VARCHAR(35)'); + $this->addSql('COMMENT ON COLUMN chill_3party.third_party.telephone IS \'(DC2Type:phone_number)\''); + + $this->addSql( + 'UPDATE chill_3party.third_party SET ' . + $this->buildMigrationPhonenumberClause($carrier_code, 'telephone') + ); + } + + public function down(Schema $schema): void + { + $this->addSql('ALTER TABLE chill_3party.third_party ALTER telephone TYPE VARCHAR(64)'); + $this->addSql('ALTER TABLE chill_3party.third_party ALTER telephone DROP DEFAULT'); + $this->addSql('COMMENT ON COLUMN chill_3party.third_party.telephone IS NULL'); + } +}