<commit_before><?php

namespace TopicCards\Utils;

use TopicCards\Interfaces\TopicMapInterface;


class XtmReader implements \Iterator
{
    /** @var TopicMapInterface */
    protected $topicmap;
    
    protected $filename;
    
    /** @var \XMLReader */
    protected $xmlreader;
    
    protected $importer;
    protected $cnt;
    
    
    public function __construct($filename, TopicMapInterface $topicmap)
    {
        $this->filename = $filename;
        $this->topicmap = $topicmap;
        
        $this->xmlreader = new \XMLReader();
        $this->importer = new XtmImport($topicmap);
        
        $this->cnt = -1;
    }


    public function rewind()
    {
        $ok = $this->xmlreader->open($this->filename);

        // Go to the root node

        if ($ok >= 0)
            $ok = $this->xmlreader->read();

        if (! $ok)
            return;
            
        // Go to the first child node

        while (true)
        {
            $ok = $this->xmlreader->read();
            
            if (! $ok)
                return;
    
            if ($this->xmlreader->nodeType === \XMLReader::ELEMENT)
            {
                $this->cnt = 0;
                return;
            }
        }
    }
    
    
    public function current()
    {
        /** @var \DOMElement $node */
        
        $node = $this->xmlreader->expand();
        
        if ($node === false)
        {
            return false;
        }

        if ($node->nodeType !== XML_ELEMENT_NODE)
        {
            return false;
        }

        if ($node->tagName === 'topic')
        {
            return $this->importer->importTopic($node);
        }
        elseif ($node->tagName === 'association')
        {
            return $this->importer->importAssociation($node);
        }
        else
        {
            return false;
        }
    }
    
    
    public function key()
    {
        return $this->cnt;
    }
    
    
    public function next()
    {
        while (true)
        {
            $ok = $this->xmlreader->next();
            
            if (! $ok)
            {
                $this->cnt = -1;
                return;
            }
    
            if ($this->xmlreader->nodeType === \XMLReader::ELEMENT)
            {
                $this->cnt++;
                return;
            }
        }
        
        $this->cnt = -1;
    }
    
    
    public function valid()
    {
        return ($this->cnt >= 0);
    }
}
<commit_msg>Fixed error handling in XtmReader::rewind()<commit_after><?php

namespace TopicCards\Utils;

use TopicCards\Interfaces\TopicMapInterface;


class XtmReader implements \Iterator
{
    /** @var TopicMapInterface */
    protected $topicmap;
    
    protected $filename;
    
    /** @var \XMLReader */
    protected $xmlreader;
    
    protected $importer;
    protected $cnt;
    
    
    public function __construct($filename, TopicMapInterface $topicmap)
    {
        $this->filename = $filename;
        $this->topicmap = $topicmap;
        
        $this->xmlreader = new \XMLReader();
        $this->importer = new XtmImport($topicmap);
        
        $this->cnt = -1;
    }


    public function rewind()
    {
        if (! file_exists($this->filename))
            return;
        
        $ok = $this->xmlreader->open($this->filename);

        // Go to the root node

        if ($ok)
            $ok = $this->xmlreader->read();

        if (! $ok)
            return;
            
        // Go to the first child node

        while (true)
        {
            $ok = $this->xmlreader->read();
            
            if (! $ok)
                return;
    
            if ($this->xmlreader->nodeType === \XMLReader::ELEMENT)
            {
                $this->cnt = 0;
                return;
            }
        }
    }
    
    
    public function current()
    {
        /** @var \DOMElement $node */
        
        $node = $this->xmlreader->expand();
        
        if ($node === false)
        {
            return false;
        }

        if ($node->nodeType !== XML_ELEMENT_NODE)
        {
            return false;
        }

        if ($node->tagName === 'topic')
        {
            return $this->importer->importTopic($node);
        }
        elseif ($node->tagName === 'association')
        {
            return $this->importer->importAssociation($node);
        }
        else
        {
            return false;
        }
    }
    
    
    public function key()
    {
        return $this->cnt;
    }
    
    
    public function next()
    {
        while (true)
        {
            $ok = $this->xmlreader->next();
            
            if (! $ok)
            {
                $this->cnt = -1;
                return;
            }
    
            if ($this->xmlreader->nodeType === \XMLReader::ELEMENT)
            {
                $this->cnt++;
                return;
            }
        }
        
        $this->cnt = -1;
    }
    
    
    public function valid()
    {
        return ($this->cnt >= 0);
    }
}

{'fraction_non_alphanumeric': 0.13018569735282498, 'fraction_numerical': 0.002173054128802845, 'mean_word_length': 1.1517212069698257, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 73, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 18}, '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': '1169979', 'n_tokens_mistral': 1460, 'n_tokens_neox': 1422, 'n_words': 282}