<commit_before><?php

namespace CleverAge\EAVManager\ImportBundle\Process;

use CleverAge\EAVManager\ProcessBundle\Process\ProcessInterface;

/**
 * @TODO describe class usage
 *
 * @todo : Use CsvFile and extend it
 */
class CsvWriterProcess implements ProcessInterface
{
    /** @var string */
    protected $filePath;

    /** @var bool */
    protected $forceEnclosures;

    /** @var array */
    protected $dataToProceed;

    /**
     * @param string $filePath
     * @param bool   $forceEnclosures
     */
    public function __construct($filePath, bool $forceEnclosures = false)
    {
        $this->filePath = $filePath;
        $this->forceEnclosures = $forceEnclosures;
    }

    /**
     * {@inheritdoc}
     */
    public function setInput($data)
    {
        if (!count($data)) {
            throw new \Exception('There is no data to export in a CSV file');
        }

        $this->dataToProceed = $data;
    }

    /**
     * {@inheritdoc}
     */
    public function execute()
    {
        $handler = fopen($this->filePath, 'w+');
        $headers = array_merge(...$this->dataToProceed); // Keep the headers as keys
        $headers = array_fill_keys(array_keys($headers), null); // Unset useless values
        $csvData = [];

        // Check input data (only strings are accepted)
        foreach ($this->dataToProceed as $item) {
            $csvData[] = array_filter(
                $item,
                function ($value) {
                    return is_string($value);
                }
            );
        }

        // Write CSV
        $this->fputcsv($handler, array_keys($headers), ';', '"');
        foreach ($csvData as $csvRow) {
            $dataRow = [];
            foreach (array_keys($headers) as $header) {
                $dataRow[] = $csvRow[$header] ?? '';
            }
            $this->fputcsv($handler, $dataRow, ';', '"', $this->forceEnclosures);
        }
        fclose($handler);
    }

    /**
     * {@inheritdoc}
     */
    public function getOutput()
    {
        // TODO: Implement getOutput() method.
    }

    /**<commit_msg>The CSV writer process should return the same data, for further processing<commit_after><?php

namespace CleverAge\EAVManager\ImportBundle\Process;

use CleverAge\EAVManager\ProcessBundle\Process\ProcessInterface;

/**
 * @TODO describe class usage
 *
 * @todo : Use CsvFile and extend it
 */
class CsvWriterProcess implements ProcessInterface
{
    /** @var string */
    protected $filePath;

    /** @var bool */
    protected $forceEnclosures;

    /** @var array */
    protected $dataToProceed;

    /**
     * @param string $filePath
     * @param bool   $forceEnclosures
     */
    public function __construct($filePath, bool $forceEnclosures = false)
    {
        $this->filePath = $filePath;
        $this->forceEnclosures = $forceEnclosures;
    }

    /**
     * {@inheritdoc}
     */
    public function setInput($data)
    {
        if (!count($data)) {
            throw new \Exception('There is no data to export in a CSV file');
        }

        $this->dataToProceed = $data;
    }

    /**
     * {@inheritdoc}
     */
    public function execute()
    {
        $handler = fopen($this->filePath, 'w+');
        $headers = array_merge(...$this->dataToProceed); // Keep the headers as keys
        $headers = array_fill_keys(array_keys($headers), null); // Unset useless values
        $csvData = [];

        // Check input data (only strings are accepted)
        foreach ($this->dataToProceed as $item) {
            $csvData[] = array_filter(
                $item,
                function ($value) {
                    return is_string($value);
                }
            );
        }

        // Write CSV
        $this->fputcsv($handler, array_keys($headers), ';', '"');
        foreach ($csvData as $csvRow) {
            $dataRow = [];
            foreach (array_keys($headers) as $header) {
                $dataRow[] = $csvRow[$header] ?? '';
            }
            $this->fputcsv($handler, $dataRow, ';', '"', $this->forceEnclosures);
        }
        fclose($handler);
    }

    /**
     * {@inheritdoc}
     */
    public function getOutput()
    {
        return $this->dataToProceed;
    }

    /**
{'fraction_non_alphanumeric': 0.1408351026185421, 'fraction_numerical': 0.0, 'mean_word_length': 1.9383229383229383, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 22, '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': '4811107', 'n_tokens_mistral': 1256, 'n_tokens_neox': 1219, 'n_words': 275}