public function CurrencyConstraintValidator::validate in Commerce Core 8.2
File
- modules/
price/ src/ Plugin/ Validation/ Constraint/ CurrencyConstraintValidator.php, line 45
Class
- CurrencyConstraintValidator
- Validates the currency constraint.
Namespace
Drupal\commerce_price\Plugin\Validation\ConstraintCode
public function validate($value, Constraint $constraint) {
if (!$value instanceof PriceItem) {
throw new UnexpectedTypeException($value, PriceItem::class);
}
$price_item = $value;
$currency_code = $price_item
->get('currency_code')
->getValue();
if ($currency_code === NULL || $currency_code === '') {
return;
}
$currencies = $this->entityTypeManager
->getStorage('commerce_currency')
->loadMultiple();
if (!isset($currencies[$currency_code])) {
$this->context
->buildViolation($constraint->invalidMessage)
->atPath('currency_code')
->setParameter('%value', $this
->formatValue($currency_code))
->addViolation();
return;
}
$available_currencies = $constraint->availableCurrencies;
if (!empty($available_currencies) && !in_array($currency_code, $available_currencies)) {
$this->context
->buildViolation($constraint->notAvailableMessage)
->atPath('currency_code')
->setParameter('%value', $this
->formatValue($currency_code))
->addViolation();
}
}