Spamworldpro Mini Shell
Spamworldpro


Server : Apache
System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64
User : corals ( 1002)
PHP Version : 7.4.33
Disable Function : exec,passthru,shell_exec,system
Directory :  /home/corals/old/app/code/Soon/DataSync/Model/Data/Type/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /home/corals/old/app/code/Soon/DataSync/Model/Data/Type/Csv.php
<?php
/**
 * @license http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 * @author Hervé Guétin <[email protected]> <@herveguetin>
 * @copyright Copyright (c) 2017 Agence Soon (http://www.agence-soon.fr)
 */

namespace Soon\DataSync\Model\Data\Type;

class Csv extends DataTypeAbstract
{
    /**
     * @var bool
     */
    public $withHeader = true;
    /**
     * @var bool
     */
    public $padHeaders = true;
    /**
     * @var string
     */
    public $textDelimiter = '"';
    /**
     * @var string
     */
    public $fieldDelimiter = ',';
    /**
     * @var string
     */
    public $eol = "\n";
    /**
     * @var array
     */
    private $data;
    /**
     * @var array
     */
    private $parsedRows;
    /**
     * @var string
     */
    private $convertString;
    /**
     * @var array
     */
    private $convertHeaders;

    /**
     * @param string $string
     * @return array
     */
    public function parse(string $string): array
    {
        $this->data = explode($this->eol, trim($string));
        $this->parseRows();

        if ($this->withHeader) {
            $array = $this->parseHeader();
        } else {
            $array = $this->parsedRows;
        }

        return $array;
    }

    private function parseRows()
    {
        $this->parsedRows = array_map(
            'str_getcsv',
            $this->data,
            array_fill(0, count($this->data), $this->fieldDelimiter),
            array_fill(0, count($this->data), $this->textDelimiter)
        );
    }

    /**
     * @return array
     */
    private function parseHeader(): array
    {
        $header = array_shift($this->parsedRows);
        $headerKey = md5(implode('', $header));
        $csvArr = [];
        foreach ($this->parsedRows as $row) {
            if (md5(implode('', $row)) === $headerKey) {
                // This is to avoid repetition of header
                continue;
            }
            $csvArr[] = array_combine($header, $row);
        }

        return $csvArr;
    }

    /**
     * @param array $array
     * @return string
     */
    public function convert(array $array): string
    {
        $this->data = $array;
        $this->convertString = '';

        if (!empty($this->data)) {
            $this->convertHeaders();
            if (empty($this->convertHeaders)) {
                $this->convertString .= $this->convertRow($array);
            } else {
                $this->convertRowWithHeaders($array);
            }
        }

        return $this->convertString;
    }

    private function convertHeaders()
    {
        $headers = [];
        if ($this->padHeaders) {
            if (is_array(reset($this->data))) {
                foreach ($this->data as $row) {
                    foreach ($row as $header => $value) {
                        $headers[$header] = $header;
                    }
                }
            } else {
                $keys = array_keys($this->data);
                if (!is_int($keys[0])) {
                    $headers = $keys;
                }
            }
        }

        $this->convertHeaders = $headers;
    }

    /**
     * @param $array
     * @return string
     */
    private function convertRow($array)
    {
        $csvRow = [];
        $row = '';
        if (empty($array)) {
            $csvRow[] = $this->textDelimiter . $this->textDelimiter;
        }
        foreach ($array as $column) {
            (is_array($column))
                ? $row .= $this->convertRow($column)
                : $csvRow[] = $this->textDelimiter . str_replace('"', '""', $column) . $this->textDelimiter;
        }
        $row .= implode($this->fieldDelimiter, $csvRow);

        return $row . $this->eol;
    }

    /**
     * @param array $array
     */
    private function convertRowWithHeaders(array $array)
    {
        if ($this->withHeader) {
            $this->convertString .= $this->convertRow($this->convertHeaders);
        }

        foreach ($array as $row) {
            if (!is_array($row)) {
                $this->convertString .= $this->convertRow($array);
                break;
            }
            foreach ($this->convertHeaders as $header) {
                if (!isset($row[$header])) {
                    $row[$header] = '';
                }
            }
            $this->convertString .= $this->convertRow($row);
        }
    }
}

Spamworldpro Mini