This repository has been archived by the owner on Sep 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 103
Enhance OFX Parser #46
Open
beroso
wants to merge
3
commits into
asgrim:master
Choose a base branch
from
beroso:fix-ofx-parse
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
|
||
|
@@ -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. | ||
* | ||
* @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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
|
||
/** | ||
|
@@ -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 . '>') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use |
||
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); | ||
} | ||
|
||
/** | ||
|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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: