class FixedRates in Currency 8.3
Provides manually entered exchange rates.
Plugin annotation
@CurrencyExchangeRateProvider(
id = "currency_fixed_rates",
label = @Translation("Fixed rates"),
operations_provider = "\Drupal\currency\Plugin\Currency\ExchangeRateProvider\FixedRatesOperationsProvider"
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\currency\Plugin\Currency\ExchangeRateProvider\FixedRates implements ContainerFactoryPluginInterface, ExchangeRateProviderInterface uses \Commercie\CurrencyExchange\FixedExchangeRateProviderTrait
Expanded class hierarchy of FixedRates
4 files declare their use of FixedRates
- FixedRatesFormTest.php in tests/
src/ Unit/ Controller/ FixedRatesFormTest.php - FixedRatesFormTest.php in tests/
src/ Unit/ Form/ FixedRatesFormTest.php - FixedRatesOverviewTest.php in tests/
src/ Unit/ Controller/ FixedRatesOverviewTest.php - FixedRatesTest.php in tests/
src/ Unit/ Plugin/ Currency/ ExchangeRateProvider/ FixedRatesTest.php
File
- src/
Plugin/ Currency/ ExchangeRateProvider/ FixedRates.php, line 20
Namespace
Drupal\currency\Plugin\Currency\ExchangeRateProviderView source
class FixedRates extends PluginBase implements ExchangeRateProviderInterface, ContainerFactoryPluginInterface {
use FixedExchangeRateProviderTrait;
/**
* The config factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* Constructs a new class instance
*
* @param mixed[] $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed[] $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory.
*/
public function __construct(array $configuration, $plugin_id, array $plugin_definition, ConfigFactoryInterface $config_factory) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->configFactory = $config_factory;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('config.factory'));
}
/**
* {@inheritdoc}
*/
public function loadAll() {
$rates_data = $this->configFactory
->get('currency.exchanger.fixed_rates')
->get('rates');
$rates = array();
foreach ($rates_data as $rate_data) {
$rates[$rate_data['currency_code_from']][$rate_data['currency_code_to']] = $rate_data['rate'];
}
return $rates;
}
/**
* Saves an exchange rate.
*
* @param string $currency_code_from
* @param string $currency_code_to
* @param string $rate
*
* @return $this
*/
public function save($currency_code_from, $currency_code_to, $rate) {
$config = $this->configFactory
->getEditable('currency.exchanger.fixed_rates');
$rates = $this
->loadAll();
$rates[$currency_code_from][$currency_code_to] = $rate;
// Massage the rates into a format that can be stored, as associative
// arrays are not supported by the config system
$rates_data = array();
foreach ($rates as $currency_code_from => $currency_code_from_rates) {
foreach ($currency_code_from_rates as $currency_code_to => $rate) {
$rates_data[] = array(
'currency_code_from' => $currency_code_from,
'currency_code_to' => $currency_code_to,
'rate' => $rate,
);
}
}
$config
->set('rates', $rates_data);
$config
->save();
return $this;
}
/**
* Deletes an exchange rate.
*
* @param string $currency_code_from
* @param string $currency_code_to
*
* @return NULL
*/
public function delete($currency_code_from, $currency_code_to) {
$config = $this->configFactory
->getEditable('currency.exchanger.fixed_rates');
$rates = $config
->get('rates');
foreach ($rates as $i => $rate) {
if ($rate['currency_code_from'] == $currency_code_from && $rate['currency_code_to'] == $currency_code_to) {
unset($rates[$i]);
}
}
$config
->set('rates', $rates);
$config
->save();
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ExchangeRateProviderInterface:: |
public | function | 1 | |
ExchangeRateProviderInterface:: |
public | function | 1 | |
FixedRates:: |
protected | property | The config factory. | |
FixedRates:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
|
FixedRates:: |
public | function | Deletes an exchange rate. | |
FixedRates:: |
public | function | ||
FixedRates:: |
public | function | Saves an exchange rate. | |
FixedRates:: |
public | function |
Constructs a new class instance Overrides PluginBase:: |
|
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. |