Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allows custom Currency Provider through inheritance #94

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions crypto-currency-provider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

use Brick\Money\Exception\UnknownCurrencyException;

require_once __DIR__ . '/vendor/autoload.php';

class CryptoCurrencyProvider implements \Brick\Money\CurrencyProviderInterface
{

public function getCurrency(string $currencyCode): \Brick\Money\Currency
{
$cryptoCurrency = [
'BTC' => new \Brick\Money\Currency('BTC', 0, 'Bitcoin', 8 ),
];

return $cryptoCurrency[$currencyCode] ?? throw UnknownCurrencyException::unknownCurrency($currencyCode);
}

public function getCurrencyForCountry(string $currencyCode): \Brick\Money\Currency
{
throw new UnknownCurrencyException('Crypto Currency don\'t have a country code.');
}
}

class CryptoCurrency extends \Brick\Money\Currency {
protected static function getCurrencyProvider(): \Brick\Money\CurrencyProviderInterface
{
return new CryptoCurrencyProvider();
}
}

\Brick\Money\Money::of(2, CryptoCurrency::of('BTC'));
\Brick\Money\Money::of(2, \Brick\Money\Currency::of('EUR'));

class CryptoMoney extends \Brick\Money\Money {
protected static function getCurrencyProvider(): \Brick\Money\CurrencyProviderInterface
{
return new CryptoCurrencyProvider();
}
}

CryptoMoney::of(2, 'BTC')->plus(CryptoMoney::of(2, 'BTC'));
CryptoMoney::of(2, 'BTC')->plus(\Brick\Money\Money::of(2, \Brick\Money\Currency::of('EUR')));
11 changes: 8 additions & 3 deletions src/Currency.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
/**
* A currency. This class is immutable.
*/
final class Currency implements Stringable, JsonSerializable
class Currency implements Stringable, JsonSerializable
{
/**
* The currency code.
Expand Down Expand Up @@ -71,6 +71,11 @@
$this->defaultFractionDigits = $defaultFractionDigits;
}

protected static function getCurrencyProvider() : CurrencyProviderInterface
{
return ISOCurrencyProvider::getInstance();
}

/**
* Returns a Currency instance matching the given ISO currency code.
*
Expand All @@ -80,7 +85,7 @@
*/
public static function of(string|int $currencyCode) : Currency
{
return ISOCurrencyProvider::getInstance()->getCurrency($currencyCode);
return static::getCurrencyProvider()->getCurrency($currencyCode);

Check failure on line 88 in src/Currency.php

View workflow job for this annotation

GitHub Actions / Psalm

PossiblyInvalidArgument

src/Currency.php:88:59: PossiblyInvalidArgument: Argument 1 of Brick\Money\CurrencyProviderInterface::getCurrency expects string, but possibly different type int|string provided (see https://psalm.dev/092)
}

/**
Expand All @@ -94,7 +99,7 @@
*/
public static function ofCountry(string $countryCode) : Currency
{
return ISOCurrencyProvider::getInstance()->getCurrencyForCountry($countryCode);
return static::getCurrencyProvider()->getCurrencyForCountry($countryCode);
}

/**
Expand Down
18 changes: 18 additions & 0 deletions src/CurrencyProviderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Brick\Money;

use Brick\Money\Exception\UnknownCurrencyException;

interface CurrencyProviderInterface
{
/**
* @throws UnknownCurrencyException If an unknown currency code is given.
*/
public function getCurrency(string $currencyCode): Currency;

/**
* @throws UnknownCurrencyException If the country code is unknown, or there is no single currency for the country.
*/
public function getCurrencyForCountry(string $currencyCode): Currency;
}
2 changes: 1 addition & 1 deletion src/ISOCurrencyProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
/**
* Provides ISO 4217 currencies.
*/
final class ISOCurrencyProvider
final class ISOCurrencyProvider implements CurrencyProviderInterface
{
private static ?ISOCurrencyProvider $instance = null;

Expand Down Expand Up @@ -147,7 +147,7 @@
*
* @throws UnknownCurrencyException If the country code is not known, or the country has no single currency.
*/
public function getCurrencyForCountry(string $countryCode) : Currency

Check failure on line 150 in src/ISOCurrencyProvider.php

View workflow job for this annotation

GitHub Actions / Psalm

ParamNameMismatch

src/ISOCurrencyProvider.php:150:50: ParamNameMismatch: Argument 1 of Brick\Money\ISOCurrencyProvider::getCurrencyForCountry has wrong name $countryCode, expecting $currencyCode as defined by Brick\Money\CurrencyProviderInterface::getCurrencyForCountry (see https://psalm.dev/230)
{
$currencies = $this->getCurrenciesForCountry($countryCode);

Expand Down
17 changes: 11 additions & 6 deletions src/Money.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
* - CustomContext handles monies with a custom scale, and optionally step.
* - AutoContext automatically adjusts the scale of the money to the minimum required.
*/
final class Money extends AbstractMoney
class Money extends AbstractMoney
{
/**
* The amount.
Expand Down Expand Up @@ -181,7 +181,7 @@
RoundingMode $roundingMode = RoundingMode::UNNECESSARY,
) : Money {
if (! $currency instanceof Currency) {
$currency = Currency::of($currency);
$currency = static::getCurrencyProvider()->getCurrency($currency);

Check failure on line 184 in src/Money.php

View workflow job for this annotation

GitHub Actions / Psalm

PossiblyInvalidArgument

src/Money.php:184:68: PossiblyInvalidArgument: Argument 1 of Brick\Money\CurrencyProviderInterface::getCurrency expects string, but possibly different type int|string provided (see https://psalm.dev/092)
}

if ($context === null) {
Expand All @@ -193,6 +193,11 @@
return self::create($amount, $currency, $context, $roundingMode);
}

protected static function getCurrencyProvider() : CurrencyProviderInterface
{
return ISOCurrencyProvider::getInstance();
}

/**
* Returns a Money from a number of minor units.
*
Expand All @@ -219,7 +224,7 @@
RoundingMode $roundingMode = RoundingMode::UNNECESSARY,
) : Money {
if (! $currency instanceof Currency) {
$currency = Currency::of($currency);
$currency = static::getCurrencyProvider()->getCurrency($currency);

Check failure on line 227 in src/Money.php

View workflow job for this annotation

GitHub Actions / Psalm

PossiblyInvalidArgument

src/Money.php:227:68: PossiblyInvalidArgument: Argument 1 of Brick\Money\CurrencyProviderInterface::getCurrency expects string, but possibly different type int|string provided (see https://psalm.dev/092)
}

if ($context === null) {
Expand All @@ -245,7 +250,7 @@
public static function zero(Currency|string|int $currency, ?Context $context = null) : Money
{
if (! $currency instanceof Currency) {
$currency = Currency::of($currency);
$currency = static::getCurrencyProvider()->getCurrency($currency);

Check failure on line 253 in src/Money.php

View workflow job for this annotation

GitHub Actions / Psalm

PossiblyInvalidArgument

src/Money.php:253:68: PossiblyInvalidArgument: Argument 1 of Brick\Money\CurrencyProviderInterface::getCurrency expects string, but possibly different type int|string provided (see https://psalm.dev/092)
}

if ($context === null) {
Expand Down Expand Up @@ -729,7 +734,7 @@
RoundingMode $roundingMode = RoundingMode::UNNECESSARY,
) : Money {
if (! $currency instanceof Currency) {
$currency = Currency::of($currency);
$currency = static::getCurrencyProvider()->getCurrency($currency);

Check failure on line 737 in src/Money.php

View workflow job for this annotation

GitHub Actions / Psalm

PossiblyInvalidArgument

src/Money.php:737:68: PossiblyInvalidArgument: Argument 1 of Brick\Money\CurrencyProviderInterface::getCurrency expects string, but possibly different type int|string provided (see https://psalm.dev/092)
}

if ($context === null) {
Expand Down Expand Up @@ -836,4 +841,4 @@
throw MoneyMismatchException::contextMismatch($method);
}
}
}
}
Loading