-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HP-1950 Refactored progressive prices (#69)
* HP-1950 Refactored progressive prices * Added ProgressivePriceCalculationTrace, refined tests Renamed ProgressivePriceThresholds to ProgressivePriceThresholdList * Make MultipliedMoney properties R/O
- Loading branch information
1 parent
54e3cc0
commit a9003e6
Showing
15 changed files
with
909 additions
and
319 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace hiqdev\php\billing\Money; | ||
|
||
use Laminas\Code\Reflection\Exception\InvalidArgumentException; | ||
use Money\Currencies\ISOCurrencies; | ||
use Money\Currency; | ||
use Money\Money; | ||
use Money\MoneyParser; | ||
use Money\Parser\DecimalMoneyParser; | ||
|
||
/** | ||
* Class MultipliedMoney a wrapper around the Money class and provides | ||
* a way to work with sub-cent prices. | ||
* | ||
* For example, if you have a price of $0.001, it will be represented as | ||
* 1 USD with a multiplier of 1000. After you do some calculations with this | ||
* price, you can convert it back to the decimal representation by dividing | ||
* the amount by the multiplier. | ||
* | ||
* By default, the MultipliedMoney uses the DecimalMoneyParser to parse the | ||
* amount. You can set your own parser by calling the setDecimalMoneyParser. | ||
* | ||
* @author Dmytro Naumenko <[email protected]> | ||
*/ | ||
final class MultipliedMoney | ||
{ | ||
private function __construct( | ||
private readonly Money $money, | ||
private readonly int $multiplier = 1 | ||
) { | ||
} | ||
|
||
/** | ||
* @param numeric-string $amount | ||
* @param string $currencyCode | ||
*/ | ||
public static function create(string $amount, string $currencyCode): MultipliedMoney | ||
{ | ||
if (!is_numeric($amount)) { | ||
throw new InvalidArgumentException('Amount of the MultipliedMoney must be numeric'); | ||
} | ||
|
||
$currency = new Currency($currencyCode); | ||
$parser = self::getMoneyParser(); | ||
|
||
if (!self::isFloat($amount) || self::isWhole($amount)) { | ||
return new self($parser->parse($amount, $currency), 1); | ||
} | ||
|
||
$multiplier = self::calculateMultiplierToInteger($amount); | ||
return new self( | ||
$parser->parse((string)($amount * $multiplier), $currency), | ||
$multiplier | ||
); | ||
} | ||
|
||
public function money(): Money | ||
{ | ||
return $this->money; | ||
} | ||
|
||
public function multiplier(): int | ||
{ | ||
return $this->multiplier; | ||
} | ||
|
||
public function getCurrency(): Currency | ||
{ | ||
return $this->money->getCurrency(); | ||
} | ||
|
||
public function getAmount(): string | ||
{ | ||
return $this->money->getAmount(); | ||
} | ||
|
||
/** | ||
* @param numeric-string $amount | ||
*/ | ||
private static function calculateMultiplierToInteger(string $amount): int | ||
{ | ||
if (self::isWhole($amount)) { | ||
return 1; | ||
} | ||
|
||
[$integer, $fraction] = explode('.', $amount, 2); | ||
return (int)('1' . implode(array_fill(0, strlen($fraction), 0))); | ||
} | ||
|
||
/** | ||
* @param numeric-string $number | ||
*/ | ||
private static function isWhole(string $number): bool | ||
{ | ||
/** @noinspection PhpWrongStringConcatenationInspection */ | ||
return is_int($number + 0); | ||
} | ||
|
||
/** | ||
* @param numeric-string $number | ||
*/ | ||
private static function isFloat(string $number): bool | ||
{ | ||
return str_contains($number, '.'); | ||
} | ||
|
||
private static MoneyParser $moneyParser; | ||
public static function setDecimalMoneyParser(MoneyParser $moneyParser): void | ||
{ | ||
self::$moneyParser = $moneyParser; | ||
} | ||
private static function getMoneyParser(): MoneyParser | ||
{ | ||
if (!isset(self::$moneyParser)) { | ||
self::setDecimalMoneyParser(new DecimalMoneyParser(new ISOCurrencies())); | ||
} | ||
|
||
return self::$moneyParser; | ||
} | ||
|
||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace hiqdev\php\billing\price; | ||
|
||
use hiqdev\php\units\Quantity; | ||
use Money\Money; | ||
use Stringable; | ||
|
||
/** | ||
* Class ProgressivePriceCalculationTrace represents a single step in the calculation of a progressive price. | ||
* | ||
* @author Dmytro Naumenko <[email protected]> | ||
*/ | ||
final class ProgressivePriceCalculationTrace implements Stringable | ||
{ | ||
public function __construct( | ||
public ProgressivePriceThreshold $threshold, | ||
public Quantity $billedUsage, | ||
public Money $charged, | ||
) { | ||
} | ||
|
||
public function __toString(): string | ||
{ | ||
return sprintf( | ||
"%s%s * %s = %s", | ||
$this->billedUsage->getQuantity(), | ||
$this->billedUsage->getUnit()->getName(), | ||
$this->threshold->getRawPrice(), | ||
number_format($this->charged->getAmount()/100, 2), | ||
); | ||
} | ||
|
||
public function toShortString(): string | ||
{ | ||
return sprintf( | ||
"%s*%s", | ||
$this->billedUsage->getQuantity(), | ||
$this->threshold->getRawPrice(), | ||
); | ||
} | ||
} |
Oops, something went wrong.