Skip to content

Commit

Permalink
Added .editorconfig to use space instead of tabs. Remove phpunit anno…
Browse files Browse the repository at this point in the history
…tation for expectedException.
  • Loading branch information
ambroisemaupate committed Aug 18, 2016
1 parent d523fd6 commit 606f709
Show file tree
Hide file tree
Showing 6 changed files with 212 additions and 197 deletions.
16 changes: 16 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# EditorConfig is awesome: http://EditorConfig.org

# top-most EditorConfig file
root = true

# Unix-style newlines with a newline ending every file
[*]
end_of_line = lf
insert_final_newline = true
indent_style = space
charset = utf-8

# 4 space indentation
[*.php]
indent_style = space
indent_size = 4
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"php": ">=5.4"
},
"require-dev": {
"phpunit/phpunit": "~4.1.3"
"phpunit/phpunit": "~5.5.2",
"squizlabs/php_codesniffer": "2.*"
},
"autoload": {
"psr-0": { "OfxParser": "lib/" }
Expand Down
204 changes: 102 additions & 102 deletions lib/OfxParser/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,106 +14,106 @@
class Parser
{

/**
* Load an OFX file into this parser by way of a filename
*
* @param string $ofxFile A path that can be loaded with file_get_contents
* @return Ofx
* @throws \InvalidArgumentException
*/
public function loadFromFile($ofxFile)
{
if (file_exists($ofxFile))
{
return $this->loadFromString(file_get_contents($ofxFile));
}
else
{
throw new \InvalidArgumentException("File '{$ofxFile}' could not be found");
}
}

/**
* Load an OFX by directly using the text content
*
* @param string $ofxContent
* @return Ofx
* @throws \Exception
*/
public function loadFromString($ofxContent)
{
$ofxContent = utf8_encode($ofxContent);
$ofxContent = str_replace("<", "\n<", $ofxContent); //add linebreaks to allow XML to parse
$sgmlStart = stripos($ofxContent, '<OFX>');
$ofxHeader = trim(substr($ofxContent, 0, $sgmlStart));
$ofxSgml = trim(substr($ofxContent, $sgmlStart));

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

$xml = $this->xmlLoadString($ofxXml);

return new Ofx($xml);
}

/**
* Load an XML string without PHP errors - throws exception instead
*
* @param string $xmlString
* @throws \Exception
* @return \SimpleXMLElement
*/
private function xmlLoadString($xmlString)
{
libxml_clear_errors();
libxml_use_internal_errors(true);
$xml = simplexml_load_string($xmlString);

if ($errors = libxml_get_errors())
{
throw new \Exception("Failed to parse OFX: " . var_export($errors, true));
}

return $xml;
}

/**
* Detect any unclosed XML tags - if they exist, close them
*
* @param string $line
* @return $line
*/
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]}>";
}
return $line;
}

/**
* Convert an SGML to an XML string
*
* @param string $sgml
* @return string
*/
private function convertSgmlToXml($sgml)
{
$sgml = str_replace("\r\n", "\n", $sgml);
$sgml = str_replace("\r", "\n", $sgml);

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

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

return trim($xml);
}
/**
* Load an OFX file into this parser by way of a filename
*
* @param string $ofxFile A path that can be loaded with file_get_contents
* @return Ofx
* @throws \InvalidArgumentException
*/
public function loadFromFile($ofxFile)
{
if (file_exists($ofxFile))
{
return $this->loadFromString(file_get_contents($ofxFile));
}
else
{
throw new \InvalidArgumentException("File '{$ofxFile}' could not be found");
}
}

/**
* Load an OFX by directly using the text content
*
* @param string $ofxContent
* @return Ofx
* @throws \Exception
*/
public function loadFromString($ofxContent)
{
$ofxContent = utf8_encode($ofxContent);
$ofxContent = str_replace("<", "\n<", $ofxContent); //add linebreaks to allow XML to parse

$sgmlStart = stripos($ofxContent, '<OFX>');
$ofxHeader = trim(substr($ofxContent, 0, $sgmlStart));
$ofxSgml = trim(substr($ofxContent, $sgmlStart));

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

$xml = $this->xmlLoadString($ofxXml);

return new Ofx($xml);
}

/**
* Load an XML string without PHP errors - throws exception instead
*
* @param string $xmlString
* @throws \Exception
* @return \SimpleXMLElement
*/
private function xmlLoadString($xmlString)
{
libxml_clear_errors();
libxml_use_internal_errors(true);
$xml = simplexml_load_string($xmlString);

if ($errors = libxml_get_errors())
{
throw new \Exception("Failed to parse OFX: " . var_export($errors, true));
}

return $xml;
}

/**
* Detect any unclosed XML tags - if they exist, close them
*
* @param string $line
* @return $line
*/
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]}>";
}
return $line;
}

/**
* Convert an SGML to an XML string
*
* @param string $sgml
* @return string
*/
private function convertSgmlToXml($sgml)
{
$sgml = str_replace("\r\n", "\n", $sgml);
$sgml = str_replace("\r", "\n", $sgml);

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

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

return trim($xml);
}
}
6 changes: 2 additions & 4 deletions tests/OfxParser/OfxTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,10 @@ public function setUp()
$this->ofxdata = simplexml_load_string( file_get_contents($ofxFile) );
}

/**
* @expectedException PHPUnit_Framework_Error
* @expectedExceptionMessage Argument 1 passed to OfxParser\Ofx::__construct() must be an instance of SimpleXMLElement, string given
*/
public function testAcceptOnlySimpleXMLElement()
{
$this->expectException('\TypeError');
$this->expectExceptionMessage('Argument 1 passed to OfxParser\Ofx::__construct() must be an instance of SimpleXMLElement, string given');
new Ofx('This is not an SimpleXMLObject');
}

Expand Down
Loading

0 comments on commit 606f709

Please sign in to comment.