Skip to content
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 @@ public function __construct(string $currencyCode, int $numericCode, string $name
$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 function __construct(string $currencyCode, int $numericCode, string $name
*/
public static function of(string|int $currencyCode) : Currency
{
return ISOCurrencyProvider::getInstance()->getCurrency($currencyCode);
return static::getCurrencyProvider()->getCurrency($currencyCode);
}

/**
Expand All @@ -94,7 +99,7 @@ public static function of(string|int $currencyCode) : Currency
*/
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
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 @@ public static function of(
RoundingMode $roundingMode = RoundingMode::UNNECESSARY,
) : Money {
if (! $currency instanceof Currency) {
$currency = Currency::of($currency);
$currency = static::getCurrencyProvider()->getCurrency($currency);
}

if ($context === null) {
Expand All @@ -193,6 +193,11 @@ public static function of(
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 @@ public static function ofMinor(
RoundingMode $roundingMode = RoundingMode::UNNECESSARY,
) : Money {
if (! $currency instanceof Currency) {
$currency = Currency::of($currency);
$currency = static::getCurrencyProvider()->getCurrency($currency);
}

if ($context === null) {
Expand All @@ -245,7 +250,7 @@ public static function ofMinor(
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);
}

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

if ($context === null) {
Expand Down Expand Up @@ -836,4 +841,4 @@ protected function checkContext(Context $context, string $method) : void
throw MoneyMismatchException::contextMismatch($method);
}
}
}
}