<commit_before><?php

/*
 * This file is part of YaEtl.
 *     (c) <NAME> / https://github.com/fab2s/YaEtl
 * This source file is licensed under the MIT license which you will
 * find in the LICENSE file or at https://opensource.org/licenses/MIT
 */

namespace fab2s\YaEtl\Loaders\File;

use fab2s\NodalFlow\NodalFlowException;
use fab2s\NodalFlow\YaEtlException;
use fab2s\YaEtl\Traits\CsvHandlerTrait;

/**
 * Class CsvLoader
 */
class CsvLoader extends FileLoaderAbstract
{
    use CsvHandlerTrait;

    /**
     * @var bool
     */
    protected $isFirstLine = true;

    /**
     * CsvLoader constructor.
     *
     * @param string      $destination
     * @param string|null $delimiter
     * @param string|null $enclosure
     * @param string|null $escape
     *
     * @throws NodalFlowException
     * @throws YaEtlException
     */
    public function __construct($destination, $delimiter = null, $enclosure = null, $escape = null)
    {
        parent::__construct($destination);
        $this->initCsvOptions($delimiter, $enclosure, $escape);
    }

    /**
     * Execute this Node
     *
     * @param array $param
     */
    public function exec($param)
    {
        $this->handleFirstLine($param)->writeCsvLine($param);
    }

    /**
     * @param array $param
     *
     * @return $this
     */
    protected function handleFirstLine($param)
    {
        if ($this->isFirstLine) {
            if ($this->useBom && ($bom = $this->prependBom(''))) {
                fwrite($this->handle, $bom);
            }

            if ($this->useSep) {
                fwrite($this->handle, "sep=$this->delimiter" . PHP_EOL);
            }

            if ($this->useHeader) {
                if (!isset($this->header)) {
                    $this->header = array_keys($param);
                }

                $this->writeCsvLine($this->header);
            }

            $this->isFirstLine = false;
        }

        return $this;
    }

    /**<commit_msg>refactor<commit_after><?php

/*
 * This file is part of YaEtl.
 *     (c) <NAME> / https://github.com/fab2s/YaEtl
 * This source file is licensed under the MIT license which you will
 * find in the LICENSE file or at https://opensource.org/licenses/MIT
 */

namespace fab2s\YaEtl\Loaders\File;

use fab2s\NodalFlow\NodalFlowException;
use fab2s\NodalFlow\YaEtlException;
use fab2s\YaEtl\Traits\CsvHandlerTrait;

/**
 * Class CsvLoader
 */
class CsvLoader extends FileLoaderAbstract
{
    use CsvHandlerTrait;

    /**
     * @var bool
     */
    protected $isFirstLine = true;

    /**
     * CsvLoader constructor.
     *
     * @param string      $destination
     * @param string|null $delimiter
     * @param string|null $enclosure
     * @param string|null $escape
     *
     * @throws NodalFlowException
     * @throws YaEtlException
     */
    public function __construct($destination, $delimiter = null, $enclosure = null, $escape = null)
    {
        parent::__construct($destination);
        $this->initCsvOptions($delimiter, $enclosure, $escape);
    }

    /**
     * Execute this Node
     *
     * @param array $param
     */
    public function exec($param)
    {
        $this->handleFirstLine($param)
            ->writeCsvLine($param);
    }

    /**
     * @param array $param
     *
     * @return $this
     */
    protected function handleFirstLine($param)
    {
        if ($this->isFirstLine) {
            $this->handleBom()
                ->handleSep()
                ->handleHeader($param);
            $this->isFirstLine = false;
        }

        return $this;
    }

    /**
     * @return $this
     */
    protected function handleBom()
    {
        if ($this->useBom && ($bom = $this->prependBom(''))) {
            fwrite($this->handle, $bom);
        }

        return $this;
    }

    /**
     * @return $this
     */
    protected function handleSep()
    {
        if ($this->useSep) {
            fwrite($this->handle, "sep=$this->delimiter" . PHP_EOL);
        }

        return $this;
    }

    /**
     * @param array $param
     *
     * @return $this
     */
    protected function handleHeader(array $param)
    {
        if ($this->useHeader) {
            if (!isset($this->header)) {
                $this->header = array_keys($param);
            }

            $this->writeCsvLine($this->header);
        }

        return $this;
    }

    /**
{'fraction_non_alphanumeric': 0.13564740808403744, 'fraction_numerical': 0.0022836263987211693, 'mean_word_length': 1.910299003322259, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 40, 'https://': 4, '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': '6863280', 'n_tokens_mistral': 1390, 'n_tokens_neox': 1343, 'n_words': 277}