add alpha3 option to Language constraint
This commit is contained in:
parent
b3b368b800
commit
ce73b98e2c
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
{
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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);
|
||||
|
|
Reference in New Issue