Skip to content

Commit

Permalink
Updated package to use RationalMoney internally
Browse files Browse the repository at this point in the history
  • Loading branch information
toonvandenbos committed Sep 13, 2024
1 parent 5c9f01e commit d689fd4
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 107 deletions.
2 changes: 1 addition & 1 deletion src/Concerns/HasModifiers.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ protected function makeModifier(string $type, string|callable|AbstractMoney|Pric
$modifier = $modifier->inclusive();
}

return $instance->add($modifier, Price::getRounding('exclusive'));
return $instance->add($modifier);
}

/**
Expand Down
49 changes: 17 additions & 32 deletions src/Concerns/OperatesOnBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Whitecube\Price\Price;
use Brick\Money\AbstractMoney;
use Brick\Money\RationalMoney;
use Brick\Money\Exception\MoneyMismatchException;
use Brick\Money\Money;
use Brick\Math\BigNumber;
Expand All @@ -21,7 +22,7 @@ public function __call(string $method, array $arguments): mixed

$result = call_user_func_array([$this->base, $method], $arguments);

if(! is_a($result, AbstractMoney::class)) {
if(! is_a($result, RationalMoney::class)) {
return $result;
}

Expand All @@ -33,64 +34,48 @@ public function __call(string $method, array $arguments): mixed
}

/**
* Check if given value equals the price's base value
* Check if the provided amount equals this price's total inclusive amount
*/
public function equals(BigNumber|int|float|string|AbstractMoney|Price $value): bool
public function equals(BigNumber|int|float|string|AbstractMoney|Price $that): bool
{
return $this->compareTo($value) === 0;
return $this->compareTo($that) === 0;
}

/**
* Compare a given value to the total inclusive value of this instance
* Compare the provided amount to this price's total inclusive amount
*/
public function compareTo(BigNumber|int|float|string|AbstractMoney|Price $value): int
public function compareTo(BigNumber|int|float|string|AbstractMoney|Price $that): int
{
return $this->compareMonies(
$this->inclusive(),
$this->valueToMoney($value)
$this->valueToRationalMoney($that, 'inclusive')
);
}

/**
* Compare a given value to the unitless base value of this instance
* Compare a provided amount to this price's unitless base amount
*/
public function compareBaseTo(BigNumber|int|float|string|AbstractMoney|Price $value): int
public function compareBaseTo(BigNumber|int|float|string|AbstractMoney|Price $that): int
{
return $this->compareMonies(
$this->base(),
$this->valueToMoney($value, 'base')
$this->valueToRationalMoney($that, 'base')
);
}

/**
* Compare the given "current" value to another value
* Compare the provided "current" amount to another amount
* @throws \Brick\Money\Exception\MoneyMismatchException
*/
protected function compareMonies(AbstractMoney $price, AbstractMoney $that): int
protected function compareMonies(RationalMoney $current, RationalMoney $that): int
{
$priceCurrency = $price->getCurrency();
$currentCurrency = $this->currency();
$thatCurrency = $that->getCurrency();

if($priceCurrency->getCurrencyCode() === $thatCurrency->getCurrencyCode()) {
return $price->getAmount()->compareTo($that->getAmount());
if($currentCurrency->getCurrencyCode() === $thatCurrency->getCurrencyCode()) {
return $current->getAmount()->compareTo($that->getAmount());
}

throw MoneyMismatchException::currencyMismatch($priceCurrency, $thatCurrency);
}

/**
* Transform a given value into a Money instance
*/
protected function valueToMoney(BigNumber|int|float|string|AbstractMoney|Price $value, string $method = 'inclusive'): AbstractMoney
{
if(is_a($value, Price::class)) {
$value = $value->$method();
}

if(is_a($value, AbstractMoney::class)) {
return $value;
}

return Money::ofMinor($value, $this->currency());
throw MoneyMismatchException::currencyMismatch($currentCurrency, $thatCurrency);
}
}
34 changes: 17 additions & 17 deletions src/Modifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Whitecube\Price;

use Brick\Money\Money;
use Brick\Money\AbstractMoney;
use Brick\Money\RationalMoney;

class Modifier implements PriceAmendable
{
Expand Down Expand Up @@ -155,11 +155,11 @@ public function appliesPerUnit(): bool
/**
* Add an addition modification to the stack
*/
public function add(...$arguments): static
public function add($argument): static
{
$this->stack[] = [
'method' => 'plus',
'arguments' => $arguments
'argument' => $argument
];

return $this;
Expand All @@ -168,11 +168,11 @@ public function add(...$arguments): static
/**
* Add a substraction modification to the stack
*/
public function subtract(...$arguments): static
public function subtract($argument): static
{
$this->stack[] = [
'method' => 'minus',
'arguments' => $arguments
'argument' => $argument
];

return $this;
Expand All @@ -181,11 +181,11 @@ public function subtract(...$arguments): static
/**
* Add a multiplication modification to the stack
*/
public function multiply(...$arguments): static
public function multiply($argument): static
{
$this->stack[] = [
'method' => 'multipliedBy',
'arguments' => $arguments
'argument' => $argument
];

return $this;
Expand All @@ -194,11 +194,11 @@ public function multiply(...$arguments): static
/**
* Add a division modification to the stack
*/
public function divide(...$arguments): static
public function divide($argument): static
{
$this->stack[] = [
'method' => 'dividedBy',
'arguments' => $arguments
'argument' => $argument
];

return $this;
Expand All @@ -219,7 +219,7 @@ public function abs(): static
/**
* Apply the modifier on the given Money instance
*/
public function apply(AbstractMoney $build, $units, $perUnit, AbstractMoney $exclusive = null, Vat $vat = null) : ?AbstractMoney
public function apply(RationalMoney $build, $units, $perUnit, RationalMoney $exclusive = null, Vat $vat = null) : ?RationalMoney
{
if(! $this->stack) {
return null;
Expand All @@ -230,15 +230,15 @@ public function apply(AbstractMoney $build, $units, $perUnit, AbstractMoney $exc
return $this->applyStackAction($action, $build);
}

$argument = is_a($action['arguments'][0] ?? null, AbstractMoney::class)
? $action['arguments'][0]
: Money::ofMinor($action['arguments'][0] ?? 0, $build->getCurrency());
$argument = is_a($action['argument'] ?? null, RationalMoney::class)
? $action['argument']
: Money::ofMinor($action['argument'] ?? 0, $build->getCurrency())->toRational();

if($this->appliesPerUnit() && (! $perUnit) && $units > 1) {
$argument = $argument->multipliedBy($units, Price::getRounding('exclusive'));
$argument = $argument->multipliedBy($units);
}

$action['arguments'][0] = $argument;
$action['argument'] = $argument;

return $this->applyStackAction($action, $build);
}, $build);
Expand All @@ -247,8 +247,8 @@ public function apply(AbstractMoney $build, $units, $perUnit, AbstractMoney $exc
/**
* Apply given stack action on the price being build
*/
protected function applyStackAction(array $action, AbstractMoney $build): AbstractMoney
protected function applyStackAction(array $action, RationalMoney $build): RationalMoney
{
return call_user_func_array([$build, $action['method']], $action['arguments'] ?? []);
return call_user_func([$build, $action['method']], $action['argument'] ?? null);
}
}
Loading

0 comments on commit d689fd4

Please sign in to comment.