Open Journal Systems  3.3.0
IntlMoneyParser.php
1 <?php
2 
3 namespace Money\Parser;
4 
8 use Money\Money;
10 use Money\Number;
11 
17 final class IntlMoneyParser implements MoneyParser
18 {
22  private $formatter;
23 
27  private $currencies;
28 
33  public function __construct(\NumberFormatter $formatter, Currencies $currencies)
34  {
35  $this->formatter = $formatter;
36  $this->currencies = $currencies;
37  }
38 
42  public function parse($money, $forceCurrency = null)
43  {
44  if (!is_string($money)) {
45  throw new ParserException('Formatted raw money should be string, e.g. $1.00');
46  }
47 
48  $currency = null;
49  $decimal = $this->formatter->parseCurrency($money, $currency);
50 
51  if (false === $decimal) {
52  throw new ParserException(
53  'Cannot parse '.$money.' to Money. '.$this->formatter->getErrorMessage()
54  );
55  }
56 
57  if (null !== $forceCurrency) {
58  $currency = $forceCurrency;
59  } else {
60  $currency = new Currency($currency);
61  }
62 
63  /*
64  * This conversion is only required whilst currency can be either a string or a
65  * Currency object.
66  */
67  if (!$currency instanceof Currency) {
68  @trigger_error('Passing a currency as string is deprecated since 3.1 and will be removed in 4.0. Please pass a '.Currency::class.' instance instead.', E_USER_DEPRECATED);
69  $currency = new Currency($currency);
70  }
71 
72  $decimal = (string) $decimal;
73  $subunit = $this->currencies->subunitFor($currency);
74  $decimalPosition = strpos($decimal, '.');
75 
76  if (false !== $decimalPosition) {
77  $decimalLength = strlen($decimal);
78  $fractionDigits = $decimalLength - $decimalPosition - 1;
79  $decimal = str_replace('.', '', $decimal);
80  $decimal = Number::roundMoneyValue($decimal, $subunit, $fractionDigits);
81 
82  if ($fractionDigits > $subunit) {
83  $decimal = substr($decimal, 0, $decimalPosition + $subunit);
84  } elseif ($fractionDigits < $subunit) {
85  $decimal .= str_pad('', $subunit - $fractionDigits, '0');
86  }
87  } else {
88  $decimal .= str_pad('', $subunit, '0');
89  }
90 
91  if ('-' === $decimal[0]) {
92  $decimal = '-'.ltrim(substr($decimal, 1), '0');
93  } else {
94  $decimal = ltrim($decimal, '0');
95  }
96 
97  if ('' === $decimal) {
98  $decimal = '0';
99  }
100 
101  return new Money($decimal, $currency);
102  }
103 }
Money\Money
Definition: Money.php:16
Money
Money\Currencies
Definition: AggregateCurrencies.php:3
Money\Parser\IntlMoneyParser
Definition: IntlMoneyParser.php:17
Money\Number
Definition: paymethod/paypal/vendor/moneyphp/money/src/Number.php:10
Money\Parser\IntlMoneyParser\__construct
__construct(\NumberFormatter $formatter, Currencies $currencies)
Definition: IntlMoneyParser.php:39
Money\Currency
Definition: vendor/moneyphp/money/src/Currency.php:14
Money\Exception\ParserException
Definition: ParserException.php:12
Money\Parser
Definition: AggregateMoneyParser.php:3
Money\MoneyParser
Definition: MoneyParser.php:10
Money\Currencies
Definition: Currencies.php:12
Money\Number\roundMoneyValue
static roundMoneyValue($moneyValue, $targetDigits, $havingDigits)
Definition: paymethod/paypal/vendor/moneyphp/money/src/Number.php:309
Money\Parser\IntlMoneyParser\parse
parse($money, $forceCurrency=null)
Definition: IntlMoneyParser.php:48
Currency
Basic class describing a currency.
Definition: Currency.inc.php:24