Skip to content

Commit 935e888

Browse files
committed
fix parse space
1 parent 423e660 commit 935e888

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/MoneyParser.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ protected static function parseString(
4343
Currency|string $currency
4444
): ?Money {
4545

46+
$value = str($value)->trim()->replace([',', ' '], '')->value();
47+
4648
if (blank($value)) {
4749
return null;
4850
}
@@ -52,7 +54,7 @@ protected static function parseString(
5254
*/
5355
preg_match("/(?<currency>[A-Z]{3})? ?(?<amount>[-\d,\.]*)/", $value, $matches);
5456
/** @var array{ currency: string, amount: string } $matches */
55-
$amount = str_replace(',', '', $matches['amount']);
57+
$amount = $matches['amount'];
5658
$currency = $matches['currency'] ?: $currency;
5759

5860
if (blank($amount)) {

tests/MoneyParserTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@
6363
['EUR', '-1234.5', -1234.5],
6464
['EUR', '1234.56', 1234.56],
6565
['EUR', '-1234.56', -1234.56],
66+
// ignore ` `
67+
['EUR', '-1 234.56', -1234.56],
68+
['EUR', '1,234.56', 1234.56],
69+
// ignore `,`
70+
['EUR', '1,234.56', 1234.56],
71+
['EUR', '-1,234.56', -1234.56],
6672
]);
6773

6874
it('can parse string money with currency', function (string $value, float $expected, ?string $expectedCurrency = null) {
@@ -82,6 +88,9 @@
8288
['EUR -1234.5', -1234.5, 'EUR'],
8389
['EUR 1234.56', 1234.56, 'EUR'],
8490
['EUR -1234.56', -1234.56, 'EUR'],
91+
// ignore ` `
92+
['GBP 1 234.56', 1234.56, 'GBP'],
93+
['USD -1 234.56', -1234.56, 'USD'],
8594
// ignore `,`
8695
['GBP 1,234.56', 1234.56, 'GBP'],
8796
['USD -1,234.56', -1234.56, 'USD'],

0 commit comments

Comments
 (0)