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

Commit

Permalink
Merge pull request #27 from asgrim/fix-banco-brasil-parse
Browse files Browse the repository at this point in the history
Detect whether newlines need adding to OFX before parsing
  • Loading branch information
asgrim authored Aug 23, 2016
2 parents f189aa5 + 3e70612 commit 8a6db19
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ $ composer require asgrim/ofxparser
You can access the nodes in your OFX file as follows:

```php
$ofxParser = new \OfxParser\Parser;
$ofxParser = new \OfxParser\Parser();
$ofx = $ofxParser->loadFromFile('/path/to/your/bankstatement.ofx');

$bankAccount = reset($ofx->bankAccounts);
Expand Down
17 changes: 16 additions & 1 deletion lib/OfxParser/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function loadFromFile($ofxFile)
public function loadFromString($ofxContent)
{
$ofxContent = utf8_encode($ofxContent);
$ofxContent = str_replace('<', "\n<", $ofxContent); // add line breaks to allow XML to parse
$ofxContent = $this->conditionallyAddNewlines($ofxContent);

$sgmlStart = stripos($ofxContent, '<OFX>');
$ofxSgml = trim(substr($ofxContent, $sgmlStart));
Expand All @@ -51,6 +51,21 @@ public function loadFromString($ofxContent)
return new Ofx($xml);
}

/**
* Detect if the OFX file is on one line. If it is, add newlines automatically.
*
* @param string $ofxContent
* @return string
*/
private function conditionallyAddNewlines($ofxContent)
{
if (preg_match('/<OFX>.*<\/OFX>/', $ofxContent) === 1) {
return str_replace('<', "\n<", $ofxContent); // add line breaks to allow XML to parse
}

return $ofxContent;
}

/**
* Load an XML string without PHP errors - throws exception instead
*
Expand Down
26 changes: 14 additions & 12 deletions tests/OfxParser/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function testXmlLoadStringLoadsValidXml()
/**
* @return array
*/
public function testCloseUnclosedXmlTagsProvider()
public function closeUnclosedXmlTagsProvider()
{
return [
['<SOMETHING>', '<SOMETHING>'],
Expand All @@ -68,7 +68,7 @@ public function testCloseUnclosedXmlTagsProvider()
}

/**
* @dataProvider testCloseUnclosedXmlTagsProvider
* @dataProvider closeUnclosedXmlTagsProvider
* @param $expected
* @param $input
*/
Expand All @@ -82,7 +82,7 @@ public function testCloseUnclosedXmlTags($expected, $input)
self::assertEquals($expected, $method->invoke($parser, $input));
}

public function testConvertSgmlToXmlProvider()
public function convertSgmlToXmlProvider()
{
return [
[<<<HERE
Expand Down Expand Up @@ -118,7 +118,7 @@ public function testConvertSgmlToXmlProvider()
}

/**
* @dataProvider testConvertSgmlToXmlProvider
* @dataProvider convertSgmlToXmlProvider
*/
public function testConvertSgmlToXml($sgml, $expected)
{
Expand All @@ -137,7 +137,7 @@ public function testLoadFromFileWhenFileDoesNotExist()
}

/**
* @dataProvider testLoadFromStringProvider
* @dataProvider loadFromStringProvider
*/
public function testLoadFromFileWhenFileDoesExist($filename)
{
Expand All @@ -156,20 +156,22 @@ public function testLoadFromFileWhenFileDoesExist($filename)
/**
* @return array
*/
public function testLoadFromStringProvider()
public function loadFromStringProvider()
{
return [
[dirname(__DIR__).'/fixtures/ofxdata.ofx'],
[dirname(__DIR__).'/fixtures/ofxdata-oneline.ofx'],
[dirname(__DIR__).'/fixtures/ofxdata-cmfr.ofx'],
[dirname(__DIR__).'/fixtures/ofxdata-credit-card.ofx'],
'ofxdata.ofx' => [dirname(__DIR__).'/fixtures/ofxdata.ofx'],
'ofxdata-oneline.ofx' => [dirname(__DIR__).'/fixtures/ofxdata-oneline.ofx'],
'ofxdata-cmfr.ofx' => [dirname(__DIR__).'/fixtures/ofxdata-cmfr.ofx'],
'ofxdata-bb.ofx' => [dirname(__DIR__).'/fixtures/ofxdata-bb.ofx'],
'ofxdata-credit-card.ofx' => [dirname(__DIR__).'/fixtures/ofxdata-credit-card.ofx'],
'ofxdata-bpbfc.ofx' => [dirname(__DIR__).'/fixtures/ofxdata-bpbfc.ofx'],
];
}

/**
* @param $filename
* @param string $filename
* @throws \Exception
* @dataProvider testLoadFromStringProvider
* @dataProvider loadFromStringProvider
*/
public function testLoadFromString($filename)
{
Expand Down
60 changes: 60 additions & 0 deletions tests/fixtures/ofxdata-bb.ofx
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
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>20151209
<LANGUAGE>POR
<DTACCTUP>20151209
<FI>
<ORG>Banco do Brasil S/A
<FID>001
</FI>
</SONRS>
</SIGNONMSGSRSV1>
<BANKMSGSRSV1>
<STMTTRNRS>
<TRNUID>0
<STATUS>
<CODE>0
<SEVERITY>INFO
</STATUS>
<STMTRS>
<CURDEF>BRL
<BANKACCTFROM>
<BANKID>001
<ACCTID>455000-5
<ACCTTYPE>CHECKING
</BANKACCTFROM>
<BANKTRANLIST>
<DTSTART>20151030
<DTEND>20151130
<STMTTRN>
<TRNTYPE>DEP
<DTPOSTED>20151103
<TRNAMT>239.55
<FITID>20151103023955
<CHECKNUM>252602
<MEMO>DOC CRÉDITO EM CONTA
</STMTTRN>
</BANKTRANLIST>
<LEDGERBAL>
<BALAMT>239.35
<DTASOF>20151209
</LEDGERBAL>
<MKTGINFO>Banco do Brasil, esse parceiro é todo seu.
</STMTRS>
</STMTTRNRS>
</BANKMSGSRSV1>
</OFX>

0 comments on commit 8a6db19

Please sign in to comment.