<commit_before><?php

namespace Zebba\Component\Loader\Csv;

use Zebba\Component\Loader\Exception\ParseException;

/**
 * @author <NAME> <<EMAIL>>
 */
class Parser
{
    /**
     * @param string $input
     * @param string $delimiter
     * @param array $options
     * @throws ParseException
     * @return array
     */
    public function parse($input, $delimiter = ';', array $options = array())
    {
        $default_options = array(
            'encoding_source' => 'UTF-8',
            'encoding_target' => 'UTF-8',
        );

        $options = array_merge($default_options, $options);

        try {
        	$input = iconv($options['encoding_source'], $options['encoding_target'], $input);
        } catch (\Exception $e) {
        	throw new ParseException('The file might not be a CSV file after all.');
        }

        $lines = preg_split ('/$\R?^/m', $input);
        $lines = array_map('trim', $lines);

        $header = trim($lines[0]);
        unset($lines[0]);

        $keys = explode($delimiter, $header);

        $result = array();

        foreach ($lines as $line) {
            $values = str_getcsv($line, $delimiter);
            
            try {
                if (count($keys) !== count($values)) {
                    throw new ParseException(sprintf('The CSV-file is damaged. With %d columns defined in the header %d in the data-row are wrong: %s', count($keys), count($values), $line));
                }

                $result[] = array_combine($keys, $values);
            } catch (ParseException $e) {<commit_msg>Fixed problem when parsing binary files<commit_after><?php

namespace Zebba\Component\Loader\Csv;

use Zebba\Component\Loader\Exception\ParseException;

/**
 * @author <NAME> <<EMAIL>>
 */
class Parser
{
    /**
     * @param string $input
     * @param string $delimiter
     * @param array $options
     * @throws ParseException
     * @return array
     */
    public function parse($input, $delimiter = ';', array $options = array())
    {
        $default_options = array(
            'encoding_source' => 'UTF-8',
            'encoding_target' => 'UTF-8',
        );

        $options = array_merge($default_options, $options);

        try {
        	$input = iconv($options['encoding_source'], $options['encoding_target'], $input);
        } catch (\Exception $e) {
        	throw new ParseException('The file might not be a CSV file after all.');
        }

        $lines = preg_split ('/$\R?^/m', $input);
        $lines = array_map('trim', $lines);

        $header = trim($lines[0]);
        unset($lines[0]);

        $keys = explode($delimiter, $header);

        $result = array();

        foreach ($lines as $line) {
            $values = str_getcsv($line, $delimiter);
            
            try {
                if (count($keys) !== count($values)) {
                    throw new ParseException(sprintf('The CSV-file is damaged. With %d columns defined in the header %d the data is wrong.', count($keys), count($values)));
                }

                $result[] = array_combine($keys, $values);
            } catch (ParseException $e) {
{'fraction_non_alphanumeric': 0.15172855313700384, 'fraction_numerical': 0.002560819462227913, 'mean_word_length': 2.1725888324873095, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, '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': '3570812', 'n_tokens_mistral': 908, 'n_tokens_neox': 884, 'n_words': 230}