Validates whether a value is a valid currency
@author Miha Vrhovnik <miha.vrhovnik@pagein.si>
@api
Expanded class hierarchy of CurrencyValidator
class CurrencyValidator 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;
$currencies = Intl::getCurrencyBundle()
->getCurrencyNames();
if (!isset($currencies[$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 |
CurrencyValidator:: |
public | function |
Checks if the passed value is valid. Overrides ConstraintValidatorInterface:: |