Skip to content

Commit

Permalink
Support array of formatter currencies
Browse files Browse the repository at this point in the history
  • Loading branch information
MrHash committed Jun 16, 2020
1 parent 29ef41b commit 008f553
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/Service/MoneyServiceProvisioner.php
Expand Up @@ -16,6 +16,7 @@
use Money\Converter;
use Money\Currencies;
use Money\Currencies\AggregateCurrencies;
use Money\Currencies\CurrencyList;
use Money\Exchange;
use Money\Exchange\FixedExchange;
use Money\Exchange\ReversedCurrenciesExchange;
Expand All @@ -34,7 +35,7 @@ public function provision(
$factory = function (Injector $injector) use ($settings): object {
$currencies = $this->buildCurrencies($injector, $settings['currencies'] ?? []);
$parsers = $this->buildParsers($injector, $currencies, $settings['parsers'] ?? []);
$formatters = $this->buildFormatters($injector, $currencies, $settings['formatters'] ?? []);
$formatters = $this->buildFormatters($injector, $settings['formatters'] ?? []);
$exchanges = $this->buildExchanges($injector, $settings['exchanges'] ?? []);
return new MoneyService(
$parsers,
Expand All @@ -53,6 +54,7 @@ private function buildCurrencies(Injector $injector, array $currencyConfigs): Ag
{
$currencies = [];
foreach ($currencyConfigs as $currencyConfig) {
//@todo support plain array of currencies
$currencies[] = $injector->make($currencyConfig['class'], $currencyConfig['settings'] ?? []);
}
return new AggregateCurrencies($currencies);
Expand All @@ -70,15 +72,15 @@ private function buildParsers(
return new AggregateMoneyParser($parsers);
}

private function buildFormatters(
Injector $injector,
Currencies $currencies,
array $formatterConfigs = []
): AggregateMoneyFormatter {
private function buildFormatters(Injector $injector, array $formatterConfigs = []): AggregateMoneyFormatter
{
$formatters = [];
foreach ($formatterConfigs as $formatterConfig) {
$currencies = $injector->make($formatterConfig['currencies']);
//@todo support array of currencies
if (is_array($formatterConfig['currencies'])) {
$currencies = new CurrencyList($formatterConfig['currencies']);
} else {
$currencies = $injector->make($formatterConfig['currencies']);
}
foreach ($currencies as $currency) {
$formatters[(string)$currency] = $injector->make(
$formatterConfig['class'],
Expand All @@ -91,6 +93,7 @@ private function buildFormatters(

private function buildExchanges(Injector $injector, array $exchangeConfigs = []): Exchange
{
//@todo more exchange service support
return new ReversedCurrenciesExchange(new FixedExchange($exchangeConfigs['fixed_rate'] ?? []));
}
}

0 comments on commit 008f553

Please sign in to comment.