Skip to content

Commit

Permalink
Merge branch 'main' into fix-remove-renderable
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardogobbosouza authored Mar 16, 2024
2 parents 87d6a06 + 82d5062 commit a7e4537
Show file tree
Hide file tree
Showing 24 changed files with 382 additions and 92 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ or add this to require section in your composer.json file:
"cknow/laravel-money": "^7.0"
```

then run ```composer update```
then run `composer update`

## Usage

Expand Down Expand Up @@ -210,7 +210,7 @@ $model->money = 10.23; // 10.23 USD or any other currency defined
$model->money = 'A$10'; // 10.00 AUD
$model->money = '1,000.23'; // 1000.23 USD or any other currency defined
$model->money = '10'; // 0.10 USD or any other currency defined
$model->money = Money::EUR(10); // 10 EUR
$model->money = Money::EUR(10); // 0.10 EUR
```

When we pass the model attribute holding the currency,
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@
"ext-intl": "*",
"illuminate/support": "^7.0|^8.0|^9.0|^10.0",
"illuminate/view": "^7.0|^8.0|^9.0|^10.0",
"moneyphp/money": "^3.3|^4.0.3"
"moneyphp/money": "^3.3|^4.2"
},
"require-dev": {
"graham-campbell/testbench": "^5.7",
"illuminate/filesystem": "^7.0|^8.0|^9.0|^10.0",
"mockery/mockery": "^1.5",
"mockery/mockery": "^1.6",
"phpunit/phpunit": "^8.5|^9.5.10|^10.0",
"spatie/phpunit-watcher": "^1.0"
"spatie/phpunit-watcher": "^1.23"
},
"autoload": {
"psr-4": {
Expand Down
1 change: 1 addition & 0 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
'locale' => config('app.locale', 'en_US'),
'defaultCurrency' => config('app.currency', 'USD'),
'defaultFormatter' => null,
'defaultSerializer' => null,
'isoCurrenciesPath' => __DIR__.'/../vendor/moneyphp/money/resources/currency.php',
'currencies' => [
'iso' => 'all',
Expand Down
6 changes: 0 additions & 6 deletions src/BladeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ class BladeExtension
{
/**
* Register.
*
* @param \Illuminate\View\Compilers\BladeCompiler $compiler
*/
public static function register(BladeCompiler $compiler)
{
Expand All @@ -27,8 +25,6 @@ public static function register(BladeCompiler $compiler)

/**
* Register aggregations.
*
* @param \Illuminate\View\Compilers\BladeCompiler $compiler
*/
private static function registerAggregations(BladeCompiler $compiler)
{
Expand All @@ -51,8 +47,6 @@ private static function registerAggregations(BladeCompiler $compiler)

/**
* Register parsers.
*
* @param \Illuminate\View\Compilers\BladeCompiler $compiler
*/
private static function registerParsers(BladeCompiler $compiler)
{
Expand Down
7 changes: 0 additions & 7 deletions src/Casts/MoneyCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ abstract class MoneyCast implements CastsAttributes
/**
* Instantiate the class.
*
* @param string|null $currency
* @param mixed $forceDecimals
*/
public function __construct(string $currency = null, $forceDecimals = null)
Expand All @@ -39,7 +38,6 @@ public function __construct(string $currency = null, $forceDecimals = null)
/**
* Get formatter.
*
* @param \Cknow\Money\Money $money
* @return string|float|int
*/
abstract protected function getFormatter(Money $money);
Expand All @@ -48,9 +46,7 @@ abstract protected function getFormatter(Money $money);
* Transform the attribute from the underlying model values.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @param string $key
* @param mixed $value
* @param array $attributes
* @return \Cknow\Money\Money|null
*/
public function get($model, string $key, $value, array $attributes)
Expand All @@ -66,9 +62,7 @@ public function get($model, string $key, $value, array $attributes)
* Transform the attribute to its underlying model values.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @param string $key
* @param mixed $value
* @param array $attributes
* @return array
*
* @throws \InvalidArgumentException
Expand Down Expand Up @@ -99,7 +93,6 @@ public function set($model, string $key, $value, array $attributes)
/**
* Get currency.
*
* @param array $attributes
* @return \Money\Currency
*/
protected function getCurrency(array $attributes)
Expand Down
1 change: 0 additions & 1 deletion src/Casts/MoneyDecimalCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ class MoneyDecimalCast extends MoneyCast
/**
* Get formatter.
*
* @param \Cknow\Money\Money $money
* @return float
*/
protected function getFormatter(Money $money)
Expand Down
1 change: 0 additions & 1 deletion src/Casts/MoneyIntegerCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ class MoneyIntegerCast extends MoneyCast
/**
* Get formatter.
*
* @param \Cknow\Money\Money $money
* @return int
*/
protected function getFormatter(Money $money)
Expand Down
1 change: 0 additions & 1 deletion src/Casts/MoneyStringCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ class MoneyStringCast extends MoneyCast
/**
* Get formatter.
*
* @param \Cknow\Money\Money $money
* @return string
*/
protected function getFormatter(Money $money)
Expand Down
1 change: 0 additions & 1 deletion src/CurrenciesTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,6 @@ private static function makeCurrencies($currenciesConfig)
* Make currencies list according to array for specified source.
*
* @param array|string $config
* @param \Money\Currencies $currencies
* @param string $sourceName
* @return \Money\Currencies
*
Expand Down
4 changes: 0 additions & 4 deletions src/Formatters/CurrencySymbolMoneyFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ class CurrencySymbolMoneyFormatter implements MoneyFormatter
*
* @param bool $right
* @param string|null $locale
* @param \Money\Currencies|null $currencies
*/
public function __construct($right = false, $locale = null, Currencies $currencies = null)
{
Expand All @@ -41,9 +40,6 @@ public function __construct($right = false, $locale = null, Currencies $currenci

/**
* Formats a Money object as string.
*
* @param \Money\Money $moeny
* @return string
*/
public function format(\Money\Money $money): string
{
Expand Down
62 changes: 28 additions & 34 deletions src/Money.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Cknow\Money;

use BadMethodCallException;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Contracts\Support\Jsonable;
use Illuminate\Support\Traits\Macroable;
Expand Down Expand Up @@ -44,6 +45,7 @@ class Money implements Arrayable, Jsonable, JsonSerializable
MoneyFactory::__callStatic as factoryCallStatic;
}
use MoneyFormatterTrait;
use MoneySerializerTrait;
use MoneyParserTrait;
use Macroable {
Macroable::__call as macroCall;
Expand Down Expand Up @@ -83,6 +85,16 @@ public function getMoney()
return $this->money;
}

/**
* Get attributes.
*
* @return array
*/
public function getAttributes()
{
return $this->attributes;
}

/**
* Divide.
*
Expand All @@ -92,17 +104,10 @@ public function getMoney()
*/
public function divide($divisor, $roundingMode = \Money\Money::ROUND_HALF_UP)
{
if (
is_int($divisor)
|| (filter_var($divisor, FILTER_VALIDATE_INT) !== false && ! is_float($divisor))
) {
return $this->__call('divide', [$divisor, $roundingMode]);
}

$money = $this->getMoney();
$calculator = static::resolveCalculator();

return new self((int) round($calculator->divide($money->getAmount(), $divisor), 0, $roundingMode), $money->getCurrency());
return $this->__call('divide', [
is_int($divisor) ? $divisor : strval($divisor),
$roundingMode,
]);
}

/**
Expand All @@ -114,23 +119,14 @@ public function divide($divisor, $roundingMode = \Money\Money::ROUND_HALF_UP)
*/
public function multiply($multiplier, $roundingMode = \Money\Money::ROUND_HALF_UP)
{
if (
is_int($multiplier)
|| (filter_var($multiplier, FILTER_VALIDATE_INT) !== false && ! is_float($multiplier))
) {
return $this->__call('multiply', [$multiplier, $roundingMode]);
}

$money = $this->getMoney();
$calculator = static::resolveCalculator();

return new self((int) round($calculator->multiply($money->getAmount(), $multiplier), 0, $roundingMode), $money->getCurrency());
return $this->__call('multiply', [
is_int($multiplier) ? $multiplier : strval($multiplier),
$roundingMode,
]);
}

/**
* Attributes.
*
* @param array $attributes
*/
public function attributes(array $attributes = [])
{
Expand All @@ -145,11 +141,7 @@ public function attributes(array $attributes = [])
#[\ReturnTypeWillChange]
public function jsonSerialize()
{
return array_merge(
$this->attributes,
$this->money->jsonSerialize(),
['formatted' => $this->format()]
);
return $this->serialize();
}

/**
Expand Down Expand Up @@ -187,8 +179,9 @@ public function __toString()
* __call.
*
* @param string $method
* @param array $arguments
* @return \Cknow\Money\Money|\Cknow\Money\Money[]|mixed
*
* @throws \BadMethodCallException
*/
public function __call($method, array $arguments)
{
Expand All @@ -197,7 +190,11 @@ public function __call($method, array $arguments)
}

if (! method_exists($this->money, $method)) {
return $this;
throw new BadMethodCallException(sprintf(
'Call to undefined method %s::%s()',
static::class,
$method
));
}

$result = call_user_func_array([$this->money, $method], static::getArguments($arguments));
Expand All @@ -219,7 +216,6 @@ public function __call($method, array $arguments)
/**
* Convert.
*
* @param \Money\Money $instance
* @return \Cknow\Money\Money
*/
public static function convert(\Money\Money $instance)
Expand All @@ -231,7 +227,6 @@ public static function convert(\Money\Money $instance)
* __callStatic.
*
* @param string $method
* @param array $arguments
* @return \Cknow\Money\Money
*/
public static function __callStatic($method, array $arguments)
Expand All @@ -248,7 +243,6 @@ public static function __callStatic($method, array $arguments)
/**
* Get arguments.
*
* @param array $arguments
* @return array
*/
private static function getArguments(array $arguments = [])
Expand Down
2 changes: 0 additions & 2 deletions src/MoneyFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,6 @@ trait MoneyFactory
* __callStatic.
*
* @param string $method
* @param array $arguments
* @return \Cknow\Money\Money
*/
public static function __callStatic($method, array $arguments)
Expand All @@ -208,7 +207,6 @@ public static function __callStatic($method, array $arguments)
/**
* Create a new instance from the base money instance.
*
* @param \Money\Money $instance
* @return \Cknow\Money\Money
*/
public static function fromMoney(\Money\Money $instance)
Expand Down
6 changes: 0 additions & 6 deletions src/MoneyFormatterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ trait MoneyFormatterTrait
* Format.
*
* @param string|null $locale
* @param \Money\Currencies|null $currencies
* @param int $style
* @return string
*
Expand Down Expand Up @@ -68,7 +67,6 @@ public function formatByAggregate(array $formatters)
* Format by bitcoin.
*
* @param int $fractionDigits
* @param \Money\Currencies|null $currencies
* @return string
*/
public function formatByBitcoin($fractionDigits = 2, Currencies $currencies = null)
Expand Down Expand Up @@ -96,7 +94,6 @@ public function formatByCurrencySymbol($right = false, $locale = null, Currencie
/**
* Format by decimal.
*
* @param \Money\Currencies|null $currencies
* @return string
*/
public function formatByDecimal(Currencies $currencies = null)
Expand All @@ -110,7 +107,6 @@ public function formatByDecimal(Currencies $currencies = null)
* Format by intl.
*
* @param string|null $locale
* @param \Money\Currencies|null $currencies
* @param int $style
* @return string
*/
Expand All @@ -126,7 +122,6 @@ public function formatByIntl($locale = null, Currencies $currencies = null, $sty
* Format by intl localized decimal.
*
* @param string|null $locale
* @param \Money\Currencies|null $currencies
* @param int $style
* @return string
*/
Expand All @@ -144,7 +139,6 @@ public function formatByIntlLocalizedDecimal(
/**
* Format by formatter.
*
* @param \Money\MoneyFormatter $formatter
* @return string
*/
public function formatByFormatter(MoneyFormatter $formatter)
Expand Down
Loading

0 comments on commit a7e4537

Please sign in to comment.