CsvParser.php 1.41 KB
<?php
/**

 */
namespace common\components\parsers;

/**
 * Class CsvParser
 * @package yii\multiparser
 * @todo - перевести на анг. яз.
 */
class CsvParser extends TableParser
{
    /** @var string - разделитель csv */
    public $delimiter = ';';



    /**
     * метод устанавливает настройки конвертера
     */
    public function setup()
    {
        parent::setup();

    }

    public function read()
    {
        parent::read();

        $this->cleanUp();

        return $this->result;
    }


    protected function readRow(  )
    {
        $this->row = fgetcsv( $this->file, 0, $this->delimiter );
    }

    protected  function isEmptyRow(){

        $is_empty = false;

        if ($this->row === false || $this->row === NULL ) {
            return true;
        }

        $j = 0;
        for ($i = 1; $i <= count( $this->row ); $i++) {

            if ( !isset( $this->row[ $i - 1 ] ) ) {
                continue;
            }

            if ( $this->isEmptyColumn( $this->row[$i - 1] ) ) {
                $j++;
            }

            if ( $j >= $this->min_column_quantity ) {
                $is_empty = true;
                break;
            }
        }

        return $is_empty;
    }

    protected  function isEmptyColumn( $val ){
        return $val == '';
    }

    protected  function setResult(  ){
        $this->result[] = $this->row;
    }
}