From eeb5db73367484d526b16ed27b7e49e52e20cbb4 Mon Sep 17 00:00:00 2001 From: Mark Armendariz Date: Wed, 28 Dec 2016 10:36:56 -0600 Subject: [PATCH] Fix incorrect regex for amount conversion. The regex in Ofx::createAmountFromStr included a "?" after the "mark" whether it be decimal or comma. That "?" made it match number strings regardless of whether they had a "mark" to parse out, and grossly changed their value. This made amounts such as 1000 or -1000 convert to 10 or -10. Removing the conditional after the mark resolved this issue. There are new tests to show that this resolves the issue without breaking others. Resolves #19 --- lib/OfxParser/Ofx.php | 4 ++-- tests/OfxParser/OfxTest.php | 7 ++++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/OfxParser/Ofx.php b/lib/OfxParser/Ofx.php index c3d0b6e..872aedb 100644 --- a/lib/OfxParser/Ofx.php +++ b/lib/OfxParser/Ofx.php @@ -310,7 +310,7 @@ private function createDateTimeFromStr($dateString, $ignoreErrors = false) private function createAmountFromStr($amountString) { // Decimal mark style (UK/US): 000.00 or 0,000.00 - if (preg_match('/^-?([\d,]+)(\.?)([\d]{2})$/', $amountString) === 1) { + if (preg_match('/^-?([\d,]+)(\.)([\d]{2})$/', $amountString) === 1) { return (float)preg_replace( ['/([,]+)/', '/\.?([\d]{2})$/'], ['', '.$1'], @@ -319,7 +319,7 @@ private function createAmountFromStr($amountString) } // European style: 000,00 or 0.000,00 - if (preg_match('/^-?([\d\.]+,?[\d]{2})$/', $amountString) === 1) { + if (preg_match('/^-?([\d\.]+,[\d]{2})$/', $amountString) === 1) { return (float)preg_replace( ['/([\.]+)/', '/,?([\d]{2})$/'], ['', '.$1'], diff --git a/tests/OfxParser/OfxTest.php b/tests/OfxParser/OfxTest.php index 486f287..1d3d02c 100644 --- a/tests/OfxParser/OfxTest.php +++ b/tests/OfxParser/OfxTest.php @@ -37,7 +37,12 @@ public function amountConversionProvider() '-1.000,00' => ['-1.000,00', -1000.0], '1' => ['1', 1.0], '10' => ['10', 10.0], - '100' => ['100', 1.0], // @todo this is weird behaviour, should not really expect this + '100' => ['100', 100.0], + '1000' => ['1000', 1000.0], + '10000' => ['10000', 10000.0], + '-100' => ['-100', -100.0], + '-1000' => ['-1000', -1000.0], + '-10000' => ['-10000', -10000.0] ]; }