Commit 42a252c2982dafc049790bbf01b03244153d5095

Authored by Mihail
1 parent d1fac7a9

fixed issue with header in xlsx parser

@@ -3,8 +3,6 @@ @@ -3,8 +3,6 @@
3 3
4 */ 4 */
5 namespace yii\multiparser; 5 namespace yii\multiparser;
6 -use common\components\CustomVarDamp;  
7 -  
8 6
9 /** 7 /**
10 * Class CsvParser 8 * Class CsvParser
@@ -72,8 +70,4 @@ class CsvParser extends TableParser @@ -72,8 +70,4 @@ class CsvParser extends TableParser
72 protected function isEmptyColumn( $val ){ 70 protected function isEmptyColumn( $val ){
73 return $val == ''; 71 return $val == '';
74 } 72 }
75 -  
76 - protected function setResult( ){  
77 - $this->result[] = $this->row;  
78 - }  
79 } 73 }
80 \ No newline at end of file 74 \ No newline at end of file
lib/ObjectCreator.php
@@ -9,8 +9,6 @@ @@ -9,8 +9,6 @@
9 namespace yii\multiparser; 9 namespace yii\multiparser;
10 10
11 11
12 -use common\components\CustomVarDamp;  
13 -  
14 class ObjectCreator { 12 class ObjectCreator {
15 public static function build( array $configuration ){ 13 public static function build( array $configuration ){
16 if ( isset( $configuration['class'] ) ) { 14 if ( isset( $configuration['class'] ) ) {
@@ -12,8 +12,6 @@ namespace yii\multiparser; @@ -12,8 +12,6 @@ namespace yii\multiparser;
12 //@todo - xml - убрать из названий функций xml и array - это и так понятно 12 //@todo - xml - убрать из названий функций xml и array - это и так понятно
13 13
14 14
15 -use common\components\CustomVarDamp;  
16 -  
17 abstract class Parser 15 abstract class Parser
18 { 16 {
19 public $converter_conf = []; 17 public $converter_conf = [];
lib/ParserHandler.php
@@ -2,7 +2,6 @@ @@ -2,7 +2,6 @@
2 2
3 namespace yii\multiparser; 3 namespace yii\multiparser;
4 4
5 -use common\components\CustomVarDamp;  
6 5
7 class ParserHandler 6 class ParserHandler
8 { 7 {
lib/TableParser.php
@@ -9,8 +9,6 @@ @@ -9,8 +9,6 @@
9 namespace yii\multiparser; 9 namespace yii\multiparser;
10 10
11 11
12 -use common\components\CustomVarDamp;  
13 -  
14 abstract class TableParser extends Parser { 12 abstract class TableParser extends Parser {
15 13
16 14
lib/XlsxParser.php
@@ -9,7 +9,6 @@ @@ -9,7 +9,6 @@
9 namespace yii\multiparser; 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,53 +48,34 @@ class XlsxParser extends TableParser {
49 48
50 public function read() 49 public function read()
51 { 50 {
52 - $this->extractFiles();  
53 51
  52 + $this->extractFiles();
54 $this->readSheets(); 53 $this->readSheets();
55 $this->readStrings(); 54 $this->readStrings();
56 55
57 foreach ( $this->sheets_arr as $sheet ) { 56 foreach ( $this->sheets_arr as $sheet ) {
58 //проходим по всем файлам из директории /xl/worksheets/ 57 //проходим по всем файлам из директории /xl/worksheets/
59 - $this->current_sheet = $sheet; 58 +
60 $sheet_path = $this->path_for_extract_files . '/xl/worksheets/' . $sheet . '.xml'; 59 $sheet_path = $this->path_for_extract_files . '/xl/worksheets/' . $sheet . '.xml';
61 if ( file_exists( $sheet_path ) && is_readable( $sheet_path ) ) { 60 if ( file_exists( $sheet_path ) && is_readable( $sheet_path ) ) {
62 61
63 $xml = simplexml_load_file( $sheet_path, "SimpleXMLIterator" ); 62 $xml = simplexml_load_file( $sheet_path, "SimpleXMLIterator" );
64 $this->current_node = $xml->sheetData->row; 63 $this->current_node = $xml->sheetData->row;
65 $this->current_node->rewind(); 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 protected function extractFiles () 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 $zip = new \ZipArchive; 76 $zip = new \ZipArchive;
97 if ( $zip->open( $this->file->getPathname() ) === TRUE ) { 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 $zip->close(); 79 $zip->close();
100 } else { 80 } else {
101 throw new \Exception( 'Ошибка чтения xlsx файла' ); 81 throw new \Exception( 'Ошибка чтения xlsx файла' );
@@ -105,7 +85,7 @@ class XlsxParser extends TableParser { @@ -105,7 +85,7 @@ class XlsxParser extends TableParser {
105 protected function readSheets () 85 protected function readSheets ()
106 { 86 {
107 if ( $this->active_sheet ) { 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 return; 89 return;
110 } 90 }
111 91
@@ -138,18 +118,11 @@ class XlsxParser extends TableParser { @@ -138,18 +118,11 @@ class XlsxParser extends TableParser {
138 } 118 }
139 119
140 120
141 -  
142 - // protected function readRow ( $item, $sheet , $current_row )  
143 protected function readRow ( ) 121 protected function readRow ( )
144 { 122 {
145 - $this->row = [];  
146 $node = $this->current_node->getChildren(); 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 $attr = $child->attributes(); 126 $attr = $child->attributes();
154 127
155 if( isset($child->v) ) { 128 if( isset($child->v) ) {
@@ -166,20 +139,12 @@ class XlsxParser extends TableParser { @@ -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 $this->current_node->next(); 142 $this->current_node->next();
177 } 143 }
178 144
179 protected function isEmptyRow(){ 145 protected function isEmptyRow(){
180 146
181 $is_empty = false; 147 $is_empty = false;
182 - // CustomVarDamp::dump(count( $this->row ), $this->current_row_number);  
183 148
184 if ( !count( $this->row ) || !$this->current_node->valid() ) { 149 if ( !count( $this->row ) || !$this->current_node->valid() ) {
185 return true; 150 return true;
@@ -188,7 +153,7 @@ class XlsxParser extends TableParser { @@ -188,7 +153,7 @@ class XlsxParser extends TableParser {
188 $j = 0; 153 $j = 0;
189 for ($i = 1; $i <= count( $this->row ); $i++) { 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 $j++; 157 $j++;
193 } 158 }
194 159
@@ -204,37 +169,4 @@ class XlsxParser extends TableParser { @@ -204,37 +169,4 @@ class XlsxParser extends TableParser {
204 protected function isEmptyColumn( $val ){ 169 protected function isEmptyColumn( $val ){
205 return $val == ''; 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 \ No newline at end of file 173 \ No newline at end of file
@@ -9,9 +9,6 @@ @@ -9,9 +9,6 @@
9 namespace yii\multiparser; 9 namespace yii\multiparser;
10 10
11 11
12 -use common\components\CustomVarDamp;  
13 -use common\components\CustomArrayHelper;  
14 -  
15 class XmlParser extends Parser{ 12 class XmlParser extends Parser{
16 13
17 public $node; 14 public $node;
lib/YiiConverter.php
@@ -8,7 +8,6 @@ @@ -8,7 +8,6 @@
8 8
9 namespace yii\multiparser; 9 namespace yii\multiparser;
10 10
11 -use common\components\CustomVarDamp;  
12 use yii\base\Component; 11 use yii\base\Component;
13 use yii\base\ErrorException; 12 use yii\base\ErrorException;
14 13
lib/YiiMultiparser.php
@@ -8,7 +8,6 @@ @@ -8,7 +8,6 @@
8 8
9 namespace yii\multiparser; 9 namespace yii\multiparser;
10 10
11 -use common\components\CustomVarDamp;  
12 use yii\base\Component; 11 use yii\base\Component;
13 12
14 13
lib/YiiParserHandler.php
@@ -9,7 +9,6 @@ @@ -9,7 +9,6 @@
9 namespace yii\multiparser; 9 namespace yii\multiparser;
10 10
11 11
12 -use common\components\CustomVarDamp;  
13 12
14 class YiiParserHandler extends ParserHandler{ 13 class YiiParserHandler extends ParserHandler{
15 14