diff --git a/CHANGELOG.md b/CHANGELOG.md index b92b228..e070d96 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ - Minimum PHP version is now 7.4 - `AbstractMoney::getAmount()` now has a return type +- `CurrencyConverter` constructor does not accept a default `$context` anymore +- `CurrencyConverter::convert()` now requires the `$context` previously accepted by the constructor as third parameter ✨ **New ISO currencies** diff --git a/src/CurrencyConverter.php b/src/CurrencyConverter.php index 05bfec0..3b328cb 100644 --- a/src/CurrencyConverter.php +++ b/src/CurrencyConverter.php @@ -13,10 +13,6 @@ /** * Converts monies into different currencies, using an exchange rate provider. - * - * @todo Now that this class provides methods to convert to both Money and RationalMoney, it makes little sense to - * provide the context in the constructor, as this only applies to convert() and not convertToRational(). - * This should probably be a parameter to convert(). */ final class CurrencyConverter { @@ -25,20 +21,12 @@ final class CurrencyConverter */ private ExchangeRateProvider $exchangeRateProvider; - /** - * The context of the monies created by this currency converter. - */ - private Context $context; - /** * @param ExchangeRateProvider $exchangeRateProvider The exchange rate provider. - * @param Context|null $context A context to create the monies in, or null to use the default. - * The context only applies to convert(), not convertToRational(). */ - public function __construct(ExchangeRateProvider $exchangeRateProvider, ?Context $context = null) + public function __construct(ExchangeRateProvider $exchangeRateProvider) { $this->exchangeRateProvider = $exchangeRateProvider; - $this->context = $context ?? new DefaultContext(); } /** @@ -48,6 +36,7 @@ public function __construct(ExchangeRateProvider $exchangeRateProvider, ?Context * * @param MoneyContainer $moneyContainer The Money, RationalMoney or MoneyBag to convert. * @param Currency|string|int $currency The Currency instance, ISO currency code or ISO numeric currency code. + * @param Context|null $context A context to create the money in, or null to use the default. * @param int $roundingMode The rounding mode, if necessary. * * @return Money @@ -55,9 +44,11 @@ public function __construct(ExchangeRateProvider $exchangeRateProvider, ?Context * @throws CurrencyConversionException If the exchange rate is not available. * @throws RoundingNecessaryException If rounding is necessary and RoundingMode::UNNECESSARY is used. */ - public function convert(MoneyContainer $moneyContainer, $currency, int $roundingMode = RoundingMode::UNNECESSARY) : Money + public function convert(MoneyContainer $moneyContainer, $currency, ?Context $context = null, int $roundingMode = RoundingMode::UNNECESSARY) : Money { - return $this->convertToRational($moneyContainer, $currency)->to($this->context, $roundingMode); + return $this + ->convertToRational($moneyContainer, $currency) + ->to($context ?? new DefaultContext(), $roundingMode); } /** diff --git a/tests/CurrencyConverterTest.php b/tests/CurrencyConverterTest.php index a314d34..2c3b912 100644 --- a/tests/CurrencyConverterTest.php +++ b/tests/CurrencyConverterTest.php @@ -50,7 +50,7 @@ public function testConvertMoney(array $money, string $toCurrency, int $rounding $this->expectException($expectedResult); } - $actualResult = $currencyConverter->convert($money, $toCurrency, $roundingMode); + $actualResult = $currencyConverter->convert($money, $toCurrency, null, $roundingMode); if (! $this->isExceptionClass($expectedResult)) { $this->assertMoneyIs($expectedResult, $actualResult); @@ -97,8 +97,8 @@ public function testConvertMoneyBag(array $monies, string $currency, Context $co $moneyBag->add($money); } - $currencyConverter = new CurrencyConverter($exchangeRateProvider, $context); - $this->assertMoneyIs($total, $currencyConverter->convert($moneyBag, $currency, $roundingMode)); + $currencyConverter = new CurrencyConverter($exchangeRateProvider); + $this->assertMoneyIs($total, $currencyConverter->convert($moneyBag, $currency, $context, $roundingMode)); } public function providerConvertMoneyBag() : array @@ -164,7 +164,7 @@ public function testConvertRationalMoney(array $money, string $toCurrency, int $ $this->expectException($expectedResult); } - $actualResult = $currencyConverter->convert($rationalMoney, $toCurrency, $roundingMode); + $actualResult = $currencyConverter->convert($rationalMoney, $toCurrency, null, $roundingMode); if (! $this->isExceptionClass($expectedResult)) { $this->assertMoneyIs($expectedResult, $actualResult);