Skip to content

Commit

Permalink
Formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardogobbosouza committed Apr 25, 2024
1 parent 2a222f3 commit bb16b12
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 65 deletions.
2 changes: 1 addition & 1 deletion src/Casts/MoneyCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ abstract class MoneyCast implements CastsAttributes
*
* @param mixed $forceDecimals
*/
public function __construct(string $currency = null, $forceDecimals = null)
public function __construct(?string $currency = null, $forceDecimals = null)
{
$this->currency = $currency;
$this->forceDecimals = is_string($forceDecimals)
Expand Down
2 changes: 1 addition & 1 deletion src/Formatters/CurrencySymbolMoneyFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class CurrencySymbolMoneyFormatter implements MoneyFormatter
* @param bool $right
* @param string|null $locale
*/
public function __construct($right = false, $locale = null, Currencies $currencies = null)
public function __construct($right = false, $locale = null, ?Currencies $currencies = null)
{
$this->right = $right;
$this->locale = $locale ?: Money::getLocale();
Expand Down
48 changes: 5 additions & 43 deletions src/Money.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Illuminate\Contracts\Support\Jsonable;
use Illuminate\Support\Traits\Macroable;
use JsonSerializable;
use ReflectionMethod;

/**
* Money.
Expand Down Expand Up @@ -41,16 +40,16 @@ class Money implements Arrayable, Jsonable, JsonSerializable
{
use CurrenciesTrait;
use LocaleTrait;
use Macroable {
Macroable::__call as macroCall;
Macroable::__callStatic as macroCallStatic;
}
use MoneyFactory {
MoneyFactory::__callStatic as factoryCallStatic;
}
use MoneyFormatterTrait;
use MoneySerializerTrait;
use MoneyParserTrait;
use Macroable {
Macroable::__call as macroCall;
Macroable::__callStatic as macroCallStatic;
}
use MoneySerializerTrait;

/**
* @var \Money\Money
Expand Down Expand Up @@ -281,41 +280,4 @@ private static function convertResult($result)

return $results;
}

/**
* Resolve calculator.
*
* @return \Money\Calculator
*
* @throws \RuntimeException
*/
private static function resolveCalculator()
{
$reflection = new ReflectionMethod(\Money\Money::class, 'getCalculator');

if ($reflection->isPublic()) {
$calculator = call_user_func([\Money\Money::class, 'getCalculator']);

return new $calculator();
}

$calculators = [
\Money\Calculator\BcMathCalculator::class,
\Money\Calculator\GmpCalculator::class,
\Money\Calculator\PhpCalculator::class,
];

foreach ($calculators as $calculator) {
if (! class_exists($calculator)) {
continue;
}

/** @var Calculator $calculator */
if ($calculator::supported()) {
return new $calculator();
}
}

throw new \RuntimeException('Cannot find calculator for money calculations');
}
}
13 changes: 6 additions & 7 deletions src/MoneyFormatterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ trait MoneyFormatterTrait
*
* @throws \InvalidArgumentException
*/
public function format($locale = null, Currencies $currencies = null, $style = NumberFormatter::CURRENCY)
public function format($locale = null, ?Currencies $currencies = null, $style = NumberFormatter::CURRENCY)
{
$defaultFormatter = config('money.defaultFormatter');

Expand Down Expand Up @@ -69,7 +69,7 @@ public function formatByAggregate(array $formatters)
* @param int $fractionDigits
* @return string
*/
public function formatByBitcoin($fractionDigits = 2, Currencies $currencies = null)
public function formatByBitcoin($fractionDigits = 2, ?Currencies $currencies = null)
{
$formatter = new BitcoinMoneyFormatter($fractionDigits, $currencies ?: new BitcoinCurrencies());

Expand All @@ -81,10 +81,9 @@ public function formatByBitcoin($fractionDigits = 2, Currencies $currencies = nu
*
* @param bool $right
* @param string|null $locale
* @param \Money\Currencies $currencies
* @return string
*/
public function formatByCurrencySymbol($right = false, $locale = null, Currencies $currencies = null)
public function formatByCurrencySymbol($right = false, $locale = null, ?Currencies $currencies = null)
{
$formatter = new CurrencySymbolMoneyFormatter($right, $locale ?: static::getLocale(), $currencies ?: static::getCurrencies());

Expand All @@ -96,7 +95,7 @@ public function formatByCurrencySymbol($right = false, $locale = null, Currencie
*
* @return string
*/
public function formatByDecimal(Currencies $currencies = null)
public function formatByDecimal(?Currencies $currencies = null)
{
$formatter = new DecimalMoneyFormatter($currencies ?: static::getCurrencies());

Expand All @@ -110,7 +109,7 @@ public function formatByDecimal(Currencies $currencies = null)
* @param int $style
* @return string
*/
public function formatByIntl($locale = null, Currencies $currencies = null, $style = NumberFormatter::CURRENCY)
public function formatByIntl($locale = null, ?Currencies $currencies = null, $style = NumberFormatter::CURRENCY)
{
$numberFormatter = new NumberFormatter($locale ?: static::getLocale(), $style);
$formatter = new IntlMoneyFormatter($numberFormatter, $currencies ?: static::getCurrencies());
Expand All @@ -127,7 +126,7 @@ public function formatByIntl($locale = null, Currencies $currencies = null, $sty
*/
public function formatByIntlLocalizedDecimal(
$locale = null,
Currencies $currencies = null,
?Currencies $currencies = null,
$style = NumberFormatter::CURRENCY
) {
$numberFormatter = new NumberFormatter($locale ?: static::getLocale(), $style);
Expand Down
6 changes: 3 additions & 3 deletions src/MoneyParserTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public static function parseByBitcoin(
public static function parseByDecimal(
$money,
$fallbackCurrency = null,
Currencies $currencies = null,
?Currencies $currencies = null,
$convert = true
) {
$parser = new DecimalMoneyParser($currencies ?: static::getCurrencies());
Expand All @@ -178,7 +178,7 @@ public static function parseByIntl(
$money,
$fallbackCurrency = null,
$locale = null,
Currencies $currencies = null,
?Currencies $currencies = null,
$style = null,
$convert = true
) {
Expand Down Expand Up @@ -206,7 +206,7 @@ public static function parseByIntlLocalizedDecimal(
$money,
$fallbackCurrency = null,
$locale = null,
Currencies $currencies = null,
?Currencies $currencies = null,
$style = null,
$convert = true
) {
Expand Down
1 change: 0 additions & 1 deletion src/MoneySerializerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ public function serializeByArray()
/**
* Serialize by serializer.
*
* @param \Cknow\Money\MoneySerializer $serializer
* @return mixed
*/
public function serializeBySerializer(MoneySerializer $serializer)
Expand Down
9 changes: 3 additions & 6 deletions src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,13 @@ function money_parse_by_bitcoin(
*
* @param string $money
* @param \Money\Currency|string|null $fallbackCurrency
* @param \Money\Currencies|null $currencies
* @param bool $convert
* @return \Cknow\Money\Money|\Money\Money
*/
function money_parse_by_decimal(
$money,
$fallbackCurrency = null,
Money\Currencies $currencies = null,
?Money\Currencies $currencies = null,
$convert = true
) {
return Cknow\Money\Money::parseByDecimal(
Expand All @@ -177,7 +176,6 @@ function money_parse_by_decimal(
* @param string $money
* @param \Money\Currency|string|null $fallbackCurrency
* @param string|null $locale
* @param \Money\Currencies|null $currencies
* @param int|null $style
* @param bool $convert
* @return \Cknow\Money\Money|\Money\Money
Expand All @@ -186,7 +184,7 @@ function money_parse_by_intl(
$money,
$fallbackCurrency = null,
$locale = null,
Money\Currencies $currencies = null,
?Money\Currencies $currencies = null,
$style = null,
$convert = true
) {
Expand All @@ -208,7 +206,6 @@ function money_parse_by_intl(
* @param string $money
* @param \Money\Currency|string|null $fallbackCurrency
* @param string|null $locale
* @param \Money\Currencies|null $currencies
* @param int|null $style
* @param bool $convert
* @return \Cknow\Money\Money|\Money\Money
Expand All @@ -217,7 +214,7 @@ function money_parse_by_intl_localized_decimal(
$money,
$fallbackCurrency = null,
$locale = null,
Money\Currencies $currencies = null,
?Money\Currencies $currencies = null,
$style = null,
$convert = true
) {
Expand Down
3 changes: 0 additions & 3 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ abstract class TestCase extends AbstractPackageTestCase
* Setup the application environment.
*
* @param \Illuminate\Contracts\Foundation\Application $app
* @return void
*/
protected function getEnvironmentSetUp($app): void
{
Expand All @@ -22,8 +21,6 @@ protected function getEnvironmentSetUp($app): void

/**
* Get the service provider class.
*
* @return string
*/
protected static function getServiceProviderClass(): string
{
Expand Down

0 comments on commit bb16b12

Please sign in to comment.