Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to allow display of the flag in the form widget #152

Merged
merged 4 commits into from
Nov 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 :
maxhelias marked this conversation as resolved.
Show resolved Hide resolved
| 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.
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
22 changes: 18 additions & 4 deletions src/Form/Type/PhoneNumberType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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' => [],
Expand Down Expand Up @@ -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');
}
}
20 changes: 19 additions & 1 deletion tests/Form/Type/PhoneNumberTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -191,23 +192,40 @@ public function testCountryChoiceFormat(string $displayType, array $expectedChoi

/**
* 0 => Display type
* 1 => Display emoji flag
* 2 => Expected choices.
*/
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'),
],
],
];
}

Expand Down