35 $this->formatter = $formatter;
36 $this->currencies = $currencies;
42 public function parse($money, $forceCurrency =
null)
44 if (!is_string($money)) {
45 throw new ParserException(
'Formatted raw money should be string, e.g. $1.00');
48 if (
null === $forceCurrency) {
50 'IntlLocalizedDecimalParser cannot parse currency symbols. Use forceCurrency argument'
54 $decimal = $this->formatter->parse($money);
56 if (
false === $decimal) {
58 'Cannot parse '.$money.
' to Money. '.$this->formatter->getErrorMessage()
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);
71 $decimal = (string) $decimal;
72 $subunit = $this->currencies->subunitFor($forceCurrency);
73 $decimalPosition = strpos($decimal,
'.');
75 if (
false !== $decimalPosition) {
76 $decimalLength = strlen($decimal);
77 $fractionDigits = $decimalLength - $decimalPosition - 1;
78 $decimal = str_replace(
'.',
'', $decimal);
81 if ($fractionDigits > $subunit) {
82 $decimal = substr($decimal, 0, $decimalPosition + $subunit);
83 } elseif ($fractionDigits < $subunit) {
84 $decimal .= str_pad(
'', $subunit - $fractionDigits,
'0');
87 $decimal .= str_pad(
'', $subunit,
'0');
90 if (
'-' === $decimal[0]) {
91 $decimal =
'-'.ltrim(substr($decimal, 1),
'0');
93 $decimal = ltrim($decimal,
'0');
96 if (
'' === $decimal) {
100 return new Money($decimal, $forceCurrency);