Open Journal Systems  3.3.0
IntlLocalizedDecimalParser.php
1 <?php
2 
3 namespace Money\Parser;
4 
8 use Money\Money;
10 use Money\Number;
11 
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  if (null === $forceCurrency) {
49  throw new ParserException(
50  'IntlLocalizedDecimalParser cannot parse currency symbols. Use forceCurrency argument'
51  );
52  }
53 
54  $decimal = $this->formatter->parse($money);
55 
56  if (false === $decimal) {
57  throw new ParserException(
58  'Cannot parse '.$money.' to Money. '.$this->formatter->getErrorMessage()
59  );
60  }
61 
62  /*
63  * This conversion is only required whilst currency can be either a string or a
64  * Currency object.
65  */
66  if (!$forceCurrency instanceof Currency) {
67  @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);
68  $forceCurrency = new Currency($forceCurrency);
69  }
70 
71  $decimal = (string) $decimal;
72  $subunit = $this->currencies->subunitFor($forceCurrency);
73  $decimalPosition = strpos($decimal, '.');
74 
75  if (false !== $decimalPosition) {
76  $decimalLength = strlen($decimal);
77  $fractionDigits = $decimalLength - $decimalPosition - 1;
78  $decimal = str_replace('.', '', $decimal);
79  $decimal = Number::roundMoneyValue($decimal, $subunit, $fractionDigits);
80 
81  if ($fractionDigits > $subunit) {
82  $decimal = substr($decimal, 0, $decimalPosition + $subunit);
83  } elseif ($fractionDigits < $subunit) {
84  $decimal .= str_pad('', $subunit - $fractionDigits, '0');
85  }
86  } else {
87  $decimal .= str_pad('', $subunit, '0');
88  }
89 
90  if ('-' === $decimal[0]) {
91  $decimal = '-'.ltrim(substr($decimal, 1), '0');
92  } else {
93  $decimal = ltrim($decimal, '0');
94  }
95 
96  if ('' === $decimal) {
97  $decimal = '0';
98  }
99 
100  return new Money($decimal, $forceCurrency);
101  }
102 }
Money\Money
Definition: Money.php:16
Money
Money\Parser\IntlLocalizedDecimalParser\parse
parse($money, $forceCurrency=null)
Definition: IntlLocalizedDecimalParser.php:48
Money\Currencies
Definition: AggregateCurrencies.php:3
Money\Number
Definition: paymethod/paypal/vendor/moneyphp/money/src/Number.php:10
Money\Parser\IntlLocalizedDecimalParser
Definition: IntlLocalizedDecimalParser.php:17
Money\Parser\IntlLocalizedDecimalParser\__construct
__construct(\NumberFormatter $formatter, Currencies $currencies)
Definition: IntlLocalizedDecimalParser.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