From 095562a12e66d53eb4da6e4b77ff34674f960180 Mon Sep 17 00:00:00 2001 From: Mihail Date: Thu, 27 Aug 2015 13:26:27 +0300 Subject: [PATCH] csv parser - add fist line parametr --- backend/components/parsers/CsvParser.php | 60 ++++++++++++++++++++---------------------------------------- backend/components/parsers/ParserHandler.php | 28 +++++++++++++++++++++------- backend/controllers/ParserController.php | 2 +- 3 files changed, 42 insertions(+), 48 deletions(-) diff --git a/backend/components/parsers/CsvParser.php b/backend/components/parsers/CsvParser.php index 07e0335..e9d9100 100644 --- a/backend/components/parsers/CsvParser.php +++ b/backend/components/parsers/CsvParser.php @@ -13,41 +13,22 @@ use yii\base\ErrorException; class CsvParser implements \IteratorAggregate { - /** @var string */ - private $filePath; /** @var bool */ private $hasHeaderRow; - /** @var string */ - private $delimiter; - - /** @var string */ - private $enclosure; - - /** @var string */ - private $escape; - /** @var resource */ - private $handler; + private $file; /** @var out encoding charset */ private $out_charset = 'UTF-8'; /** @var out encoding charset */ - public $in_charset; + private $in_charset; /** @var out encoding charset */ - public $first_line; + private $first_line; - public function __construct($filePath, $first_line = 0,$hasHeaderRow = TRUE) - { - $this->in_charset = 'windows-1251'; - $this->filePath = $filePath; - $this->hasHeaderRow = $hasHeaderRow; - $this->first_line = $first_line; - - } // public function encodeFile( $in_charset, $out_charset, $filePath ){ // @@ -66,9 +47,18 @@ class CsvParser implements \IteratorAggregate { * @param string $escape * @return $this */ - public function setup($delimiter = ';', $enclosure = '"', $escape = '\\') + public function setup( $file, $first_line = 1, $hasHeaderRow = TRUE, $delimiter = ';') { - $this->handler->setCsvControl($delimiter); + + $this->file = $file; + + $this->file->setCsvControl($delimiter); + $this->file->setFlags(\SplFileObject::READ_CSV); + $this->file->seek( $this->first_line ); + + $this->first_line = $first_line; + $this->in_charset = 'windows-1251'; + $this->hasHeaderRow = $hasHeaderRow; } public function getIterator() @@ -94,8 +84,6 @@ class CsvParser implements \IteratorAggregate { { $return = []; - $this->openHandler(); - $line = 0; $keys = NULL; @@ -125,26 +113,18 @@ class CsvParser implements \IteratorAggregate { return $return; } - private function openHandler() - { - try { - $this->handler = new \SplFileObject( $this->filePath , 'r' );; - } catch (ErrorException $e) { - Yii::warning("Ошибка открытия файла {$this->filePath}"); - } - $this->handler->setFlags(\SplFileObject::READ_CSV);// | \SplFileObject::READ_AHEAD | \SplFileObject::SKIP_EMPTY | \SplFileObject::DROP_NEW_LINE); - $this->handler->fseek( $this->first_line ); - $this->setup(); - } + private function closeHandler() { - $this->handler = NULL; + $this->file = NULL; } private function readRow() { - $dirt_value_arr = $this->handler->fgetcsv( ); - return $this->encodeArray( $dirt_value_arr ); + $dirt_value_arr = $this->file->fgetcsv( ); + $encode_arr = $this->encodeArray( $dirt_value_arr ); + + return $encode_arr; } diff --git a/backend/components/parsers/ParserHandler.php b/backend/components/parsers/ParserHandler.php index dcf9969..d66836a 100644 --- a/backend/components/parsers/ParserHandler.php +++ b/backend/components/parsers/ParserHandler.php @@ -8,25 +8,39 @@ class ParserHandler { /** @var string */ private $filePath; - /** @var string */ + /** @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 $filePath parsing file path + * @param string first line in file for parsing */ - public function __construct( $filePath ) + public function __construct( $filePath, $first_line ) { $this->filePath = $filePath; - preg_match( '/\.[^\.]+$/i',$filePath, $resultArray ); - $this->extension = $resultArray[0]; + $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'){ + if ($this->extension = 'csv'){ - $csvParser = new CsvParser( $this->filePath ); + $csvParser = new CsvParser( ); + $csvParser->setup( $this->fileObject, $this->first_line ); return $csvParser->read(); }; } diff --git a/backend/controllers/ParserController.php b/backend/controllers/ParserController.php index c657db6..2cad596 100644 --- a/backend/controllers/ParserController.php +++ b/backend/controllers/ParserController.php @@ -63,7 +63,7 @@ class ParserController extends Controller $filePath = Yii::getAlias('@webroot') . '/uploads/' . $model->file->baseName . '.' . $model->file->extension; $model->file->saveAs( $filePath ); - $parser = new ParserHandler( $filePath ); + $parser = new ParserHandler( $filePath, 0 ); $data = $parser->run(); if( !is_array($data) ){ -- libgit2 0.21.4