Address.php
6 years ago
CardInfo.php
6 years ago
DonorInfo.php
6 years ago
Money.php
5 years ago
ValueObjects.php
6 years ago
Money.php
141 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\ValueObjects; |
| 4 | |
| 5 | /** |
| 6 | * Class Money |
| 7 | * @package Give\ValueObjects |
| 8 | * |
| 9 | * @since 2.9.0 |
| 10 | */ |
| 11 | class Money { |
| 12 | /** |
| 13 | * @var int|string |
| 14 | */ |
| 15 | private $amount; |
| 16 | |
| 17 | /** |
| 18 | * @var int |
| 19 | */ |
| 20 | private $minorAmount; |
| 21 | |
| 22 | /** |
| 23 | * @var string |
| 24 | */ |
| 25 | private $currency; |
| 26 | |
| 27 | /** |
| 28 | * @var array |
| 29 | */ |
| 30 | private $currencyData; |
| 31 | |
| 32 | /** |
| 33 | * Return Money class object. |
| 34 | * |
| 35 | * @since 2.9.0 |
| 36 | * |
| 37 | * @param int|string $amount Amount value without currency formatting |
| 38 | * @param string $currency |
| 39 | * |
| 40 | * @return Money |
| 41 | */ |
| 42 | public static function of( $amount, $currency ) { |
| 43 | $object = new static(); |
| 44 | |
| 45 | $object->amount = $amount; |
| 46 | $object->currency = $currency; |
| 47 | $object->currencyData = self::getCurrencyData( $currency ); |
| 48 | |
| 49 | return $object; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Return Money class object. |
| 54 | * |
| 55 | * @since 2.9.0 |
| 56 | * |
| 57 | * @param int|string $amount |
| 58 | * @param string $currency |
| 59 | * |
| 60 | * @return Money |
| 61 | */ |
| 62 | public static function ofMinor( $amount, $currency ) { |
| 63 | $object = new static(); |
| 64 | |
| 65 | $object->minorAmount = $amount; |
| 66 | $object->currency = $currency; |
| 67 | $object->currencyData = self::getCurrencyData( $currency ); |
| 68 | |
| 69 | return $object; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Retrieves the currency data for a given currency with some optimizations to avoid loading all the currencies more |
| 74 | * than once. |
| 75 | * |
| 76 | * @since 2.9.0 |
| 77 | * |
| 78 | * @param $currency |
| 79 | * |
| 80 | * @return array |
| 81 | */ |
| 82 | private static function getCurrencyData( $currency ) { |
| 83 | static $currenciesData = null; |
| 84 | |
| 85 | if ( $currenciesData === null ) { |
| 86 | $currenciesData = give_get_currencies( 'all' ); |
| 87 | } |
| 88 | |
| 89 | return $currenciesData[ $currency ]; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Get amount in smallest unit of currency. |
| 94 | * |
| 95 | * @sicne 2.9.0 |
| 96 | * |
| 97 | * @return int |
| 98 | */ |
| 99 | public function getMinorAmount() { |
| 100 | if ( $this->minorAmount ) { |
| 101 | return $this->minorAmount; |
| 102 | } |
| 103 | |
| 104 | $decimals = $this->getNumberDecimals(); |
| 105 | |
| 106 | $tensMultiplier = 10 ** $decimals; |
| 107 | |
| 108 | return $this->minorAmount = absint( $this->amount * $tensMultiplier ); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Get amount in smallest unit of currency. |
| 113 | * |
| 114 | * @sicne 2.9.0 |
| 115 | * |
| 116 | * @return string |
| 117 | */ |
| 118 | public function getAmount() { |
| 119 | if ( $this->amount ) { |
| 120 | return $this->amount; |
| 121 | } |
| 122 | |
| 123 | $decimals = $this->getNumberDecimals(); |
| 124 | |
| 125 | $tensMultiplier = 10 ** $decimals; |
| 126 | |
| 127 | return $this->amount = absint( $this->minorAmount / $tensMultiplier ); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Returns the number of decimals based on the currency |
| 132 | * |
| 133 | * @since 2.9.0 |
| 134 | * |
| 135 | * @return int |
| 136 | */ |
| 137 | private function getNumberDecimals() { |
| 138 | return $this->currencyData['setting']['number_decimals']; |
| 139 | } |
| 140 | } |
| 141 |