PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.15.5
GiveWP – Donation Plugin and Fundraising Platform v4.15.5
4.16.9 4.16.8.1 4.16.8 4.16.7.2 4.16.7.1 4.16.7 4.16.6.1 4.16.6 4.16.5.1 4.16.5 4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 2.30.0 All 255 releases
give / vendor / moneyphp / money / src / Formatter / DecimalMoneyFormatter.php

DecimalMoneyFormatter.php in GiveWP – Donation Plugin and Fundraising Platform 4.15.5, at vendor/moneyphp/money/src/Formatter/DecimalMoneyFormatter.php

60 lines 1.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Money\Formatter;
4
5 use Money\Currencies;
6 use Money\Money;
7 use Money\MoneyFormatter;
8
9 /**
10 * Formats a Money object as a decimal string.
11 *
12 * @author Teoh Han Hui <teohhanhui@gmail.com>
13 */
14 final class DecimalMoneyFormatter implements MoneyFormatter
15 {
16 /**
17 * @var Currencies
18 */
19 private $currencies;
20
21 public function __construct(Currencies $currencies)
22 {
23 $this->currencies = $currencies;
24 }
25
26 /**
27 * {@inheritdoc}
28 */
29 public function format(Money $money)
30 {
31 $valueBase = $money->getAmount();
32 $negative = false;
33
34 if ($valueBase[0] === '-') {
35 $negative = true;
36 $valueBase = substr($valueBase, 1);
37 }
38
39 $subunit = $this->currencies->subunitFor($money->getCurrency());
40 $valueLength = strlen($valueBase);
41
42 if ($valueLength > $subunit) {
43 $formatted = substr($valueBase, 0, $valueLength - $subunit);
44 $decimalDigits = substr($valueBase, $valueLength - $subunit);
45
46 if (strlen($decimalDigits) > 0) {
47 $formatted .= '.'.$decimalDigits;
48 }
49 } else {
50 $formatted = '0.'.str_pad('', $subunit - $valueLength, '0').$valueBase;
51 }
52
53 if ($negative === true) {
54 $formatted = '-'.$formatted;
55 }
56
57 return $formatted;
58 }
59 }
60