Completed
Push — master ( cb517e...d88e38 )
by Adelar
02:26
created

OfxParser::loadOfx()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
namespace Adelarcubs\OFXParser;
3
4
/**
5
 *
6
 * @author Adelar Tiemann Junior <[email protected]>
7
 */
8
class OfxParser
9
{
10
11
    /**
12
     *
13
     * @param mixed $ofx
14
     *            File or File Path
15
     * @return  Ofx
16
     */
17 5
    public static function loadOfx($ofx)
18
    {
19 5
        if (file_exists($ofx)) {
20 4
            $ofx = file_get_contents($ofx);
21 4
        }
22 5
        return OfxParser::loadFromString($ofx);
23
    }
24
25 5
    private static function loadFromString($ofxContent)
26
    {
27 5
        $xml = OfxParser::closeUnclosedXmlTags($ofxContent);
28 5
        libxml_clear_errors();
29 5
        libxml_use_internal_errors(true);
30 5
        $xml = simplexml_load_string($xml);
31 5
        $errors = libxml_get_errors();
32 5
        if (! empty($errors)) {
33 1
            throw new \Exception("Failed to parse OFX: " . var_export($errors, true));
34
        }
35 4
        return new Ofx($xml);
36
    }
37
38 5
    private static function closeUnclosedXmlTags($ofxContent)
39
    {
40 5
        $lines = OfxParser::ofxToPrepareArray($ofxContent);
41 5
        $xml = "";
42
43 5
        $lastClosedTag = '';
44 5
        foreach ($lines as $line) {
45 5
            if ($line != "") {
46 5
                $read = $line;
47 5
                $pos = strpos($read, ">");
48 5
                $tag = substr($read, 1, $pos);
49 5
                $value = trim(substr($read, $pos + 1));
50 5
                $line = '<' . $tag;
51 5
                if ($value != '') {
52 4
                    $line .= $value;
53 4
                    if (strpos($value, '</' . $tag) === false) {
54 4
                        $lastClosedTag = '/' . $tag;
55 4
                        $line .= '<' . $lastClosedTag;
56 4
                    }
57 4
                } else {
58 5
                    if ($lastClosedTag == $tag) {
59 1
                        $line = '';
60 1
                    }
61
                }
62 5
                $xml .= $line . "\n";
63 5
            }
64 5
        }
65 5
        return $xml;
66
    }
67
68 5
    private static function getOfxPart($ofxFileContent)
69
    {
70 5
        $sgmlStart = stripos($ofxFileContent, '<OFX>');
71 5
        return trim(substr($ofxFileContent, $sgmlStart));
72
    }
73
74 5
    private static function ofxToPrepareArray($ofx)
75
    {
76 5
        $ofxContent = OfxParser::getOfxPart($ofx);
77
78 5
        $xml = str_replace("\r", "", $ofxContent);
79 5
        $xml = str_replace("\n", "", $xml);
80 5
        $xml = str_replace("<", "\n<", $xml);
81
82 5
        return explode("\n", $xml);
83
    }
84
}
85