Skip to content

Commit

Permalink
bug #3268 IntlExtension > Return empty string when input is null (ruudk)
Browse files Browse the repository at this point in the history
This PR was submitted for the 3.x branch but it was merged into the 2.x branch instead.

Discussion
----------

IntlExtension > Return empty string when input is null

```twig
{{ inputCountry | country_name('en') }}
```
When `inputCountry` is `null` it will give this error:
```
Argument 1 passed to Twig\Extra\Intl\IntlExtension::getCountryName() must be of the type string, null given
```

With this change, an empty string will be returned instead.

See discussion in #3257 (comment)

Commits
-------

caecd2a IntlExtension > Return empty string when input is null
  • Loading branch information
fabpot committed Feb 13, 2020
2 parents 54a87ed + caecd2a commit 4d62307
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 6 deletions.
36 changes: 30 additions & 6 deletions extra/intl-extra/src/IntlExtension.php
Expand Up @@ -155,53 +155,77 @@ public function getFunctions()
];
}

public function getCountryName(string $country, string $locale = null): string
public function getCountryName(?string $country, string $locale = null): string
{
if ($country === null) {
return '';
}

try {
return Countries::getName($country, $locale);
} catch (MissingResourceException $exception) {
return $country;
}
}

public function getCurrencyName(string $currency, string $locale = null): string
public function getCurrencyName(?string $currency, string $locale = null): string
{
if ($currency === null) {
return '';
}

try {
return Currencies::getName($currency, $locale);
} catch (MissingResourceException $exception) {
return $currency;
}
}

public function getCurrencySymbol(string $currency, string $locale = null): string
public function getCurrencySymbol(?string $currency, string $locale = null): string
{
if ($currency === null) {
return '';
}

try {
return Currencies::getSymbol($currency, $locale);
} catch (MissingResourceException $exception) {
return $currency;
}
}

public function getLanguageName(string $language, string $locale = null): string
public function getLanguageName(?string $language, string $locale = null): string
{
if ($language === null) {
return '';
}

try {
return Languages::getName($language, $locale);
} catch (MissingResourceException $exception) {
return $language;
}
}

public function getLocaleName(string $data, string $locale = null): string
public function getLocaleName(?string $data, string $locale = null): string
{
if ($data === null) {
return '';
}

try {
return Locales::getName($data, $locale);
} catch (MissingResourceException $exception) {
return $data;
}
}

public function getTimezoneName(string $timezone, string $locale = null): string
public function getTimezoneName(?string $timezone, string $locale = null): string
{
if ($timezone === null) {
return '';
}

try {
return Timezones::getName($timezone, $locale);
} catch (MissingResourceException $exception) {
Expand Down
2 changes: 2 additions & 0 deletions extra/intl-extra/tests/Fixtures/country_name.test
Expand Up @@ -2,6 +2,7 @@
"country_name" filter
--TEMPLATE--
{{ 'UNKNOWN'|country_name }}
{{ null|country_name }}
{{ 'FR'|country_name }}
{{ 'US'|country_name }}
{{ 'US'|country_name('fr') }}
Expand All @@ -10,6 +11,7 @@
return [];
--EXPECT--
UNKNOWN

France
United States
États-Unis
Expand Down
2 changes: 2 additions & 0 deletions extra/intl-extra/tests/Fixtures/currency_name.test
Expand Up @@ -2,6 +2,7 @@
"currency_name" filter
--TEMPLATE--
{{ 'UNKNOWN'|currency_name }}
{{ null|currency_name }}
{{ 'EUR'|currency_name }}
{{ 'JPY'|currency_name }}
{{ 'EUR'|currency_name('fr') }}
Expand All @@ -10,6 +11,7 @@
return [];
--EXPECT--
UNKNOWN

Euro
Japanese Yen
euro
Expand Down
2 changes: 2 additions & 0 deletions extra/intl-extra/tests/Fixtures/currency_symbol.test
Expand Up @@ -2,11 +2,13 @@
"currency_symbol" filter
--TEMPLATE--
{{ 'UNKNOWN'|currency_symbol }}
{{ null|currency_symbol }}
{{ 'EUR'|currency_symbol }}
{{ 'JPY'|currency_symbol }}
--DATA--
return [];
--EXPECT--
UNKNOWN

¥
2 changes: 2 additions & 0 deletions extra/intl-extra/tests/Fixtures/language_name.test
Expand Up @@ -2,6 +2,7 @@
"language_name" filter
--TEMPLATE--
{{ 'UNKNOWN'|language_name }}
{{ null|language_name }}
{{ 'de'|language_name }}
{{ 'fr'|language_name }}
{{ 'de'|language_name('fr') }}
Expand All @@ -11,6 +12,7 @@
return [];
--EXPECT--
UNKNOWN

German
French
allemand
Expand Down
2 changes: 2 additions & 0 deletions extra/intl-extra/tests/Fixtures/locale_name.test
Expand Up @@ -2,6 +2,7 @@
"locale_name" filter
--TEMPLATE--
{{ 'UNKNOWN'|locale_name }}
{{ null|locale_name }}
{{ 'de'|locale_name }}
{{ 'fr'|locale_name }}
{{ 'de'|locale_name('fr') }}
Expand All @@ -11,6 +12,7 @@
return [];
--EXPECT--
UNKNOWN

German
French
allemand
Expand Down
2 changes: 2 additions & 0 deletions extra/intl-extra/tests/Fixtures/timezone_name.test
Expand Up @@ -2,13 +2,15 @@
"timezone_name" filter
--TEMPLATE--
{{ 'UNKNOWN'|timezone_name }}
{{ null|timezone_name }}
{{ 'Europe/Paris'|timezone_name }}
{{ 'America/Los_Angeles'|timezone_name }}
{{ 'America/Los_Angeles'|timezone_name('fr') }}
--DATA--
return [];
--EXPECT--
UNKNOWN

Central European Time (Paris)
Pacific Time (Los Angeles)
heure du Pacifique nord-américain (Los Angeles)

0 comments on commit 4d62307

Please sign in to comment.