[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:
parent
4e97b97215
commit
39dfa3d724
@ -28,4 +28,5 @@ class Locale extends Constraint
|
|||||||
);
|
);
|
||||||
|
|
||||||
public $message = 'This value is not a valid locale.';
|
public $message = 'This value is not a valid locale.';
|
||||||
|
public $canonicalize = false;
|
||||||
}
|
}
|
||||||
|
@ -41,6 +41,9 @@ class LocaleValidator extends ConstraintValidator
|
|||||||
}
|
}
|
||||||
|
|
||||||
$value = (string) $value;
|
$value = (string) $value;
|
||||||
|
if ($constraint->canonicalize) {
|
||||||
|
$value = \Locale::canonicalize($value);
|
||||||
|
}
|
||||||
$locales = Intl::getLocaleBundle()->getLocaleNames();
|
$locales = Intl::getLocaleBundle()->getLocaleNames();
|
||||||
$aliases = Intl::getLocaleBundle()->getAliases();
|
$aliases = Intl::getLocaleBundle()->getAliases();
|
||||||
|
|
||||||
|
@ -90,4 +90,45 @@ class LocaleValidatorTest extends ConstraintValidatorTestCase
|
|||||||
array('foobar'),
|
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