/**
  * Implements #element_validate for the rounding step element.
  */
 public function validateRoundingStep(array $element, FormStateInterface $form_state, array $form)
 {
     $rounding_step = $this->inputParser->parseAmount($element['#value']);
     if ($rounding_step === FALSE) {
         $form_state->setError($element, $this->t('The rounding step is not numeric.'));
     }
     $form_state->setValueForElement($element, $rounding_step);
 }
 /**
  * Implements preg_replace_callback() callback.
  *
  * @see self::process()
  */
 function processCallback(array $matches)
 {
     $currency_code = $matches[1];
     $amount = $this->input->parseAmount($matches[2]);
     // The amount is invalid, so return the token.
     if (!$amount) {
         return $matches[0];
     }
     /** @var \Drupal\currency\Entity\CurrencyInterface $currency */
     $currency = $this->currencyStorage->load($currency_code);
     $this->currentFilterProcessResult->addCacheableDependency($currency);
     if ($currency) {
         return $currency->formatAmount($amount);
     }
     // The currency code is invalid, so return the token.
     return $matches[0];
 }
 /**
  * Implements preg_replace_callback() callback.
  *
  * @see self::process()
  */
 function processCallback(array $matches)
 {
     $currency_code_from = $matches[1];
     $currency_code_to = $matches[2];
     $amount = str_replace(':', '', $matches[3]);
     if (strlen($amount) !== 0) {
         $amount = $this->input->parseAmount($amount);
         // The amount is invalid, so return the token.
         if (!$amount) {
             return $matches[0];
         }
     } else {
         $amount = 1;
     }
     $exchange_rate = $this->exchangeRateProvider->load($currency_code_from, $currency_code_to);
     $this->currentFilterProcessResult->addCacheableDependency($exchange_rate);
     if ($exchange_rate) {
         return bcmul($amount, $exchange_rate->getRate(), 6);
     }
     // No exchange rate could be loaded, so return the token.
     return $matches[0];
 }
 /**
  * Implements form #element_validate callback.
  */
 public function elementValidate($element, FormStateInterface $form_state, array $form)
 {
     $values = $form_state->getValues();
     $value = NestedArray::getValue($values, $element['#parents']);
     $amount = $value['amount'];
     $currency_code = $value['currency_code'];
     // Confirm that the amount is numeric.
     $amount = $this->input->parseAmount($amount);
     if ($amount === FALSE) {
         $form_state->setError($element['amount'], $this->t('%title is not numeric.', ['%title' => $element['#title']]));
     }
     // Confirm the amount lies within the allowed range.
     /** @var \Drupal\currency\Entity\CurrencyInterface $currency */
     $currency = $this->currencyStorage->load($currency_code);
     if ($element['#minimum_amount'] !== FALSE && bccomp($element['#minimum_amount'], $amount, 6) > 0) {
         $form_state->setError($element['amount'], $this->t('The minimum amount is @amount.', ['@amount' => $currency->formatAmount($element['#minimum_amount'])]));
     } elseif ($element['#maximum_amount'] !== FALSE && bccomp($amount, $element['#maximum_amount'], 6) > 0) {
         $form_state->setError($element['amount'], $this->t('The maximum amount is @amount.', ['@amount' => $currency->formatAmount($element['#maximum_amount'])]));
     }
     // The amount in $form_state is a human-readable, optionally localized
     // string, which cannot be used by other code. $amount is a numeric string
     // after running it through \Drupal::service('currency.input')->parseAmount().
     $form_state->setValueForElement($element, ['amount' => $amount, 'currency_code' => $currency_code]);
 }