feature #22353 [Validator] Add canonicalize
option for Locale
validator (phansys)
This PR was squashed before being merged into the 4.1-dev branch (closes #22353).
Discussion
----------
[Validator] Add `canonicalize` option for `Locale` validator
|Q |A |
|--- |--- |
|Branch |master|
|Bug fix? |no |
|New feature? |yes |
|BC breaks? |no |
|Deprecations?|no |
|Tests pass? |yes |
|Fixed tickets|n/a |
|License |MIT |
|Doc PR |n/a |
Allow non canonicalized locales ('fr-FR' by instance) to pass the validation.
Relates to symfony/symfony-docs#7660.
Commits
-------
39dfa3d724
[Validator] Add option for LANG="en_US.UTF-8" LC_COLLATE="en_US.UTF-8" LC_CTYPE="en_US.UTF-8" LC_MESSAGES="en_US.UTF-8" LC_MONETARY="en_US.UTF-8" LC_NUMERIC="en_US.UTF-8" LC_TIME="en_US.UTF-8" LC_ALL= validator
This commit is contained in:
commit
5f0c279227
@ -28,4 +28,5 @@ class Locale extends Constraint
|
||||
);
|
||||
|
||||
public $message = 'This value is not a valid locale.';
|
||||
public $canonicalize = false;
|
||||
}
|
||||
|
@ -41,6 +41,9 @@ class LocaleValidator extends ConstraintValidator
|
||||
}
|
||||
|
||||
$value = (string) $value;
|
||||
if ($constraint->canonicalize) {
|
||||
$value = \Locale::canonicalize($value);
|
||||
}
|
||||
$locales = Intl::getLocaleBundle()->getLocaleNames();
|
||||
$aliases = Intl::getLocaleBundle()->getAliases();
|
||||
|
||||
|
@ -90,4 +90,45 @@ class LocaleValidatorTest extends ConstraintValidatorTestCase
|
||||
array('foobar'),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider getUncanonicalizedLocales
|
||||
*/
|
||||
public function testInvalidLocalesWithoutCanonicalization(string $locale)
|
||||
{
|
||||
$constraint = new Locale(array(
|
||||
'message' => 'myMessage',
|
||||
));
|
||||
|
||||
$this->validator->validate($locale, $constraint);
|
||||
|
||||
$this->buildViolation('myMessage')
|
||||
->setParameter('{{ value }}', '"'.$locale.'"')
|
||||
->setCode(Locale::NO_SUCH_LOCALE_ERROR)
|
||||
->assertRaised();
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider getUncanonicalizedLocales
|
||||
*/
|
||||
public function testValidLocalesWithCanonicalization(string $locale)
|
||||
{
|
||||
$constraint = new Locale(array(
|
||||
'message' => 'myMessage',
|
||||
'canonicalize' => true,
|
||||
));
|
||||
|
||||
$this->validator->validate($locale, $constraint);
|
||||
|
||||
$this->assertNoViolation();
|
||||
}
|
||||
|
||||
public function getUncanonicalizedLocales(): iterable
|
||||
{
|
||||
return array(
|
||||
array('en-US'),
|
||||
array('es-AR'),
|
||||
array('fr_FR.utf8'),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user