Skip to content
This repository has been archived by the owner on Sep 8, 2020. It is now read-only.

Enhance OFX Parser #46

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 25 additions & 27 deletions lib/OfxParser/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,8 @@ public function loadFromFile($ofxFile)
public function loadFromString($ofxContent)
{
$ofxContent = utf8_encode($ofxContent);
$ofxContent = $this->conditionallyAddNewlines($ofxContent);

$sgmlStart = stripos($ofxContent, '<OFX>');
$ofxSgml = trim(substr($ofxContent, $sgmlStart));
$ofxSgml = trim($this->fixNewlines(substr($ofxContent, $sgmlStart)));

$ofxXml = $this->convertSgmlToXml($ofxSgml);

Expand All @@ -52,18 +50,17 @@ public function loadFromString($ofxContent)
}

/**
* Detect if the OFX file is on one line. If it is, add newlines automatically.
* Prepare OFX file contents.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you put a better description here of what's going on to make it clearer please? Something like:

Normalise newlines by removing and adding newlines only before opening tags

*
* @param string $ofxContent
* @return string
*/
private function conditionallyAddNewlines($ofxContent)
private function fixNewlines($ofxContent)
{
if (preg_match('/<OFX>.*<\/OFX>/', $ofxContent) === 1) {
return str_replace('<', "\n<", $ofxContent); // add line breaks to allow XML to parse
}

return $ofxContent;
// clear all new line characters first
$ofxContent = str_replace(["\r", "\n"], '', $ofxContent);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, seems unlikely, but possible that there might be other newlines we didn't intend to replace here (for example, maybe a transaction description has a newline that should be kept for whatever reason). We don't have an existing test, but this may be a BC break.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

understood

// add line breaks before opening tags only, to allow XML to parse
return preg_replace('/<[^\/!]/', "\n" . '$0', $ofxContent);
}

/**
Expand Down Expand Up @@ -94,17 +91,22 @@ private function xmlLoadString($xmlString)
*/
private function closeUnclosedXmlTags($line)
{
// Matches: <SOMETHING>blah
// Does not match: <SOMETHING>
// Does not match: <SOMETHING>blah</SOMETHING>
if (preg_match(
"/<([A-Za-z0-9.]+)>([\wà-úÀ-Ú0-9\.\-\_\+\, ;:\[\]\'\&\/\\\*\(\)\+\{\|\}\!\£\$\?=@€£#%±§~`]+)$/",
trim($line),
$matches
)) {
return "<{$matches[1]}>{$matches[2]}</{$matches[1]}>";
$line = trim($line);
$tag = ltrim(substr($line, 1, strpos($line, '>') - 1), '/');

// Line is "<SOMETHING>" or "</SOMETHING>"
if ($line == '<' . $tag . '>' || $line == '</' . $tag . '>') {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use === for equality checks

return $line;
}
return $line;

// Tag is properly closed
if (strpos($line, '</' . $tag . '>') !== false) {
return $line;
}

$lines = explode("\n", str_replace('</', "\n" . '</', $line));
$lines[0] = trim($lines[0]) . '</' . $tag .'>';
return implode('', $lines);
}

/**
Expand All @@ -115,15 +117,11 @@ private function closeUnclosedXmlTags($line)
*/
private function convertSgmlToXml($sgml)
{
$sgml = str_replace(["\r\n", "\r"], "\n", $sgml);

$lines = explode("\n", $sgml);

$xml = '';
foreach ($lines as $line) {
$xml .= trim($this->closeUnclosedXmlTags($line)) . "\n";
foreach (explode("\n", $sgml) as $line) {
$xml .= $this->closeUnclosedXmlTags($line) . "\n";
}

return trim($xml);
return rtrim($xml);
}
}
1 change: 1 addition & 0 deletions tests/OfxParser/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ public function loadFromStringProvider()
return [
'ofxdata.ofx' => [dirname(__DIR__).'/fixtures/ofxdata.ofx'],
'ofxdata-oneline.ofx' => [dirname(__DIR__).'/fixtures/ofxdata-oneline.ofx'],
'ofxdata-oneline-with-closing-tags.ofx' => [dirname(__DIR__).'/fixtures/ofxdata-oneline-with-closing-tags.ofx'],
'ofxdata-cmfr.ofx' => [dirname(__DIR__).'/fixtures/ofxdata-cmfr.ofx'],
'ofxdata-bb.ofx' => [dirname(__DIR__).'/fixtures/ofxdata-bb.ofx'],
'ofxdata-bb-two-stmtrs.ofx' => [dirname(__DIR__).'/fixtures/ofxdata-bb-two-stmtrs.ofx'],
Expand Down
10 changes: 10 additions & 0 deletions tests/fixtures/ofxdata-oneline-with-closing-tags.ofx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
OFXHEADER:100
DATA:OFXSGML
VERSION:102
SECURITY:NONE
ENCODING:USASCII
CHARSET:1252
COMPRESSION:NONE
OLDFILEUID:NONE
NEWFILEUID:NONE
<OFX><SIGNONMSGSRSV1><SONRS><STATUS><CODE>0</CODE><SEVERITY>INFO</SEVERITY></STATUS><DTSERVER>20160720101818<LANGUAGE>FRE</SONRS></SIGNONMSGSRSV1><BANKMSGSRSV1><STMTTRNRS><TRNUID>1469002698556<STATUS><CODE>0<SEVERITY>INFO</STATUS><CLTCOOKIE>1469002698556<STMTRS><CURDEF>EUR<BANKACCTFROM><BANKID>XXXXX<BRANCHID>XXXXX<ACCTID>XXXXXXXXXXX<ACCTTYPE>CHECKING<ACCTKEY>55</BANKACCTFROM><BANKTRANLIST><DTSTART>20160208<DTEND>20160720<STMTTRN><TRNTYPE>DEBIT<DTPOSTED>20160719<TRNAMT>-1.55<FITID>PU3YMMLLQ9<NAME>FRAIS TRIMESTRIELS</STMTTRN><STMTTRN><TRNTYPE>PAYMENT<DTPOSTED>20160503<TRNAMT>-383.00<FITID>PU3Y_VLC48<NAME>PRELEVEMENT</STMTTRN><STMTTRN><TRNTYPE>DEBIT<DTPOSTED>20160418<TRNAMT>-1.55<FITID>PU3YIVLLQ9<NAME>FRAIS</STMTTRN><STMTTRN><TRNTYPE>DIRECTDEP<DTPOSTED>20160411<TRNAMT>200.00<FITID>PU3Y5ALLCK<NAME>VIREMENT DE MR XXXX</STMTTRN><STMTTRN><TRNTYPE>DIRECTDEP<DTPOSTED>20160329<TRNAMT>-80.00<FITID>PU3YBALLL2<NAME>VIREMENT POUR</STMTTRN><STMTTRN><TRNTYPE>DIRECTDEP<DTPOSTED>20160321<TRNAMT>-600.00<FITID>PU3YWALLC4<NAME>VIREMENT</STMTTRN><STMTTRN><TRNTYPE>DIRECTDEP<DTPOSTED>20160216<TRNAMT>-1000.00<FITID>PU3Y36LCPF<NAME>VIREMENT</STMTTRN><STMTTRN><TRNTYPE>PAYMENT<DTPOSTED>20160216<TRNAMT>-111.00<FITID>PU3Y36LC4I<NAME>PRELEVEMENT</STMTTRN></BANKTRANLIST><LEDGERBAL><BALAMT>100.00<DTASOF>20160719</LEDGERBAL><AVAILBAL><BALAMT>100.00<DTASOF>20160719</AVAILBAL></STMTRS></STMTTRNRS></BANKMSGSRSV1></OFX>