From 61323a646e6e68b44d6d9342a197d2aaedb1ac82 Mon Sep 17 00:00:00 2001 From: Mihail Date: Wed, 16 Dec 2015 15:10:48 +0200 Subject: [PATCH] redid read method for more flexible way to determine first row --- examples/UploadFileParsingForm.php | 2 +- examples/config.php | 13 +++++++++---- lib/TableParser.php | 45 +++++++++++++++++++++------------------------ 3 files changed, 31 insertions(+), 29 deletions(-) diff --git a/examples/UploadFileParsingForm.php b/examples/UploadFileParsingForm.php index ea64421..29845f2 100644 --- a/examples/UploadFileParsingForm.php +++ b/examples/UploadFileParsingForm.php @@ -58,7 +58,7 @@ JS; $data = Yii::$app->multiparser->parse( $this->file_path, $options ); if( !is_array( $data ) || count($data) == 0 ){ - throw new ErrorException("Reading error from file - {$this->file_path}"); + throw new ErrorException("Parser return empty array. Check file and configuration settings (config.php)"); } if( !$this->file_path && file_exists( $this->file_path ) ) diff --git a/examples/config.php b/examples/config.php index 2178d28..d39755c 100644 --- a/examples/config.php +++ b/examples/config.php @@ -86,10 +86,12 @@ return [ ], 'template' => ['class' => 'yii\multiparser\XlsxParser', + 'min_column_quantity' => 2, + 'active_sheet' => 1, 'path_for_extract_files' => $_SERVER["DOCUMENT_ROOT"] . '/tests/_data/xlsx_tmp/', 'keys' => [ - 0 => 'Original', - 1 => 'Replacement', + 1 => 'Original', + 2 => 'Replacement', ], ], 'basic_column' => [ @@ -107,6 +109,7 @@ return [ ], 'template' => ['class' => 'yii\multiparser\XlsParser', + 'empty_lines_quantity' => 4, 'converter_conf' => [ 'class' => ' yii\multiparser\Converter', 'configuration' => ["encode" => [], @@ -128,6 +131,7 @@ return [ ['custom' => ['class' => 'yii\multiparser\CsvParser', 'delimiter' => "\t", + 'min_column_quantity' => 3, 'converter_conf' => [ 'class' => 'yii\multiparser\Converter', 'configuration' => ["encode" => []], @@ -135,16 +139,17 @@ return [ ], 'template' => ['class' => 'yii\multiparser\CsvParser', + 'min_column_quantity' => 3, 'delimiter' => "\t", 'keys' => [ 0 => 'Brand', - 1 => 'Article', + 1 => 'Description', 2 => 'Price', 4 => 'Count', ], 'converter_conf' => [ 'class' => 'yii\multiparser\Converter', - 'configuration' => ["encode" => 'Brand', + 'configuration' => ["encode" => [], "float" => 'Price', "integer" => 'Count' ] diff --git a/lib/TableParser.php b/lib/TableParser.php index 54d957e..ef8cc6a 100644 --- a/lib/TableParser.php +++ b/lib/TableParser.php @@ -64,42 +64,41 @@ abstract class TableParser extends Parser public function read() { - // получим первую значимую строку - $this->shiftToFirstValuableLine(); - - // первый проход, строка прочитана в shiftToFirstValuableLine + // первый проход $first_circle = true; + $this->current_row_number = 1; // будем считать количество пустых строк подряд - при достижении $empty_lines_quantity - считаем что это конец файла и выходим $empty_lines = 0; while ($empty_lines < $this->empty_lines_quantity) { - // прочтем строку из файла, если это не первый проход - if (!$first_circle){ - $this->readRow(); - } - - $first_circle = false; + $this->readRow(); + $this->current_row_number++; // уберем пустые колонки из ряда - if ($this->keys === NULL) { + if ( $this->keys === NULL ) { $this->filterRow(); } - if ($this->isEmptyRow()) { + if ( $this->isEmptyRow() ) { //счетчик пустых строк $empty_lines++; - $this->current_row_number++; continue; } + if ( $first_circle ) { + // при первом проходе нужно учесть настройки поп поиску первой строки + // такие как first_line и has_header_row + $this->shiftToFirstValuableLine(); + } + + $first_circle = false; + // запустим конвертирование $this->adjustRowToSettings(); // установим отпарсенную строку в итоговый массив результата $this->setResult(); - // строка не пустая, имеем прочитанный массив значений - $this->current_row_number++; // если у нас установлен лимит, при его достижении прекращаем парсинг if ($this->isLastLine()) @@ -111,23 +110,21 @@ abstract class TableParser extends Parser } /** - * определяет первую значимую строку, - * считывается файл пока в нем не встретится строка с непустыми колонками - * или пока не дойдет до first_line + * определяет первую значимую строку согласно first_line и has_header_row, + * считывается пока не дойдет до first_line * пропускает заголовок если он указан */ protected function shiftToFirstValuableLine() { - // читаем пока не встретим значимую строку, или пока не дойдем до first_line - do { - $this->current_row_number++; + // читаем пока не дойдем до first_line + while ( $this->first_line > $this->current_row_number ) { $this->readRow(); - } while ( $this->isEmptyRow() && ( $this->first_line < $this->current_row_number ) ); - + $this->current_row_number++; + } // если указан заголовок, то его мы тоже пропускаем (читаем далее) if( $this->has_header_row ) { - $this->current_row_number++; $this->readRow(); + $this->current_row_number++; } } -- libgit2 0.21.4