<commit_before><?php
namespace Adelarcubs\OFXParser;

class OfxParser
{

    private $ofx;

    /**
     *
     * @param mixed $ofx
     *            File or File Path
     */
    public function __construct($ofx)
    {
        if (file_exists($ofx)) {
            $ofx = file_get_contents($ofx);
        }
        return $this->loadFromString($ofx);
    }

    public function getOfx()
    {
        return $this->ofx;
    }

    private function loadFromString($ofxContent)
    {
        $xml = $this->closeUnclosedXmlTags($ofxContent);
        libxml_clear_errors();
        libxml_use_internal_errors(true);
        $xml = simplexml_load_string($xml);
        $errors = libxml_get_errors();
        if (! empty($errors)) {
            throw new \Exception("Failed to parse OFX: " . var_export($errors, true));
        }
        $this->ofx = new Ofx($xml);
        return $this->ofx;
    }

    private function closeUnclosedXmlTags($ofxContent)
    {
        $lines = $this->ofxToPrepareArray($ofxContent);
        $xml = "";
        
        $lastClosedTag = '';
        foreach ($lines as $line) {
            if ($line != "") {
                $read = $line;
                $pos = strpos($read, ">");
                $tag = substr($read, 1, $pos);
                $value = trim(substr($read, $pos + 1));
                $line = '<' . $tag;
                if ($value != '') {
                    $line .= $value;
                    if (strpos($value, '</' . $tag) === false) {
                        $lastClosedTag = '/' . $tag;
                        $line .= '<' . $lastClosedTag;
                    }
                } else {
                    if ($lastClosedTag == $tag) {
                        $line = '';
                    }
                }
                $xml .= $line . "\n";
            }
        }
        return $xml;
    }

    private function getOfxPart($ofxFileContent)
    {
        $sgmlStart = stripos($ofxFileContent, '<OFX>');
        return trim(substr($ofxFileContent, $sgmlStart));
    }

    private function ofxToPrepareArray($ofx)
    {
        $ofxContent = $this->getOfxPart($ofx);
        
        $xml = str_replace("\r", "", $ofxContent);
        $xml = str_replace("\n", "", $xml);
        $xml = str_replace("<", "\n<", $xml);
        
        return explode("\n", $xml);
    }
}<commit_msg>Mudando a forma de criar o arquivo<commit_after><?php
namespace Adelarcubs\OFXParser;

/**
 *
 * @author <NAME> <<EMAIL>>
 */
class OfxParser
{

    /**
     *
     * @param mixed $ofx
     *            File or File Path
     * @return  Ofx
     */
    public static function loadOfx($ofx)
    {
        if (file_exists($ofx)) {
            $ofx = file_get_contents($ofx);
        }
        return $this->loadFromString($ofx);
    }

    private function loadFromString($ofxContent)
    {
        $xml = $this->closeUnclosedXmlTags($ofxContent);
        libxml_clear_errors();
        libxml_use_internal_errors(true);
        $xml = simplexml_load_string($xml);
        $errors = libxml_get_errors();
        if (! empty($errors)) {
            throw new \Exception("Failed to parse OFX: " . var_export($errors, true));
        }
        return new Ofx($xml);
    }

    private function closeUnclosedXmlTags($ofxContent)
    {
        $lines = $this->ofxToPrepareArray($ofxContent);
        $xml = "";

        $lastClosedTag = '';
        foreach ($lines as $line) {
            if ($line != "") {
                $read = $line;
                $pos = strpos($read, ">");
                $tag = substr($read, 1, $pos);
                $value = trim(substr($read, $pos + 1));
                $line = '<' . $tag;
                if ($value != '') {
                    $line .= $value;
                    if (strpos($value, '</' . $tag) === false) {
                        $lastClosedTag = '/' . $tag;
                        $line .= '<' . $lastClosedTag;
                    }
                } else {
                    if ($lastClosedTag == $tag) {
                        $line = '';
                    }
                }
                $xml .= $line . "\n";
            }
        }
        return $xml;
    }

    private function getOfxPart($ofxFileContent)
    {
        $sgmlStart = stripos($ofxFileContent, '<OFX>');
        return trim(substr($ofxFileContent, $sgmlStart));
    }

    private function ofxToPrepareArray($ofx)
    {
        $ofxContent = $this->getOfxPart($ofx);

        $xml = str_replace("\r", "", $ofxContent);
        $xml = str_replace("\n", "", $xml);
        $xml = str_replace("<", "\n<", $xml);

        return explode("\n", $xml);
    }
}
{'fraction_non_alphanumeric': 0.1547104580812446, 'fraction_numerical': 0.000864304235090752, 'mean_word_length': 1.4754010695187165, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 34}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3052015', 'n_tokens_mistral': 1443, 'n_tokens_neox': 1375, 'n_words': 263}