/**
  * @covers ::process
  * @covers ::processCallback
  */
 public function testProcess()
 {
     $cache_contexts = Cache::mergeContexts(['baz', 'qux']);
     $cache_tags = Cache::mergeTags(['foo', 'bar']);
     $currency_code_from = 'EUR';
     $currency_code_to = 'NLG';
     $rate = '2.20371';
     $exchange_rate_provider_id = 'foo_bar';
     $exchange_rate = new ExchangeRate($currency_code_from, $currency_code_to, $rate, $exchange_rate_provider_id);
     $exchange_rate->addCacheContexts($cache_contexts);
     $exchange_rate->addCacheTags($cache_tags);
     $this->input->expects($this->any())->method('parseAmount')->will($this->returnArgument(0));
     $this->exchangeRateProvider->expects($this->any())->method('load')->with($currency_code_from, $currency_code_to)->willReturn($exchange_rate);
     $langcode = $this->randomMachineName(2);
     $tokens_valid = ['[currency:EUR:NLG]' => '2.20371', '[currency:EUR:NLG:1]' => '2.20371', '[currency:EUR:NLG:2]' => '4.40742'];
     $tokens_invalid = ['[currency]', '[currency:]', '[currency::]', '[currency:EUR]', '[currency:EUR:123]', '[currency:123:EUR]', '[currency:123]'];
     foreach ($tokens_valid as $token => $replacement) {
         $result = $this->sut->process($token, $langcode);
         $this->assertInstanceOf(FilterProcessResult::class, $result);
         $this->assertSame($replacement, $result->getProcessedText());
         $this->assertSame($cache_contexts, $result->getCacheContexts());
         $this->assertSame($cache_tags, $result->getCacheTags());
     }
     foreach ($tokens_invalid as $token) {
         $result = $this->sut->process($token, $langcode);
         $this->assertInstanceOf(FilterProcessResult::class, $result);
         $this->assertSame($token, $result->getProcessedText());
         $this->assertEmpty($result->getCacheContexts());
         $this->assertEmpty($result->getCacheTags());
     }
 }
 /**
  * @covers ::process
  * @covers ::processCallback
  */
 function testProcess()
 {
     $cache_contexts = Cache::mergeContexts(['baz', 'qux']);
     $cache_tags = Cache::mergeTags(['foo', 'bar']);
     $map = [['100', TRUE, LanguageInterface::TYPE_CONTENT, '€100.00'], ['100.7654', TRUE, LanguageInterface::TYPE_CONTENT, '€100.77'], ['1.99', TRUE, LanguageInterface::TYPE_CONTENT, '€1.99'], ['2.99', TRUE, LanguageInterface::TYPE_CONTENT, '€2.99']];
     $currency = $this->getMock(CurrencyInterface::class);
     $currency->expects($this->any())->method('formatAmount')->willReturnMap($map);
     $currency->expects($this->atLeastOnce())->method('getCacheContexts')->willReturn($cache_contexts);
     $currency->expects($this->atLeastOnce())->method('getCacheTags')->willReturn($cache_tags);
     $this->currencyStorage->expects($this->any())->method('load')->with('EUR')->willReturn($currency);
     $this->input->expects($this->any())->method('parseAmount')->will($this->returnArgument(0));
     $langcode = $this->randomMachineName(2);
     $tokens_valid = ['[currency-localize:EUR:100]' => '€100.00', '[currency-localize:EUR:100.7654]' => '€100.77', '[currency-localize:EUR:1.99]' => '€1.99', '[currency-localize:EUR:2.99]' => '€2.99'];
     $tokens_invalid = ['[currency-localize]', '[currency-localize:]', '[currency-localize::]', '[currency-localize:EUR]', '[currency-localize:123:456]', '[currency-localize:123]'];
     foreach ($tokens_valid as $token => $replacement) {
         $result = $this->sut->process($token, $langcode);
         $this->assertInstanceOf(FilterProcessResult::class, $result);
         $this->assertSame($replacement, $result->getProcessedText());
         $this->assertSame($cache_contexts, $result->getCacheContexts());
         $this->assertSame($cache_tags, $result->getCacheTags());
     }
     foreach ($tokens_invalid as $token) {
         $result = $this->sut->process($token, $langcode);
         $this->assertInstanceOf(FilterProcessResult::class, $result);
         $this->assertSame($token, $result->getProcessedText());
         $this->assertEmpty($result->getCacheContexts());
         $this->assertEmpty($result->getCacheTags());
     }
 }
 /**
  * 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);
 }
 /**
  * @covers ::validateRoundingStep
  * @dataProvider providerTestValidateRoundingStep
  */
 public function testValidateRoundingStep($valid, $input_value, $parsed_value)
 {
     $element = array('#value' => $input_value);
     $form = array();
     $form_state = $this->getMock(FormStateInterface::class);
     $this->inputParser->expects($this->once())->method('parseAmount')->with($input_value)->willReturn($parsed_value);
     if (!$valid) {
         $form_state->expects($this->once())->method('setError')->with($element, 'The rounding step is not numeric.');
     } else {
         $form_state->expects($this->never())->method('setError');
         $form_state->expects($this->never())->method('setErrorByName');
     }
     $form_state->expects($this->once())->method('setValueForElement')->with($element, $parsed_value);
     $this->sut->validateRoundingStep($element, $form_state, $form);
 }
 /**
  * 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]);
 }