Skip to content

Commit d6b67d4

Browse files
ro0NLfabpot
authored andcommitted
[Intl] Simplify API
1 parent 9a7a276 commit d6b67d4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+3767
-60
lines changed

UPGRADE-4.3.md

+9
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,15 @@ HttpKernel
9494
* Renamed `PostResponseEvent` to `TerminateEvent`
9595
* Deprecated `TranslatorListener` in favor of `LocaleAwareListener`
9696

97+
Intl
98+
----
99+
100+
* Deprecated `ResourceBundle` namespace
101+
* Deprecated `Intl::getCurrencyBundle()`, use `Currencies` instead
102+
* Deprecated `Intl::getLanguageBundle()`, use `Languages` or `Scripts` instead
103+
* Deprecated `Intl::getLocaleBundle()`, use `Locales` instead
104+
* Deprecated `Intl::getRegionBundle()`, use `Regions` instead
105+
97106
Messenger
98107
---------
99108

UPGRADE-5.0.md

+9
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,15 @@ HttpKernel
237237
* Removed `PostResponseEvent`, use `TerminateEvent` instead
238238
* Removed `TranslatorListener` in favor of `LocaleAwareListener`
239239

240+
Intl
241+
----
242+
243+
* Removed `ResourceBundle` namespace
244+
* Removed `Intl::getLanguageBundle()`, use `Languages` or `Scripts` instead
245+
* Removed `Intl::getCurrencyBundle()`, use `Currencies` instead
246+
* Removed `Intl::getLocaleBundle()`, use `Locales` instead
247+
* Removed `Intl::getRegionBundle()`, use `Regions` instead
248+
240249
Messenger
241250
---------
242251

src/Symfony/Component/Form/Extension/Core/Type/CountryType.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
1616
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
1717
use Symfony\Component\Form\ChoiceList\Loader\IntlCallbackChoiceLoader;
18-
use Symfony\Component\Intl\Intl;
18+
use Symfony\Component\Intl\Regions;
1919
use Symfony\Component\OptionsResolver\Options;
2020
use Symfony\Component\OptionsResolver\OptionsResolver;
2121

@@ -44,7 +44,7 @@ public function configureOptions(OptionsResolver $resolver)
4444
$choiceTranslationLocale = $options['choice_translation_locale'];
4545

4646
return new IntlCallbackChoiceLoader(function () use ($choiceTranslationLocale) {
47-
return array_flip(Intl::getRegionBundle()->getCountryNames($choiceTranslationLocale));
47+
return array_flip(Regions::getNames($choiceTranslationLocale));
4848
});
4949
},
5050
'choice_translation_domain' => false,
@@ -83,7 +83,7 @@ public function loadChoiceList($value = null)
8383
return $this->choiceList;
8484
}
8585

86-
return $this->choiceList = new ArrayChoiceList(array_flip(Intl::getRegionBundle()->getCountryNames()), $value);
86+
return $this->choiceList = new ArrayChoiceList(array_flip(Regions::getNames()), $value);
8787
}
8888

8989
/**

src/Symfony/Component/Form/Extension/Core/Type/CurrencyType.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
1616
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
1717
use Symfony\Component\Form\ChoiceList\Loader\IntlCallbackChoiceLoader;
18-
use Symfony\Component\Intl\Intl;
18+
use Symfony\Component\Intl\Currencies;
1919
use Symfony\Component\OptionsResolver\Options;
2020
use Symfony\Component\OptionsResolver\OptionsResolver;
2121

@@ -44,7 +44,7 @@ public function configureOptions(OptionsResolver $resolver)
4444
$choiceTranslationLocale = $options['choice_translation_locale'];
4545

4646
return new IntlCallbackChoiceLoader(function () use ($choiceTranslationLocale) {
47-
return array_flip(Intl::getCurrencyBundle()->getCurrencyNames($choiceTranslationLocale));
47+
return array_flip(Currencies::getNames($choiceTranslationLocale));
4848
});
4949
},
5050
'choice_translation_domain' => false,
@@ -83,7 +83,7 @@ public function loadChoiceList($value = null)
8383
return $this->choiceList;
8484
}
8585

86-
return $this->choiceList = new ArrayChoiceList(array_flip(Intl::getCurrencyBundle()->getCurrencyNames()), $value);
86+
return $this->choiceList = new ArrayChoiceList(array_flip(Currencies::getNames()), $value);
8787
}
8888

8989
/**

src/Symfony/Component/Form/Extension/Core/Type/LanguageType.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
1616
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
1717
use Symfony\Component\Form\ChoiceList\Loader\IntlCallbackChoiceLoader;
18-
use Symfony\Component\Intl\Intl;
18+
use Symfony\Component\Intl\Languages;
1919
use Symfony\Component\OptionsResolver\Options;
2020
use Symfony\Component\OptionsResolver\OptionsResolver;
2121

@@ -44,7 +44,7 @@ public function configureOptions(OptionsResolver $resolver)
4444
$choiceTranslationLocale = $options['choice_translation_locale'];
4545

4646
return new IntlCallbackChoiceLoader(function () use ($choiceTranslationLocale) {
47-
return array_flip(Intl::getLanguageBundle()->getLanguageNames($choiceTranslationLocale));
47+
return array_flip(Languages::getNames($choiceTranslationLocale));
4848
});
4949
},
5050
'choice_translation_domain' => false,
@@ -83,7 +83,7 @@ public function loadChoiceList($value = null)
8383
return $this->choiceList;
8484
}
8585

86-
return $this->choiceList = new ArrayChoiceList(array_flip(Intl::getLanguageBundle()->getLanguageNames()), $value);
86+
return $this->choiceList = new ArrayChoiceList(array_flip(Languages::getNames()), $value);
8787
}
8888

8989
/**

src/Symfony/Component/Form/Extension/Core/Type/LocaleType.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
1616
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
1717
use Symfony\Component\Form\ChoiceList\Loader\IntlCallbackChoiceLoader;
18-
use Symfony\Component\Intl\Intl;
18+
use Symfony\Component\Intl\Locales;
1919
use Symfony\Component\OptionsResolver\Options;
2020
use Symfony\Component\OptionsResolver\OptionsResolver;
2121

@@ -44,7 +44,7 @@ public function configureOptions(OptionsResolver $resolver)
4444
$choiceTranslationLocale = $options['choice_translation_locale'];
4545

4646
return new IntlCallbackChoiceLoader(function () use ($choiceTranslationLocale) {
47-
return array_flip(Intl::getLocaleBundle()->getLocaleNames($choiceTranslationLocale));
47+
return array_flip(Locales::getNames($choiceTranslationLocale));
4848
});
4949
},
5050
'choice_translation_domain' => false,
@@ -83,7 +83,7 @@ public function loadChoiceList($value = null)
8383
return $this->choiceList;
8484
}
8585

86-
return $this->choiceList = new ArrayChoiceList(array_flip(Intl::getLocaleBundle()->getLocaleNames()), $value);
86+
return $this->choiceList = new ArrayChoiceList(array_flip(Locales::getNames()), $value);
8787
}
8888

8989
/**

src/Symfony/Component/Form/composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"require": {
1919
"php": "^7.1.3",
2020
"symfony/event-dispatcher": "^4.3",
21-
"symfony/intl": "~3.4|~4.0",
21+
"symfony/intl": "^4.3",
2222
"symfony/options-resolver": "~4.2",
2323
"symfony/polyfill-ctype": "~1.8",
2424
"symfony/polyfill-mbstring": "~1.0",
@@ -43,6 +43,7 @@
4343
"symfony/doctrine-bridge": "<3.4",
4444
"symfony/framework-bundle": "<3.4",
4545
"symfony/http-kernel": "<4.3",
46+
"symfony/intl": "<4.3",
4647
"symfony/translation": "<4.2",
4748
"symfony/twig-bridge": "<3.4.5|<4.0.5,>=4.0"
4849
},

src/Symfony/Component/Intl/CHANGELOG.md

+9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
CHANGELOG
22
=========
33

4+
4.3.0
5+
-----
6+
7+
* deprecated `ResourceBundle` namespace
8+
* added `Currencies` in favor of `Intl::getCurrencyBundle()`
9+
* added `Languages` and `Scripts` in favor of `Intl::getLanguageBundle()`
10+
* added `Locales` in favor of `Intl::getLocaleBundle()`
11+
* added `Regions` in favor of `Intl::getRegionBundle()`
12+
413
4.2.0
514
-----
615

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Intl;
13+
14+
use Symfony\Component\Intl\Exception\MissingResourceException;
15+
16+
/**
17+
* Gives access to currency-related ICU data.
18+
*
19+
* @author Bernhard Schussek <bschussek@gmail.com>
20+
* @author Roland Franssen <franssen.roland@gmail.com>
21+
*/
22+
final class Currencies extends ResourceBundle
23+
{
24+
private const INDEX_SYMBOL = 0;
25+
private const INDEX_NAME = 1;
26+
private const INDEX_FRACTION_DIGITS = 0;
27+
private const INDEX_ROUNDING_INCREMENT = 1;
28+
29+
/**
30+
* @return string[]
31+
*/
32+
public static function getCurrencyCodes(): array
33+
{
34+
return self::readEntry(['Currencies'], 'meta');
35+
}
36+
37+
public static function exists(string $currency): bool
38+
{
39+
try {
40+
self::readEntry(['Names', $currency, self::INDEX_NAME]);
41+
42+
return true;
43+
} catch (MissingResourceException $e) {
44+
return false;
45+
}
46+
}
47+
48+
public static function getName(string $currency, string $displayLocale = null): string
49+
{
50+
return self::readEntry(['Names', $currency, self::INDEX_NAME], $displayLocale);
51+
}
52+
53+
/**
54+
* @return string[]
55+
*/
56+
public static function getNames(string $displayLocale = null): array
57+
{
58+
// ====================================================================
59+
// For reference: It is NOT possible to return names indexed by
60+
// numeric code here, because some numeric codes map to multiple
61+
// 3-letter codes (e.g. 32 => "ARA", "ARP", "ARS")
62+
// ====================================================================
63+
64+
$names = self::readEntry(['Names'], $displayLocale);
65+
66+
if ($names instanceof \Traversable) {
67+
$names = iterator_to_array($names);
68+
}
69+
70+
array_walk($names, function (&$value) {
71+
$value = $value[self::INDEX_NAME];
72+
});
73+
74+
return self::asort($names, $displayLocale);
75+
}
76+
77+
public static function getSymbol(string $currency, string $displayLocale = null): string
78+
{
79+
return self::readEntry(['Names', $currency, self::INDEX_SYMBOL], $displayLocale);
80+
}
81+
82+
public static function getFractionDigits(string $currency): int
83+
{
84+
try {
85+
return self::readEntry(['Meta', $currency, self::INDEX_FRACTION_DIGITS], 'meta');
86+
} catch (MissingResourceException $e) {
87+
return self::readEntry(['Meta', 'DEFAULT', self::INDEX_FRACTION_DIGITS], 'meta');
88+
}
89+
}
90+
91+
/**
92+
* @return float|int
93+
*/
94+
public static function getRoundingIncrement(string $currency)
95+
{
96+
try {
97+
return self::readEntry(['Meta', $currency, self::INDEX_ROUNDING_INCREMENT], 'meta');
98+
} catch (MissingResourceException $e) {
99+
return self::readEntry(['Meta', 'DEFAULT', self::INDEX_ROUNDING_INCREMENT], 'meta');
100+
}
101+
}
102+
103+
public static function getNumericCode(string $currency): int
104+
{
105+
return self::readEntry(['Alpha3ToNumeric', $currency], 'meta');
106+
}
107+
108+
public static function forNumericCode(int $numericCode): array
109+
{
110+
return self::readEntry(['NumericToAlpha3', (string) $numericCode], 'meta');
111+
}
112+
113+
protected static function getPath(): string
114+
{
115+
return Intl::getDataDirectory().'/'.Intl::CURRENCY_DIR;
116+
}
117+
}

src/Symfony/Component/Intl/Data/Provider/CurrencyDataProvider.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
*
2121
* @author Bernhard Schussek <bschussek@gmail.com>
2222
*
23-
* @internal
23+
* @internal to be removed in 5.0.
2424
*/
2525
class CurrencyDataProvider
2626
{

src/Symfony/Component/Intl/Data/Provider/LanguageDataProvider.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
*
2020
* @author Bernhard Schussek <bschussek@gmail.com>
2121
*
22-
* @internal
22+
* @internal to be removed in 5.0.
2323
*/
2424
class LanguageDataProvider
2525
{

src/Symfony/Component/Intl/Data/Provider/LocaleDataProvider.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
*
2020
* @author Bernhard Schussek <bschussek@gmail.com>
2121
*
22-
* @internal
22+
* @internal to be removed in 5.0.
2323
*/
2424
class LocaleDataProvider
2525
{

src/Symfony/Component/Intl/Data/Provider/RegionDataProvider.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
*
2020
* @author Bernhard Schussek <bschussek@gmail.com>
2121
*
22-
* @internal
22+
* @internal to be removed in 5.0.
2323
*/
2424
class RegionDataProvider
2525
{

src/Symfony/Component/Intl/Data/Provider/ScriptDataProvider.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
*
2020
* @author Bernhard Schussek <bschussek@gmail.com>
2121
*
22-
* @internal
22+
* @internal to be removed in 5.0.
2323
*/
2424
class ScriptDataProvider
2525
{

0 commit comments

Comments
 (0)