<commit_before><?php

namespace Stopsopa\UtilsBundle\Lib;

use Stopsopa\UtilsBundle\Lib\Standalone\UtilFilesystem;
use Iterator;

class CsvFileIterator implements Iterator {
    protected $file;
    protected $key = 0;
    protected $current;

    public function __construct($file) {
        UtilFilesystem::checkFile($file);
        $this->file = fopen($file, 'r');
    }

    public function __destruct() {
        fclose($this->file);
    }

    public function rewind() {
        rewind($this->file);
        $this->current = fgetcsv($this->file);
        $this->key = 0;
    }

    public function valid() {
        return !feof($this->file);
    }

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

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

    public function next() {
        $this->current = fgetcsv($this->file);
        $this->key++;
    }
}<commit_msg>CsvFileIterator fix<commit_after><?php

namespace Stopsopa\UtilsBundle\Lib;

use Stopsopa\UtilsBundle\Lib\Standalone\UtilFilesystem;
use Iterator;

/**
 *
for ($it->rewind(); $it->valid(); $it->next()) {
    echo $it->key().' - '.$it->current();
}
 */
class CsvFileIterator implements Iterator {
    protected $file;
    protected $key = 0;
    protected $current;
    protected $valid;
    protected $options;

    public function __construct($file, $options = array()) {
        UtilFilesystem::checkFile($file);
        $this->file = fopen($file, 'r');
        if (is_array($options)) {
            $this->options = array_merge(array(
                'length'    => 0,
                'delimiter' => ',',
                'enclosure' => '"',
                'escape'    => '\\',
                'skip'      => 0 // skip lines
            ), $options);
        }
    }

    public function __destruct() {
        fclose($this->file);
    }

    public function rewind() {
        rewind($this->file);
        $this->key = 0;

        if ($this->options['skip']) {
            for ( $i = 0 ; $i < $this->options['skip'] ; ++$i )
                $this->next();
        }

        $this->next();
    }

    public function next() {
        $this->valid    = !feof($this->file);
        $this->current  = fgetcsv(
            $this->file,
            $this->options['length'],
            $this->options['delimiter'],
            $this->options['enclosure'],
            $this->options['escape']
        );
        $this->key++;
    }

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

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

    public function current() {
        return $this->current;
    }
}
{'fraction_non_alphanumeric': 0.16350531107739, 'fraction_numerical': 0.002655538694992413, 'mean_word_length': 1.9595959595959596, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 46, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '3860748', 'n_tokens_mistral': 816, 'n_tokens_neox': 784, 'n_words': 159}