Commit 42a252c2982dafc049790bbf01b03244153d5095
1 parent
d1fac7a9
fixed issue with header in xlsx parser
Showing
10 changed files
with
9 additions
and
96 deletions
Show diff stats
lib/CsvParser.php
| ... | ... | @@ -3,8 +3,6 @@ |
| 3 | 3 | |
| 4 | 4 | */ |
| 5 | 5 | namespace yii\multiparser; |
| 6 | -use common\components\CustomVarDamp; | |
| 7 | - | |
| 8 | 6 | |
| 9 | 7 | /** |
| 10 | 8 | * Class CsvParser |
| ... | ... | @@ -72,8 +70,4 @@ class CsvParser extends TableParser |
| 72 | 70 | protected function isEmptyColumn( $val ){ |
| 73 | 71 | return $val == ''; |
| 74 | 72 | } |
| 75 | - | |
| 76 | - protected function setResult( ){ | |
| 77 | - $this->result[] = $this->row; | |
| 78 | - } | |
| 79 | 73 | } |
| 80 | 74 | \ No newline at end of file | ... | ... |
lib/ObjectCreator.php
lib/Parser.php
lib/ParserHandler.php
lib/TableParser.php
lib/XlsxParser.php
| ... | ... | @@ -9,7 +9,6 @@ |
| 9 | 9 | namespace yii\multiparser; |
| 10 | 10 | |
| 11 | 11 | |
| 12 | -use common\components\CustomVarDamp; | |
| 13 | 12 | |
| 14 | 13 | |
| 15 | 14 | /** |
| ... | ... | @@ -49,53 +48,34 @@ class XlsxParser extends TableParser { |
| 49 | 48 | |
| 50 | 49 | public function read() |
| 51 | 50 | { |
| 52 | - $this->extractFiles(); | |
| 53 | 51 | |
| 52 | + $this->extractFiles(); | |
| 54 | 53 | $this->readSheets(); |
| 55 | 54 | $this->readStrings(); |
| 56 | 55 | |
| 57 | 56 | foreach ( $this->sheets_arr as $sheet ) { |
| 58 | 57 | //проходим по всем файлам из директории /xl/worksheets/ |
| 59 | - $this->current_sheet = $sheet; | |
| 58 | + | |
| 60 | 59 | $sheet_path = $this->path_for_extract_files . '/xl/worksheets/' . $sheet . '.xml'; |
| 61 | 60 | if ( file_exists( $sheet_path ) && is_readable( $sheet_path ) ) { |
| 62 | 61 | |
| 63 | 62 | $xml = simplexml_load_file( $sheet_path, "SimpleXMLIterator" ); |
| 64 | 63 | $this->current_node = $xml->sheetData->row; |
| 65 | 64 | $this->current_node->rewind(); |
| 66 | - if ( $this->current_node->valid() ) { | |
| 67 | 65 | |
| 68 | - parent::read(); | |
| 69 | - | |
| 70 | - } | |
| 66 | + parent::read(); | |
| 71 | 67 | |
| 72 | 68 | } |
| 73 | 69 | |
| 74 | 70 | } |
| 75 | - | |
| 76 | - | |
| 77 | - if ( $this->active_sheet ) { | |
| 78 | - | |
| 79 | - // в настройках указан конкретный лист с которогшо будем производить чтение, поэтому и возвращаем подмассив | |
| 80 | - return $this->result[ $this->current_sheet ]; | |
| 81 | - }else{ | |
| 82 | - return $this->result; | |
| 83 | - } | |
| 84 | - | |
| 71 | + return $this->$result; | |
| 85 | 72 | } |
| 86 | 73 | |
| 87 | 74 | protected function extractFiles () |
| 88 | 75 | { |
| 89 | - $this->path_for_extract_files = $this->path_for_extract_files . session_id(); | |
| 90 | - if ( !mkdir($this->path_for_extract_files) ) | |
| 91 | - { | |
| 92 | - throw new \Exception( 'Ошибка создания временного каталога - ' . $this->path_for_extract_files ); | |
| 93 | - } | |
| 94 | - | |
| 95 | - | |
| 96 | 76 | $zip = new \ZipArchive; |
| 97 | 77 | if ( $zip->open( $this->file->getPathname() ) === TRUE ) { |
| 98 | - $zip->extractTo( $this->path_for_extract_files . '/' ); | |
| 78 | + $zip->extractTo( $this->path_for_extract_files ); | |
| 99 | 79 | $zip->close(); |
| 100 | 80 | } else { |
| 101 | 81 | throw new \Exception( 'Ошибка чтения xlsx файла' ); |
| ... | ... | @@ -105,7 +85,7 @@ class XlsxParser extends TableParser { |
| 105 | 85 | protected function readSheets () |
| 106 | 86 | { |
| 107 | 87 | if ( $this->active_sheet ) { |
| 108 | - $this->sheets_arr[ ] = 'Sheet' . $this->active_sheet; | |
| 88 | + $this->sheets_arr[ $this->active_sheet ] = 'Sheet' . $this->active_sheet; | |
| 109 | 89 | return; |
| 110 | 90 | } |
| 111 | 91 | |
| ... | ... | @@ -138,18 +118,11 @@ class XlsxParser extends TableParser { |
| 138 | 118 | } |
| 139 | 119 | |
| 140 | 120 | |
| 141 | - | |
| 142 | - // protected function readRow ( $item, $sheet , $current_row ) | |
| 143 | 121 | protected function readRow ( ) |
| 144 | 122 | { |
| 145 | - $this->row = []; | |
| 146 | 123 | $node = $this->current_node->getChildren(); |
| 147 | - if ($node === NULL) { | |
| 148 | - return; | |
| 149 | - } | |
| 150 | - //foreach ( $node as $child ) { | |
| 151 | - for ( $node->rewind(); $node->valid(); $node->next() ) { | |
| 152 | - $child = $node->current(); | |
| 124 | + | |
| 125 | + foreach ( $node as $child ) { | |
| 153 | 126 | $attr = $child->attributes(); |
| 154 | 127 | |
| 155 | 128 | if( isset($child->v) ) { |
| ... | ... | @@ -166,20 +139,12 @@ class XlsxParser extends TableParser { |
| 166 | 139 | } |
| 167 | 140 | |
| 168 | 141 | } |
| 169 | - // дополним ряд пустыми значениями если у нас ключей больше чем значений | |
| 170 | - if ( $this->has_header_row && ( count( $this->keys ) > count( $this->row ) ) ) { | |
| 171 | - $extra_coloumn = count( $this->keys ) - count( $this->row ); | |
| 172 | - for ( $i = 1; $i <= $extra_coloumn; $i++ ) { | |
| 173 | - $this->row[] = ''; | |
| 174 | - } | |
| 175 | - } | |
| 176 | 142 | $this->current_node->next(); |
| 177 | 143 | } |
| 178 | 144 | |
| 179 | 145 | protected function isEmptyRow(){ |
| 180 | 146 | |
| 181 | 147 | $is_empty = false; |
| 182 | - // CustomVarDamp::dump(count( $this->row ), $this->current_row_number); | |
| 183 | 148 | |
| 184 | 149 | if ( !count( $this->row ) || !$this->current_node->valid() ) { |
| 185 | 150 | return true; |
| ... | ... | @@ -188,7 +153,7 @@ class XlsxParser extends TableParser { |
| 188 | 153 | $j = 0; |
| 189 | 154 | for ($i = 1; $i <= count( $this->row ); $i++) { |
| 190 | 155 | |
| 191 | - if ( $this->isEmptyColumn( $this->row[$i - 1] ) ) { | |
| 156 | + if ( isset($this->row[$i - 1]) && $this->isEmptyColumn( $this->row[$i - 1] ) ) { | |
| 192 | 157 | $j++; |
| 193 | 158 | } |
| 194 | 159 | |
| ... | ... | @@ -204,37 +169,4 @@ class XlsxParser extends TableParser { |
| 204 | 169 | protected function isEmptyColumn( $val ){ |
| 205 | 170 | return $val == ''; |
| 206 | 171 | } |
| 207 | - | |
| 208 | - protected function setResult( ){ | |
| 209 | - $this->result[ $this->current_sheet ][] = $this->row; | |
| 210 | - } | |
| 211 | - | |
| 212 | - protected function deleteExtractFiles () | |
| 213 | - { | |
| 214 | - $this->removeDir( $this->path_for_extract_files ); | |
| 215 | - | |
| 216 | - } | |
| 217 | - | |
| 218 | - protected function removeDir($dir) { | |
| 219 | - if (is_dir($dir)) { | |
| 220 | - $objects = scandir($dir); | |
| 221 | - foreach ($objects as $object) { | |
| 222 | - if ($object != "." && $object != "..") { | |
| 223 | - if (filetype($dir."/".$object) == "dir") | |
| 224 | - $this->removeDir($dir."/".$object); | |
| 225 | - else | |
| 226 | - unlink($dir."/".$object); | |
| 227 | - } | |
| 228 | - } | |
| 229 | - reset($objects); | |
| 230 | - rmdir($dir); | |
| 231 | - } | |
| 232 | - } | |
| 233 | - | |
| 234 | - function __destruct() | |
| 235 | - { | |
| 236 | - $this->deleteExtractFiles(); | |
| 237 | - } | |
| 238 | - | |
| 239 | - | |
| 240 | 172 | } |
| 241 | 173 | \ No newline at end of file | ... | ... |
lib/XmlParser.php
lib/YiiConverter.php
lib/YiiMultiparser.php