Address.php
5 years ago
CardInfo.php
5 years ago
DonorInfo.php
5 years ago
Money.php
5 years ago
ValueObjects.php
6 years ago
Money.php
123 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\ValueObjects; |
| 4 | |
| 5 | /** |
| 6 | * Class Money |
| 7 | * @package Give\ValueObjects |
| 8 | * |
| 9 | * @since 2.9.0 |
| 10 | * @since 2.11.0 Refactored to make the minor amount the base. |
| 11 | */ |
| 12 | class Money { |
| 13 | /** |
| 14 | * The amount in smallest unit of currency. |
| 15 | * @var int |
| 16 | */ |
| 17 | protected $minorAmount; |
| 18 | |
| 19 | /** |
| 20 | * @var array |
| 21 | */ |
| 22 | protected $currencyData; |
| 23 | |
| 24 | /** |
| 25 | * Money constructor. |
| 26 | * @param int $minorAmount |
| 27 | * @param array $currencyData |
| 28 | */ |
| 29 | public function __construct( $minorAmount, $currencyData ) { |
| 30 | $this->minorAmount = $minorAmount; |
| 31 | $this->currencyData = $currencyData; |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Get amount in smallest unit of currency. |
| 36 | * |
| 37 | * @sicne 2.9.0 |
| 38 | * @since 2.11.0 Round minor amount to account for floating point precision. |
| 39 | * |
| 40 | * @return int |
| 41 | */ |
| 42 | public function getMinorAmount() { |
| 43 | return $this->minorAmount; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Get amount in smallest unit of currency. |
| 48 | * |
| 49 | * @sicne 2.9.0 |
| 50 | * |
| 51 | * @return string |
| 52 | */ |
| 53 | public function getAmount() { |
| 54 | if ( $this->currencyData['setting']['number_decimals'] ) { |
| 55 | return $this->minorAmount / ( 10 ** $this->currencyData['setting']['number_decimals'] ); |
| 56 | } |
| 57 | return $this->minorAmount; |
| 58 | } |
| 59 | |
| 60 | // Static Methods and Factories |
| 61 | |
| 62 | /** |
| 63 | * @since 2.9.0 |
| 64 | * @since 2.11.0 Converts the amount to a minor amount when creating an instance. |
| 65 | * |
| 66 | * @param int|string $amount Amount value without currency formatting |
| 67 | * @param string $currency |
| 68 | * |
| 69 | * @return Money |
| 70 | */ |
| 71 | public static function of( $amount, $currency ) { |
| 72 | |
| 73 | $currencyData = self::getCurrencyData( $currency ); |
| 74 | |
| 75 | /** |
| 76 | * When working with float values, be careful when casting to an integer. |
| 77 | * Due to "floating point precision", the output may not match the expected value. |
| 78 | * |
| 79 | * @link https://www.php.net/manual/en/language.types.float.php |
| 80 | * This can lead to confusing results: |
| 81 | * for example, floor((0.1+0.7)*10) will usually return 7 instead of the expected 8, |
| 82 | * since the internal representation will be something like 7.9999999999999991118.... |
| 83 | */ |
| 84 | $amount = absint( |
| 85 | round( $amount * ( 10 ** $currencyData['setting']['number_decimals'] ) ) |
| 86 | ); |
| 87 | |
| 88 | return new static( $amount, $currencyData ); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * @since 2.9.0 |
| 93 | * |
| 94 | * @param int|string $minorAmount |
| 95 | * @param string $currency |
| 96 | * |
| 97 | * @return Money |
| 98 | */ |
| 99 | public static function ofMinor( $minorAmount, $currency ) { |
| 100 | return new static( $minorAmount, self::getCurrencyData( $currency ) ); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Retrieves the currency data for a given currency with some optimizations to avoid loading all the currencies more |
| 105 | * than once. |
| 106 | * |
| 107 | * @since 2.9.0 |
| 108 | * |
| 109 | * @param $currency |
| 110 | * |
| 111 | * @return array |
| 112 | */ |
| 113 | private static function getCurrencyData( $currency ) { |
| 114 | static $currenciesData = null; |
| 115 | |
| 116 | if ( $currenciesData === null ) { |
| 117 | $currenciesData = give_get_currencies( 'all' ); |
| 118 | } |
| 119 | |
| 120 | return $currenciesData[ $currency ]; |
| 121 | } |
| 122 | } |
| 123 |