ParserHandler.php 1.18 KB
<?php
namespace app\components\parsers;

use app\components\parsers\CsvParser;

class ParserHandler {

/** @var string */
    private $filePath;

    /** @var instance of SplFileObject */
    private $fileObject;

    /** @var string - extension of file $filePath */
    private $extension;

    /** @var string - extension of file $filePath */
    private $first_line;

    /**
     * @param string first line in file for parsing
     */
    public function __construct( $filePath, $first_line  )
    {
        $this->filePath = $filePath;
        $this->first_line = $first_line;

        try {
            $this->fileObject = new \SplFileObject( $this->filePath , 'r' );;
        } catch (ErrorException $e) {
            Yii::warning("Ошибка открытия файла {$this->filePath}");
        }

        //preg_match( '/\.[^\.]+$/i',$filePath, $resultArray );
        $this->extension = $this->fileObject->getExtension();
        $this->run();
    }

    public function run(){
        if ($this->extension = 'csv'){

            $csvParser = new CsvParser( );
            $csvParser->setup( $this->fileObject, $this->first_line );
            return $csvParser->read();
        };
    }
}