Skip to content

Commit 61c9967

Browse files
committed
feature #28846 [Intl] Simplify API (ro0NL)
This PR was squashed before being merged into the 4.3-dev branch (closes #28846). Discussion ---------- [Intl] Simplify API | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no <!-- see https://symfony.com/bc --> | Deprecations? | no | Tests pass? | yes <!-- please add some, will be required by reviewers --> | Fixed tickets | #18368 | License | MIT | Doc PR | symfony/symfony-docs#11221 Simplifies the Intl API. It greatly reduces the no. of boilerplate classes in this component. Very over complicated, much wow :) Solving (IMHO): ```php class LanguageBundle extends LanguageDataProvider implements LanguageBundleInterface ``` Which seems very over complicated just to provide static data. ```php // before Intl::getLanguageBundle()->getLanguageName() // string | null // after Languages::getName() // string Languages::exists() // bool ``` I left out Canonicalization on puropose, that's a new topic to me. - [x] Languages - [x] Locales - [x] Currencies - [x] Regions - [x] Scripts - [ ] Timezones (#28831) - [x] Update constraints - [x] Update form types Thoughts? Commits ------- d6b67d469a [Intl] Simplify API
2 parents 31f6eec + 2022eaf commit 61c9967

11 files changed

+26
-30
lines changed

Constraints/Bic.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace Symfony\Component\Validator\Constraints;
1313

14-
use Symfony\Component\Intl\Intl;
14+
use Symfony\Component\Intl\Regions;
1515
use Symfony\Component\PropertyAccess\PropertyAccess;
1616
use Symfony\Component\Validator\Constraint;
1717
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
@@ -47,7 +47,7 @@ class Bic extends Constraint
4747

4848
public function __construct($options = null)
4949
{
50-
if (!class_exists(Intl::class)) {
50+
if (!class_exists(Regions::class)) {
5151
// throw new LogicException(sprintf('The "symfony/intl" component is required to use the "%s" constraint.', __CLASS__));
5252
@trigger_error(sprintf('Using the "%s" constraint without the "symfony/intl" component installed is deprecated since Symfony 4.2.', __CLASS__), E_USER_DEPRECATED);
5353
}

Constraints/BicValidator.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace Symfony\Component\Validator\Constraints;
1313

14-
use Symfony\Component\Intl\Intl;
14+
use Symfony\Component\Intl\Regions;
1515
use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException;
1616
use Symfony\Component\PropertyAccess\PropertyAccess;
1717
use Symfony\Component\PropertyAccess\PropertyAccessor;
@@ -105,8 +105,8 @@ public function validate($value, Constraint $constraint)
105105
}
106106

107107
// @deprecated since Symfony 4.2, will throw in 5.0
108-
if (class_exists(Intl::class)) {
109-
$validCountryCode = isset(Intl::getRegionBundle()->getCountryNames()[substr($canonicalize, 4, 2)]);
108+
if (class_exists(Regions::class)) {
109+
$validCountryCode = Regions::exists(substr($canonicalize, 4, 2));
110110
} else {
111111
$validCountryCode = ctype_alpha(substr($canonicalize, 4, 2));
112112
// throw new LogicException('The "symfony/intl" component is required to use the Bic constraint.');

Constraints/Country.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace Symfony\Component\Validator\Constraints;
1313

14-
use Symfony\Component\Intl\Intl;
14+
use Symfony\Component\Intl\Regions;
1515
use Symfony\Component\Validator\Constraint;
1616
use Symfony\Component\Validator\Exception\LogicException;
1717

@@ -33,7 +33,7 @@ class Country extends Constraint
3333

3434
public function __construct($options = null)
3535
{
36-
if (!class_exists(Intl::class)) {
36+
if (!class_exists(Regions::class)) {
3737
// throw new LogicException(sprintf('The "symfony/intl" component is required to use the "%s" constraint.', __CLASS__));
3838
@trigger_error(sprintf('Using the "%s" constraint without the "symfony/intl" component installed is deprecated since Symfony 4.2.', __CLASS__), E_USER_DEPRECATED);
3939
}

Constraints/CountryValidator.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace Symfony\Component\Validator\Constraints;
1313

14-
use Symfony\Component\Intl\Intl;
14+
use Symfony\Component\Intl\Regions;
1515
use Symfony\Component\Validator\Constraint;
1616
use Symfony\Component\Validator\ConstraintValidator;
1717
use Symfony\Component\Validator\Exception\LogicException;
@@ -42,14 +42,13 @@ public function validate($value, Constraint $constraint)
4242
throw new UnexpectedValueException($value, 'string');
4343
}
4444

45-
if (!class_exists(Intl::class)) {
45+
if (!class_exists(Regions::class)) {
4646
throw new LogicException('The "symfony/intl" component is required to use the Country constraint.');
4747
}
4848

4949
$value = (string) $value;
50-
$countries = Intl::getRegionBundle()->getCountryNames();
5150

52-
if (!isset($countries[$value])) {
51+
if (!Regions::exists($value)) {
5352
$this->context->buildViolation($constraint->message)
5453
->setParameter('{{ value }}', $this->formatValue($value))
5554
->setCode(Country::NO_SUCH_COUNTRY_ERROR)

Constraints/Currency.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace Symfony\Component\Validator\Constraints;
1313

14-
use Symfony\Component\Intl\Intl;
14+
use Symfony\Component\Intl\Currencies;
1515
use Symfony\Component\Validator\Constraint;
1616
use Symfony\Component\Validator\Exception\LogicException;
1717

@@ -34,7 +34,7 @@ class Currency extends Constraint
3434

3535
public function __construct($options = null)
3636
{
37-
if (!class_exists(Intl::class)) {
37+
if (!class_exists(Currencies::class)) {
3838
// throw new LogicException(sprintf('The "symfony/intl" component is required to use the "%s" constraint.', __CLASS__));
3939
@trigger_error(sprintf('Using the "%s" constraint without the "symfony/intl" component installed is deprecated since Symfony 4.2.', __CLASS__), E_USER_DEPRECATED);
4040
}

Constraints/CurrencyValidator.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace Symfony\Component\Validator\Constraints;
1313

14-
use Symfony\Component\Intl\Intl;
14+
use Symfony\Component\Intl\Currencies;
1515
use Symfony\Component\Validator\Constraint;
1616
use Symfony\Component\Validator\ConstraintValidator;
1717
use Symfony\Component\Validator\Exception\LogicException;
@@ -43,14 +43,13 @@ public function validate($value, Constraint $constraint)
4343
throw new UnexpectedValueException($value, 'string');
4444
}
4545

46-
if (!class_exists(Intl::class)) {
46+
if (!class_exists(Currencies::class)) {
4747
throw new LogicException('The "symfony/intl" component is required to use the Currency constraint.');
4848
}
4949

5050
$value = (string) $value;
51-
$currencies = Intl::getCurrencyBundle()->getCurrencyNames();
5251

53-
if (!isset($currencies[$value])) {
52+
if (!Currencies::exists($value)) {
5453
$this->context->buildViolation($constraint->message)
5554
->setParameter('{{ value }}', $this->formatValue($value))
5655
->setCode(Currency::NO_SUCH_CURRENCY_ERROR)

Constraints/Language.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace Symfony\Component\Validator\Constraints;
1313

14-
use Symfony\Component\Intl\Intl;
14+
use Symfony\Component\Intl\Languages;
1515
use Symfony\Component\Validator\Constraint;
1616
use Symfony\Component\Validator\Exception\LogicException;
1717

@@ -33,7 +33,7 @@ class Language extends Constraint
3333

3434
public function __construct($options = null)
3535
{
36-
if (!class_exists(Intl::class)) {
36+
if (!class_exists(Languages::class)) {
3737
// throw new LogicException(sprintf('The "symfony/intl" component is required to use the "%s" constraint.', __CLASS__));
3838
@trigger_error(sprintf('Using the "%s" constraint without the "symfony/intl" component installed is deprecated since Symfony 4.2.', __CLASS__), E_USER_DEPRECATED);
3939
}

Constraints/LanguageValidator.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace Symfony\Component\Validator\Constraints;
1313

14-
use Symfony\Component\Intl\Intl;
14+
use Symfony\Component\Intl\Languages;
1515
use Symfony\Component\Validator\Constraint;
1616
use Symfony\Component\Validator\ConstraintValidator;
1717
use Symfony\Component\Validator\Exception\LogicException;
@@ -42,14 +42,13 @@ public function validate($value, Constraint $constraint)
4242
throw new UnexpectedValueException($value, 'string');
4343
}
4444

45-
if (!class_exists(Intl::class)) {
45+
if (!class_exists(Languages::class)) {
4646
throw new LogicException('The "symfony/intl" component is required to use the Language constraint.');
4747
}
4848

4949
$value = (string) $value;
50-
$languages = Intl::getLanguageBundle()->getLanguageNames();
5150

52-
if (!isset($languages[$value])) {
51+
if (!Languages::exists($value)) {
5352
$this->context->buildViolation($constraint->message)
5453
->setParameter('{{ value }}', $this->formatValue($value))
5554
->setCode(Language::NO_SUCH_LANGUAGE_ERROR)

Constraints/Locale.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace Symfony\Component\Validator\Constraints;
1313

14-
use Symfony\Component\Intl\Intl;
14+
use Symfony\Component\Intl\Locales;
1515
use Symfony\Component\Validator\Constraint;
1616
use Symfony\Component\Validator\Exception\LogicException;
1717

@@ -38,7 +38,7 @@ public function __construct($options = null)
3838
@trigger_error('The "canonicalize" option with value "false" is deprecated since Symfony 4.1, set it to "true" instead.', E_USER_DEPRECATED);
3939
}
4040

41-
if (!class_exists(Intl::class)) {
41+
if (!class_exists(Locales::class)) {
4242
// throw new LogicException(sprintf('The "symfony/intl" component is required to use the "%s" constraint.', __CLASS__));
4343
@trigger_error(sprintf('Using the "%s" constraint without the "symfony/intl" component installed is deprecated since Symfony 4.2.', __CLASS__), E_USER_DEPRECATED);
4444
}

Constraints/LocaleValidator.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Validator\Constraints;
1313

1414
use Symfony\Component\Intl\Intl;
15+
use Symfony\Component\Intl\Locales;
1516
use Symfony\Component\Validator\Constraint;
1617
use Symfony\Component\Validator\ConstraintValidator;
1718
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
@@ -46,10 +47,8 @@ public function validate($value, Constraint $constraint)
4647
if ($constraint->canonicalize) {
4748
$value = \Locale::canonicalize($value);
4849
}
49-
$localeBundle = Intl::getLocaleBundle();
50-
$locales = $localeBundle->getLocaleNames();
5150

52-
if (!isset($locales[$value]) && !\in_array($value, $localeBundle->getAliases(), true)) {
51+
if (!Locales::exists($value) && !\in_array($value, Locales::getAliases(), true)) {
5352
$this->context->buildViolation($constraint->message)
5453
->setParameter('{{ value }}', $this->formatValue($inputValue))
5554
->setCode(Locale::NO_SUCH_LOCALE_ERROR)

0 commit comments

Comments
 (0)