From 6da397db8540a6e895814ec2d20f8cb6352623db Mon Sep 17 00:00:00 2001 From: Ricardo Gobbo de Souza Date: Thu, 17 Aug 2023 13:00:03 -0300 Subject: [PATCH] Fix money parser --- src/MoneyParserTrait.php | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/src/MoneyParserTrait.php b/src/MoneyParserTrait.php index 9c4ce42..d1f6fee 100644 --- a/src/MoneyParserTrait.php +++ b/src/MoneyParserTrait.php @@ -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); + } + } } }