Validates whether a value is a valid language code
@author Bernhard Schussek <bschussek@gmail.com>
@api
Expanded class hierarchy of LanguageValidator
class LanguageValidator extends ConstraintValidator {
/**
* {@inheritDoc}
*/
public function validate($value, Constraint $constraint) {
if (null === $value || '' === $value) {
return;
}
if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
throw new UnexpectedTypeException($value, 'string');
}
$value = (string) $value;
$languages = Intl::getLanguageBundle()
->getLanguageNames();
if (!isset($languages[$value])) {
$this->context
->addViolation($constraint->message, array(
'{{ value }}' => $value,
));
}
}
}
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ConstraintValidator:: |
protected | property | ||
ConstraintValidator:: |
public | function |
Initializes the constraint validator. Overrides ConstraintValidatorInterface:: |
1 |
LanguageValidator:: |
public | function |
Checks if the passed value is valid. Overrides ConstraintValidatorInterface:: |