feature #35848 [Validator] add alpha3 option to Language constraint (xabbuh)

This PR was merged into the 5.1-dev branch.

Discussion
----------

[Validator] add alpha3 option to Language constraint

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       | Fix https://github.com/symfony/symfony/pull/35116#issuecomment-570038901
| License       | MIT
| Doc PR        |

Commits
-------

ce73b98e2c add alpha3 option to Language constraint
This commit is contained in:
Fabien Potencier 2020-02-24 17:28:00 +01:00
commit b5e6582398
4 changed files with 52 additions and 2 deletions

View File

@ -5,7 +5,7 @@ CHANGELOG
-----
* added the `Hostname` constraint and validator
* added option `alpha3` to `Country` constraint
* added the `alpha3` option to the `Country` and `Language` constraints
* allow to define a reusable set of constraints by extending the `Compound` constraint
* added `Sequentially` constraint, to sequentially validate a set of constraints (any violation raised will prevent further validation of the nested constraints)
* added the `divisibleBy` option to the `Count` constraint

View File

@ -30,6 +30,7 @@ class Language extends Constraint
];
public $message = 'This value is not a valid language.';
public $alpha3 = false;
public function __construct($options = null)
{

View File

@ -43,7 +43,7 @@ class LanguageValidator extends ConstraintValidator
$value = (string) $value;
if (!Languages::exists($value)) {
if ($constraint->alpha3 ? !Languages::alpha3CodeExists($value) : !Languages::exists($value)) {
$this->context->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value))
->setCode(Language::NO_SUCH_LANGUAGE_ERROR)

View File

@ -102,6 +102,55 @@ class LanguageValidatorTest extends ConstraintValidatorTestCase
];
}
/**
* @dataProvider getValidAlpha3Languages
*/
public function testValidAlpha3Languages($language)
{
$this->validator->validate($language, new Language([
'alpha3' => true,
]));
$this->assertNoViolation();
}
public function getValidAlpha3Languages()
{
return [
['deu'],
['eng'],
['fra'],
];
}
/**
* @dataProvider getInvalidAlpha3Languages
*/
public function testInvalidAlpha3Languages($language)
{
$constraint = new Language([
'alpha3' => true,
'message' => 'myMessage',
]);
$this->validator->validate($language, $constraint);
$this->buildViolation('myMessage')
->setParameter('{{ value }}', '"'.$language.'"')
->setCode(Language::NO_SUCH_LANGUAGE_ERROR)
->assertRaised();
}
public function getInvalidAlpha3Languages()
{
return [
['foobar'],
['en'],
['ZZZ'],
['zzz'],
];
}
public function testValidateUsingCountrySpecificLocale()
{
IntlTestHelper::requireFullIntl($this, false);