From 0fafd73c792e1d18f543269c63ac27c76cb5b322 Mon Sep 17 00:00:00 2001 From: Geordie Date: Tue, 22 Aug 2023 13:26:27 +0200 Subject: [PATCH 1/4] Add country_display_emoji_flag option to allow display of the flag in the form widget --- README.md | 6 ++++++ composer.json | 1 + src/Form/Type/PhoneNumberType.php | 22 ++++++++++++++++++---- tests/Form/Type/PhoneNumberTypeTest.php | 20 +++++++++++++++++++- 4 files changed, 44 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index ea16d1f1..d7ec2eec 100644 --- a/README.md +++ b/README.md @@ -175,6 +175,12 @@ The option `country_display_type` can be specified to change the country dropdow | `display_country_full` (default) | United Kingdom (+44) | | `display_country_short` | GB +44 | +And with the option `country_display_emoji_flag` set to `true` (default is `false`) you can add the emoji flag of the country before the label : +| display type | Result | +| ----------------------------------| --------------------------| +| `display_country_full` (default) | đŸ‡Ŧ🇧 United Kingdom (+44) | +| `display_country_short` | đŸ‡Ŧ🇧 GB +44 | + ### Validating phone numbers ℹī¸ _Using a Symfony or PHP version that does not support attributes? This bundle also supports validation as annotation. diff --git a/composer.json b/composer.json index 4be49cb7..98c4a84d 100644 --- a/composer.json +++ b/composer.json @@ -13,6 +13,7 @@ }, "require": { "php": ">=7.4", + "ext-mbstring": "*", "giggsey/libphonenumber-for-php": "^8.0", "symfony/framework-bundle": "^4.4|^5.3|^6.0", "symfony/intl": "^4.4|^5.3|^6.0" diff --git a/src/Form/Type/PhoneNumberType.php b/src/Form/Type/PhoneNumberType.php index 127b199c..aa498d8a 100644 --- a/src/Form/Type/PhoneNumberType.php +++ b/src/Form/Type/PhoneNumberType.php @@ -67,7 +67,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void continue; } - $label = $this->formatDisplayChoice($options['country_display_type'], $intlCountries[$regionCode], $regionCode, $countryCode); + $label = $this->formatDisplayChoice($options['country_display_type'], $intlCountries[$regionCode], $regionCode, $countryCode, $options['country_display_emoji_flag']); $countryChoices[$label] = $regionCode; } @@ -125,6 +125,7 @@ public function configureOptions(OptionsResolver $resolver): void 'error_bubbling' => false, 'country_choices' => [], 'country_display_type' => self::DISPLAY_COUNTRY_FULL, + 'country_display_emoji_flag' => false, 'country_placeholder' => false, 'preferred_country_choices' => [], 'country_options' => [], @@ -155,12 +156,25 @@ public function getBlockPrefix(): string return 'phone_number'; } - private function formatDisplayChoice(string $displayType, string $regionName, string $regionCode, string $countryCode): string + private function formatDisplayChoice(string $displayType, string $regionName, string $regionCode, string $countryCode, bool $displayEmojiFlag): string { + $formattedDisplay = sprintf('%s (+%s)', $regionName, $countryCode); if (self::DISPLAY_COUNTRY_SHORT === $displayType) { - return sprintf('%s +%s', $regionCode, $countryCode); + $formattedDisplay = sprintf('%s +%s', $regionCode, $countryCode); } - return sprintf('%s (+%s)', $regionName, $countryCode); + if ($displayEmojiFlag) { + $formattedDisplay = self::getEmojiFlag($regionCode).' '.$formattedDisplay; + } + + return $formattedDisplay; + } + + private static function getEmojiFlag(string $countryCode): string + { + $regionalOffset = 0x1F1A5; + + return mb_chr($regionalOffset + mb_ord($countryCode[0], 'UTF-8'), 'UTF-8') + .mb_chr($regionalOffset + mb_ord($countryCode[1], 'UTF-8'), 'UTF-8'); } } diff --git a/tests/Form/Type/PhoneNumberTypeTest.php b/tests/Form/Type/PhoneNumberTypeTest.php index 8eded9c0..0ab1971b 100644 --- a/tests/Form/Type/PhoneNumberTypeTest.php +++ b/tests/Form/Type/PhoneNumberTypeTest.php @@ -175,10 +175,11 @@ public function countryChoiceChoicesProvider() /** * @dataProvider countryChoiceFormatProvider */ - public function testCountryChoiceFormat(string $displayType, array $expectedChoices) + public function testCountryChoiceFormat(string $displayType, bool $displayEmojiFlag, array $expectedChoices) { $options['widget'] = PhoneNumberType::WIDGET_COUNTRY_CHOICE; $options['country_display_type'] = $displayType; + $options['country_display_emoji_flag'] = $displayEmojiFlag; $form = $this->factory->create(PhoneNumberType::class, null, $options); $view = $form->createView(); @@ -191,6 +192,7 @@ public function testCountryChoiceFormat(string $displayType, array $expectedChoi /** * 0 => Display type + * 1 => Display emoji flag * 2 => Expected choices. */ public function countryChoiceFormatProvider() @@ -198,16 +200,32 @@ public function countryChoiceFormatProvider() return [ [ PhoneNumberType::DISPLAY_COUNTRY_FULL, + false, [ $this->createChoiceView('United Kingdom (+44)', 'GB'), ], ], [ PhoneNumberType::DISPLAY_COUNTRY_SHORT, + false, [ $this->createChoiceView('GB +44', 'GB'), ], ], + [ + PhoneNumberType::DISPLAY_COUNTRY_FULL, + true, + [ + $this->createChoiceView('đŸ‡Ŧ🇧 United Kingdom (+44)', 'GB'), + ], + ], + [ + PhoneNumberType::DISPLAY_COUNTRY_SHORT, + true, + [ + $this->createChoiceView('đŸ‡Ŧ🇧 GB +44', 'GB'), + ], + ], ]; } From 4e2b10f29287b607566613016eee62b81158c949 Mon Sep 17 00:00:00 2001 From: Geordie Date: Mon, 28 Aug 2023 07:31:23 +0200 Subject: [PATCH 2/4] fix: remove require ext-mbstring --- composer.json | 1 - 1 file changed, 1 deletion(-) diff --git a/composer.json b/composer.json index 98c4a84d..4be49cb7 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,6 @@ }, "require": { "php": ">=7.4", - "ext-mbstring": "*", "giggsey/libphonenumber-for-php": "^8.0", "symfony/framework-bundle": "^4.4|^5.3|^6.0", "symfony/intl": "^4.4|^5.3|^6.0" From c2ff6deb3756426ce52ca4613bdb19b01642c77e Mon Sep 17 00:00:00 2001 From: Geordie Date: Tue, 29 Aug 2023 16:06:17 +0200 Subject: [PATCH 3/4] add polyfill-mbstring requirement --- composer.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 4be49cb7..da41d438 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,8 @@ "php": ">=7.4", "giggsey/libphonenumber-for-php": "^8.0", "symfony/framework-bundle": "^4.4|^5.3|^6.0", - "symfony/intl": "^4.4|^5.3|^6.0" + "symfony/intl": "^4.4|^5.3|^6.0", + "symfony/polyfill-mbstring": "^1.28" }, "require-dev": { "doctrine/doctrine-bundle": "^1.12|^2.0", From 977c83db04879d4bc6c12cb65df04c4981895e59 Mon Sep 17 00:00:00 2001 From: Geordie Date: Wed, 30 Aug 2023 18:29:14 +0200 Subject: [PATCH 4/4] doc: add CHANGELOG info about default option value change in future release --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ad0213ef..b21e8631 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Changed +- The default value for the `PhoneNumberType` form type option `country_display_emoji_flag` will change from `false` to `true` on the next major release + ## [3.9.2] - 2023-06-29 ### Changed