Skip to content

Commit

Permalink
Fix money parser
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardogobbosouza committed Aug 17, 2023
1 parent 1604577 commit 6da397d
Showing 1 changed file with 24 additions and 9 deletions.
33 changes: 24 additions & 9 deletions src/MoneyParserTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,34 @@ public static function parse(
}

$locale = $locale ?: static::getLocale();
$style = NumberFormatter::CURRENCY || NumberFormatter::DECIMAL;

$parsers = [
new IntlMoneyParser(new NumberFormatter($locale, $style), $currencies),
new IntlLocalizedDecimalParser(new NumberFormatter($locale, $style), $currencies),
new DecimalMoneyParser($currencies),
new BitcoinMoneyParser($bitCointDigits ?? 2),
];
$bitCointDigits = $bitCointDigits ?? 2;

try {
$parsers = [
new IntlMoneyParser(new NumberFormatter($locale, NumberFormatter::CURRENCY), $currencies),
new IntlLocalizedDecimalParser(new NumberFormatter($locale, NumberFormatter::CURRENCY), $currencies),
new DecimalMoneyParser($currencies),
new BitcoinMoneyParser($bitCointDigits),
];

return static::parseByAggregate($value, null, $parsers, $convert);
} catch (ParserException $e) {
return static::parseByAggregate($value, $currency, $parsers, $convert);
try {
return static::parseByAggregate($value, $currency, $parsers, $convert);
} catch (ParserException $e) {
$parsers = [
new IntlMoneyParser(new NumberFormatter($locale, NumberFormatter::DECIMAL), $currencies),
new IntlLocalizedDecimalParser(new NumberFormatter($locale, NumberFormatter::DECIMAL), $currencies),
new DecimalMoneyParser($currencies),
new BitcoinMoneyParser($bitCointDigits),
];

try {
return static::parseByAggregate($value, null, $parsers, $convert);
} catch (ParserException $e) {
return static::parseByAggregate($value, $currency, $parsers, $convert);
}
}
}
}

Expand Down

0 comments on commit 6da397d

Please sign in to comment.