Commit d1fac7a9ab175babccfe47e9a448528b154db928

Authored by Mihail
1 parent 7a3cd272

add parsing sheets to xslx parser

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