[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:
Javier Spagnoletti 2017-04-09 17:48:10 -03:00 committed by Fabien Potencier
parent 4e97b97215
commit 39dfa3d724
3 changed files with 45 additions and 0 deletions

View File

@ -28,4 +28,5 @@ class Locale extends Constraint
);
public $message = 'This value is not a valid locale.';
public $canonicalize = false;
}

View File

@ -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();

View File

@ -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'),
);
}
}