Commit d1fac7a9ab175babccfe47e9a448528b154db928
1 parent
7a3cd272
add parsing sheets to xslx parser
Showing
3 changed files
with
97 additions
and
19 deletions
Show diff stats
lib/CsvParser.php
| @@ -72,4 +72,8 @@ class CsvParser extends TableParser | @@ -72,4 +72,8 @@ class CsvParser extends TableParser | ||
| 72 | protected function isEmptyColumn( $val ){ | 72 | protected function isEmptyColumn( $val ){ |
| 73 | return $val == ''; | 73 | return $val == ''; |
| 74 | } | 74 | } |
| 75 | + | ||
| 76 | + protected function setResult( ){ | ||
| 77 | + $this->result[] = $this->row; | ||
| 78 | + } | ||
| 75 | } | 79 | } |
| 76 | \ No newline at end of file | 80 | \ No newline at end of file |
lib/TableParser.php
| @@ -53,6 +53,8 @@ abstract class TableParser extends Parser { | @@ -53,6 +53,8 @@ abstract class TableParser extends Parser { | ||
| 53 | 53 | ||
| 54 | protected abstract function readRow(); | 54 | protected abstract function readRow(); |
| 55 | 55 | ||
| 56 | + protected abstract function setResult(); | ||
| 57 | + | ||
| 56 | 58 | ||
| 57 | public function read() | 59 | public function read() |
| 58 | { | 60 | { |
| @@ -66,23 +68,26 @@ abstract class TableParser extends Parser { | @@ -66,23 +68,26 @@ abstract class TableParser extends Parser { | ||
| 66 | // прочтем строку из файла | 68 | // прочтем строку из файла |
| 67 | $this->readRow(); | 69 | $this->readRow(); |
| 68 | 70 | ||
| 71 | + // уберем пустые колонки из ряда | ||
| 72 | + $this->filterRow(); | ||
| 73 | + | ||
| 69 | if ( $this->isEmptyRow() ) { | 74 | if ( $this->isEmptyRow() ) { |
| 70 | //счетчик пустых строк | 75 | //счетчик пустых строк |
| 76 | + //CustomVarDamp::dump($this->current_row_number); | ||
| 71 | $empty_lines++; | 77 | $empty_lines++; |
| 72 | continue; | 78 | continue; |
| 73 | } | 79 | } |
| 74 | 80 | ||
| 75 | - // уберем пустые колонки из ряда | ||
| 76 | - $this->filterRow(); | ||
| 77 | - | ||
| 78 | - | ||
| 79 | $this->adjustRowToSettings( ); | 81 | $this->adjustRowToSettings( ); |
| 80 | 82 | ||
| 81 | // строка не пустая, имеем прочитанный массив значений | 83 | // строка не пустая, имеем прочитанный массив значений |
| 82 | $this->current_row_number++; | 84 | $this->current_row_number++; |
| 83 | 85 | ||
| 84 | // для первой строки утановим ключи из заголовка | 86 | // для первой строки утановим ключи из заголовка |
| 85 | - $this->setKeysFromHeader(); | 87 | + if ( !$this->setKeysFromHeader() ) { |
| 88 | + $this->setResult(); | ||
| 89 | + } | ||
| 90 | + | ||
| 86 | 91 | ||
| 87 | // если у нас установлен лимит, при его достижении прекращаем парсинг | 92 | // если у нас установлен лимит, при его достижении прекращаем парсинг |
| 88 | if ( $this->isLastLine() ) | 93 | if ( $this->isLastLine() ) |
| @@ -91,8 +96,7 @@ abstract class TableParser extends Parser { | @@ -91,8 +96,7 @@ abstract class TableParser extends Parser { | ||
| 91 | // обнуляем счетчик, так как считаюся пустые строки ПОДРЯД | 96 | // обнуляем счетчик, так как считаюся пустые строки ПОДРЯД |
| 92 | $empty_lines = 0; | 97 | $empty_lines = 0; |
| 93 | 98 | ||
| 94 | - $this->result[] = $this->row; | ||
| 95 | - $this->row = []; | 99 | + |
| 96 | 100 | ||
| 97 | } | 101 | } |
| 98 | 102 | ||
| @@ -129,7 +133,7 @@ abstract class TableParser extends Parser { | @@ -129,7 +133,7 @@ abstract class TableParser extends Parser { | ||
| 129 | if ( $this->keys !== NULL ) { | 133 | if ( $this->keys !== NULL ) { |
| 130 | 134 | ||
| 131 | if (count($this->keys) !== count($this->row)) { | 135 | if (count($this->keys) !== count($this->row)) { |
| 132 | - throw new \Exception("Ошибка парсинга файла в строке # {$this->current_row_number}. Не соответсвие числа ключевых колонок (заголовка) - числу колонок с данными", 0, 1, $this->file->getBasename(), $this->current_row_number); | 136 | + throw new \Exception("Ошибка парсинга файла в строке # {$this->current_row_number}. Не соответсвие числа ключевых колонок (заголовка) - числу колонок с данными"); |
| 133 | } | 137 | } |
| 134 | 138 | ||
| 135 | $this->row = array_combine($this->keys, $this->row); | 139 | $this->row = array_combine($this->keys, $this->row); |
| @@ -152,14 +156,22 @@ abstract class TableParser extends Parser { | @@ -152,14 +156,22 @@ abstract class TableParser extends Parser { | ||
| 152 | // в файле есть заголовок, но он еще не назначен - назначим | 156 | // в файле есть заголовок, но он еще не назначен - назначим |
| 153 | if ($this->keys === NULL) { | 157 | if ($this->keys === NULL) { |
| 154 | $this->keys = array_values( $this->row ); | 158 | $this->keys = array_values( $this->row ); |
| 159 | + return true; | ||
| 155 | } | 160 | } |
| 156 | } | 161 | } |
| 162 | + return false; | ||
| 157 | } | 163 | } |
| 158 | 164 | ||
| 159 | protected function filterRow(){ | 165 | protected function filterRow(){ |
| 166 | + // если есть заголовок или ключи - все значения нужны, не фильтруем | ||
| 167 | + if ( $this->has_header_row || $this->keys !== NULL ) { | ||
| 168 | + return; | ||
| 169 | + } | ||
| 170 | + // CustomVarDamp::dump( $this->row); | ||
| 160 | $this->row = array_filter( $this->row, function($val){ | 171 | $this->row = array_filter( $this->row, function($val){ |
| 161 | return !$this->isEmptyColumn($val); | 172 | return !$this->isEmptyColumn($val); |
| 162 | }); | 173 | }); |
| 174 | + //CustomVarDamp::dump( $this->row); | ||
| 163 | } | 175 | } |
| 164 | 176 | ||
| 165 | protected function isLastLine(){ | 177 | protected function isLastLine(){ |
lib/XlsxParser.php
| @@ -49,36 +49,53 @@ class XlsxParser extends TableParser { | @@ -49,36 +49,53 @@ class XlsxParser extends TableParser { | ||
| 49 | 49 | ||
| 50 | public function read() | 50 | public function read() |
| 51 | { | 51 | { |
| 52 | - | ||
| 53 | - // $this->extractFiles(); | 52 | + $this->extractFiles(); |
| 54 | 53 | ||
| 55 | $this->readSheets(); | 54 | $this->readSheets(); |
| 56 | $this->readStrings(); | 55 | $this->readStrings(); |
| 57 | 56 | ||
| 58 | foreach ( $this->sheets_arr as $sheet ) { | 57 | foreach ( $this->sheets_arr as $sheet ) { |
| 59 | //проходим по всем файлам из директории /xl/worksheets/ | 58 | //проходим по всем файлам из директории /xl/worksheets/ |
| 60 | - | 59 | + $this->current_sheet = $sheet; |
| 61 | $sheet_path = $this->path_for_extract_files . '/xl/worksheets/' . $sheet . '.xml'; | 60 | $sheet_path = $this->path_for_extract_files . '/xl/worksheets/' . $sheet . '.xml'; |
| 62 | if ( file_exists( $sheet_path ) && is_readable( $sheet_path ) ) { | 61 | if ( file_exists( $sheet_path ) && is_readable( $sheet_path ) ) { |
| 63 | 62 | ||
| 64 | $xml = simplexml_load_file( $sheet_path, "SimpleXMLIterator" ); | 63 | $xml = simplexml_load_file( $sheet_path, "SimpleXMLIterator" ); |
| 65 | $this->current_node = $xml->sheetData->row; | 64 | $this->current_node = $xml->sheetData->row; |
| 66 | $this->current_node->rewind(); | 65 | $this->current_node->rewind(); |
| 66 | + if ( $this->current_node->valid() ) { | ||
| 67 | 67 | ||
| 68 | - parent::read(); | 68 | + parent::read(); |
| 69 | + | ||
| 70 | + } | ||
| 69 | 71 | ||
| 70 | } | 72 | } |
| 71 | 73 | ||
| 72 | } | 74 | } |
| 73 | -CustomVarDamp::dumpAndDie($this->$result); | ||
| 74 | - // return $this->$result_arr; | 75 | + |
| 76 | + | ||
| 77 | + if ( $this->active_sheet ) { | ||
| 78 | + | ||
| 79 | + // в настройках указан конкретный лист с которогшо будем производить чтение, поэтому и возвращаем подмассив | ||
| 80 | + return $this->result[ $this->current_sheet ]; | ||
| 81 | + }else{ | ||
| 82 | + return $this->result; | ||
| 83 | + } | ||
| 84 | + | ||
| 75 | } | 85 | } |
| 76 | 86 | ||
| 77 | protected function extractFiles () | 87 | protected function extractFiles () |
| 78 | { | 88 | { |
| 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 | + | ||
| 79 | $zip = new \ZipArchive; | 96 | $zip = new \ZipArchive; |
| 80 | if ( $zip->open( $this->file->getPathname() ) === TRUE ) { | 97 | if ( $zip->open( $this->file->getPathname() ) === TRUE ) { |
| 81 | - $zip->extractTo( $this->path_for_extract_files ); | 98 | + $zip->extractTo( $this->path_for_extract_files . '/' ); |
| 82 | $zip->close(); | 99 | $zip->close(); |
| 83 | } else { | 100 | } else { |
| 84 | throw new \Exception( 'Ошибка чтения xlsx файла' ); | 101 | throw new \Exception( 'Ошибка чтения xlsx файла' ); |
| @@ -88,7 +105,7 @@ CustomVarDamp::dumpAndDie($this->$result); | @@ -88,7 +105,7 @@ CustomVarDamp::dumpAndDie($this->$result); | ||
| 88 | protected function readSheets () | 105 | protected function readSheets () |
| 89 | { | 106 | { |
| 90 | if ( $this->active_sheet ) { | 107 | if ( $this->active_sheet ) { |
| 91 | - $this->sheets_arr[ $this->active_sheet ] = 'Sheet' . $this->active_sheet; | 108 | + $this->sheets_arr[ ] = 'Sheet' . $this->active_sheet; |
| 92 | return; | 109 | return; |
| 93 | } | 110 | } |
| 94 | 111 | ||
| @@ -125,9 +142,14 @@ CustomVarDamp::dumpAndDie($this->$result); | @@ -125,9 +142,14 @@ CustomVarDamp::dumpAndDie($this->$result); | ||
| 125 | // protected function readRow ( $item, $sheet , $current_row ) | 142 | // protected function readRow ( $item, $sheet , $current_row ) |
| 126 | protected function readRow ( ) | 143 | protected function readRow ( ) |
| 127 | { | 144 | { |
| 145 | + $this->row = []; | ||
| 128 | $node = $this->current_node->getChildren(); | 146 | $node = $this->current_node->getChildren(); |
| 129 | - | ||
| 130 | - foreach ( $node as $child ) { | 147 | + if ($node === NULL) { |
| 148 | + return; | ||
| 149 | + } | ||
| 150 | + //foreach ( $node as $child ) { | ||
| 151 | + for ( $node->rewind(); $node->valid(); $node->next() ) { | ||
| 152 | + $child = $node->current(); | ||
| 131 | $attr = $child->attributes(); | 153 | $attr = $child->attributes(); |
| 132 | 154 | ||
| 133 | if( isset($child->v) ) { | 155 | if( isset($child->v) ) { |
| @@ -144,13 +166,20 @@ CustomVarDamp::dumpAndDie($this->$result); | @@ -144,13 +166,20 @@ CustomVarDamp::dumpAndDie($this->$result); | ||
| 144 | } | 166 | } |
| 145 | 167 | ||
| 146 | } | 168 | } |
| 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 | + } | ||
| 147 | $this->current_node->next(); | 176 | $this->current_node->next(); |
| 148 | - CustomVarDamp::dump($this->row); | ||
| 149 | } | 177 | } |
| 150 | 178 | ||
| 151 | protected function isEmptyRow(){ | 179 | protected function isEmptyRow(){ |
| 152 | 180 | ||
| 153 | $is_empty = false; | 181 | $is_empty = false; |
| 182 | + // CustomVarDamp::dump(count( $this->row ), $this->current_row_number); | ||
| 154 | 183 | ||
| 155 | if ( !count( $this->row ) || !$this->current_node->valid() ) { | 184 | if ( !count( $this->row ) || !$this->current_node->valid() ) { |
| 156 | return true; | 185 | return true; |
| @@ -175,4 +204,37 @@ CustomVarDamp::dumpAndDie($this->$result); | @@ -175,4 +204,37 @@ CustomVarDamp::dumpAndDie($this->$result); | ||
| 175 | protected function isEmptyColumn( $val ){ | 204 | protected function isEmptyColumn( $val ){ |
| 176 | return $val == ''; | 205 | return $val == ''; |
| 177 | } | 206 | } |
| 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 | + | ||
| 178 | } | 240 | } |
| 179 | \ No newline at end of file | 241 | \ No newline at end of file |