1 | <?php |
||
15 | final class Money implements MoneyContract |
||
16 | { |
||
17 | const DECIMAL_NUMBER_REGEXP = '(?P<amount> 0*(([1-9][0-9]*|[0-9])(\.[0-9]+)?))'; |
||
18 | const SIMPLE_CURRENCY_PATTERN = '/^'.self::DECIMAL_NUMBER_REGEXP.'$/x'; |
||
19 | const INNER_FRACTIONAL_DIGITS = 8; |
||
20 | |||
21 | /** @var Decimal */ |
||
22 | private $amount; |
||
23 | |||
24 | /** @var CurrencyContract */ |
||
25 | private $currency; |
||
26 | |||
27 | /** |
||
28 | * @param Decimal $amount |
||
29 | * @param CurrencyContract $currency |
||
30 | */ |
||
31 | 56 | private function __construct(Decimal $amount, CurrencyContract $currency) |
|
40 | |||
41 | 11 | public static function fromFloat(float $amount, CurrencyContract $currency): Money |
|
42 | { |
||
43 | try { |
||
44 | 11 | return new self( |
|
45 | 11 | Decimal::fromFloat($amount, self::INNER_FRACTIONAL_DIGITS), |
|
46 | 8 | $currency |
|
47 | ); |
||
48 | 7 | } catch (InfiniteInputError $e) { |
|
49 | 2 | throw new InvalidArgumentException('Currency amounts must be finite', 0, $e); |
|
50 | 5 | } catch (NaNInputError $e) { |
|
51 | 1 | throw new InvalidArgumentException('Currency amounts must be numbers', 0, $e); |
|
52 | } |
||
53 | } |
||
54 | |||
55 | 8 | public static function fromFractionalUnits(int $amount, CurrencyContract $currency): Money |
|
65 | |||
66 | 64 | public static function fromString(string $amount, CurrencyContract $currency): Money |
|
73 | |||
74 | 64 | private static function extractNumericAmount(string $amount, CurrencyContract $currency): Decimal |
|
86 | |||
87 | /** |
||
88 | * @param CurrencyContract $currency |
||
89 | * |
||
90 | * @return string |
||
91 | */ |
||
92 | 56 | private static function getAmountPlusIsoCodePattern(CurrencyContract $currency): string |
|
98 | |||
99 | 48 | private static function getAmountPlusSymbolPattern(CurrencyContract $currency): string |
|
107 | |||
108 | 8 | public static function fromDecimal(Decimal $amount, CurrencyContract $currency): Money |
|
109 | { |
||
110 | 8 | return new self( |
|
111 | 8 | Decimal::fromDecimal($amount, self::INNER_FRACTIONAL_DIGITS), |
|
112 | 8 | $currency |
|
113 | ); |
||
114 | } |
||
115 | |||
116 | 9 | public function getCurrency(): CurrencyContract |
|
120 | |||
121 | /** |
||
122 | * {@inheritdoc} |
||
123 | */ |
||
124 | 13 | public function getAmountAsDecimal(): Decimal |
|
128 | |||
129 | /** |
||
130 | * {@inheritdoc} |
||
131 | */ |
||
132 | 24 | public function getAmountAsFractionalUnits(): int |
|
141 | |||
142 | /** |
||
143 | * {@inheritdoc} |
||
144 | */ |
||
145 | 126 | public function format(MoneyFormatInterface $currencyFormat = null): string |
|
146 | { |
||
147 | 126 | if (is_null($currencyFormat)) { |
|
148 | 24 | $currencyFormat = MoneyFormat::default(); |
|
149 | } |
||
150 | |||
151 | 126 | $nDecimals = $currencyFormat->getPrecision(); |
|
152 | 126 | if (is_null($nDecimals)) { |
|
153 | 110 | $nDecimals = $this->currency->getNumFractionalDigits() + $currencyFormat->getExtraPrecision(); |
|
154 | } |
||
155 | |||
156 | 126 | $amount = Decimal::fromDecimal($this->amount, $nDecimals); |
|
157 | |||
158 | 126 | $number = ('' === $currencyFormat->getThousandsSeparator()) |
|
159 | 110 | ? \str_replace('.', $currencyFormat->getDecimalsSeparator(), $amount->__toString()) // This is safer! |
|
160 | 16 | : \number_format( |
|
161 | 16 | $amount->asFloat(), |
|
162 | 16 | $nDecimals, |
|
163 | 16 | $currencyFormat->getDecimalsSeparator(), |
|
164 | 126 | $currencyFormat->getThousandsSeparator() |
|
165 | ); |
||
166 | |||
167 | 126 | return $this->decorate($number, $currencyFormat); |
|
168 | } |
||
169 | |||
170 | /** |
||
171 | * @param string $number |
||
172 | * @param MoneyFormatInterface $currencyFormat |
||
173 | * |
||
174 | * @return string |
||
175 | */ |
||
176 | 126 | private function decorate(string $number, MoneyFormatInterface $currencyFormat): string |
|
193 | |||
194 | /** |
||
195 | * {@inheritdoc} |
||
196 | */ |
||
197 | 12 | public function equals(MoneyContract $currency): bool |
|
204 | } |
||
205 |