Commit b46a9c6c9fd8ba483df3182a94ebd8332fa86f40

Authored by Mihail
1 parent 8cb5acbf

add examples for xml and data files

examples/ParserController.php
@@ -102,7 +102,16 @@ class ParserController extends Controller @@ -102,7 +102,16 @@ class ParserController extends Controller
102 'pageSize' => 10, 102 'pageSize' => 10,
103 ], 103 ],
104 ]); 104 ]);
  105 +
  106 + if ( empty( $data[0] ) ) {
  107 + // если нет первого ряла - это xml custom-файл с вложенными узлами, массив ассоциативный (дерево),
  108 + // такой массив нет возможности вывести с помощью GridView
  109 + // просто выведем его как есть
  110 + echo "<pre>";
  111 + return print_r($data);
  112 + }
105 // если отпарсенные данные - ассоциативный массив, то пользователю нечего выбирать 113 // если отпарсенные данные - ассоциативный массив, то пользователю нечего выбирать
  114 + // но выведем его в GridView
106 $assoc_data_arr = $this->is_assoc($data[0]); 115 $assoc_data_arr = $this->is_assoc($data[0]);
107 116
108 if ( $assoc_data_arr ) { 117 if ( $assoc_data_arr ) {
examples/_data/template.csv 0 → 100644
  1 +name;art;price;brand;count
  2 +some name 1;94783;03.Âĺđ;Autofrance;298.00
  3 +some name 2;BS-82001;23.31;Optimal;2.00
  4 +some name 3;94782;Ńĺđ.27;Dayco;3.00
  5 +some name 4;941019;13.88;Dayco;4.00
  6 +some name 5;94554;Ëčď.37;Dayco;2.00
  7 +some name 6;94785;×ĺđ.46;Dayco;17.00
  8 +some name 7;10233;Ëţň.18;Quick Brake;2.00
  9 +some name 8;94863;×ĺđ.54;Dayco;y
  10 +some name 9;94331;Ëčď.80;Dayco;2.00
  11 +some name 10;94862;09.Ňđŕ;Dayco;6.00
  12 +some name 11;94656;×ĺđ.72;Dayco;2.00
  13 +some name 12;94785;Ňđŕ.21;Total;10.00
  14 +some name 13;94786;Ňđŕ.36;Total;15.00
  15 +some name 14;94789;19.71;Total;6.00
  16 +some name 15;94754;24.21;Total;5.00
  17 +some name 16;94780;20.93;Total;13.00
examples/_data/template.xlsx 0 → 100644
No preview for this file type
examples/_data/template.xml 0 → 100644
  1 +<?xml version="1.0" encoding="UTF-8"?>
  2 +<Каталог><Товар><Код>0 003 218 174</Код><Наименование>Стартер Iveco E-1/E-2 -99</Наименование><ОЕ>0 001 218 174</ОЕ><Колво>1000</Колво><Ожидаемое>0</Ожидаемое><Розница>213.33</Розница><Город>Киев</Город><Производитель>Bosch</Производитель><Категория>89</Категория><Группа>Bosch</Группа></Товар><Товар><Код>0 120 488 253</Код><Наименование>Генератор Bosh</Наименование><ОЕ>98417134</ОЕ><Колво>0</Колво><Ожидаемое>0</Ожидаемое><Розница>169.30</Розница><Город>Киев</Город><Производитель>Bosch</Производитель><Категория>89</Категория><Группа>Bosch</Группа></Товар><Товар><Код>0 221 503 407</Код><Наименование>Катушка зажигания Brava 1.2 16v/N.Punto 1.2 16v</Наименование><ОЕ></ОЕ><Колво>0</Колво><Ожидаемое>0</Ожидаемое><Розница>182.00</Розница><Город>Киев</Город><Производитель>Bosch</Производитель><Категория></Категория><Группа>Bosch</Группа></Товар><Товар><Код>0 221 503 417</Код><Наименование>Катушка зажигания</Наименование><ОЕ></ОЕ><Колво>0</Колво><Ожидаемое>10</Ожидаемое><Розница>10.00</Розница><Город>Киев</Город><Производитель>Bosch</Производитель><Категория></Категория><Группа>Bosch</Группа></Товар></Каталог>
0 \ No newline at end of file 3 \ No newline at end of file
examples/config.php
@@ -46,7 +46,6 @@ return [ @@ -46,7 +46,6 @@ return [
46 'template' => 46 'template' =>
47 ['class' => 'yii\multiparser\XmlParser', 47 ['class' => 'yii\multiparser\XmlParser',
48 'node' => 'Товар', 48 'node' => 'Товар',
49 - 'has_header_row' => false,  
50 'keys' => [ 49 'keys' => [
51 "BRAND" => 'Производитель', 50 "BRAND" => 'Производитель',
52 "ARTICLE" => 'Код', 51 "ARTICLE" => 'Код',
@@ -59,14 +58,9 @@ return [ @@ -59,14 +58,9 @@ return [
59 'converter_conf' => [ 58 'converter_conf' => [
60 'class' => 'yii\multiparser\Converter', 59 'class' => 'yii\multiparser\Converter',
61 'configuration' => [ 60 'configuration' => [
62 - 'converter_conf' => [  
63 - 'class' => 'yii\multiparser\Converter',  
64 - 'configuration' => ["encode" => 'DESCR',  
65 "string" => ['DESCR', 'BRAND'], 61 "string" => ['DESCR', 'BRAND'],
66 "float" => 'PRICE', 62 "float" => 'PRICE',
67 "integer" => ['BOX', 'ADD_BOX'], 63 "integer" => ['BOX', 'ADD_BOX'],
68 - ],  
69 - ],  
70 ], 64 ],
71 ], 65 ],
72 ], 66 ],
@@ -82,17 +76,30 @@ return [ @@ -82,17 +76,30 @@ return [
82 ], 76 ],
83 ], 77 ],
84 'xlsx' => 78 'xlsx' =>
85 - ['web' =>  
86 - ['class' => 'common\components\parsers\XlsxParser',  
87 - // 'path_for_extract_files' => \Yii::getAlias('@temp_upload') . '/xlsx/',  
88 - //'auto_detect_first_line' => true,  
89 - //'has_header_row' => true,  
90 - 'active_sheet' => 1, 79 + ['custom' =>
  80 + ['class' => 'yii\multiparser\XlsxParser',
  81 + 'path_for_extract_files' => $_SERVER["DOCUMENT_ROOT"] . '/tests/_data/xlsx_tmp/',
91 'converter_conf' => [ 82 'converter_conf' => [
92 - 'class' => 'common\components\parsers\CustomConverter',  
93 - 'configuration' => ["string" => []], 83 + 'class' => 'yii\multiparser\Converter',
  84 + 'configuration' => ["encode" => []],
94 ] 85 ]
95 ], 86 ],
96 - ] 87 + 'template' =>
  88 + ['class' => 'yii\multiparser\XlsxParser',
  89 + 'path_for_extract_files' => $_SERVER["DOCUMENT_ROOT"] . 'tests/_data/xlsx_tmp',
  90 + 'keys' => [
  91 + 0 => 'Original',
  92 + 1 => 'Replacement',
  93 + ],
  94 + ],
  95 + 'basic_column' => [
  96 + Null => 'null',
  97 + "Description" => 'Название',
  98 + "Article" => 'Артикул',
  99 + "Price" => 'Цена',
  100 + "Brand" => 'Производитель',
  101 + "Count" => 'Количество',
  102 + ],
  103 + ],
97 ]; 104 ];
98 105
lib/ObjectCreator.php
@@ -15,7 +15,7 @@ class ObjectCreator { @@ -15,7 +15,7 @@ class ObjectCreator {
15 $class = trim( $configuration['class'] ); 15 $class = trim( $configuration['class'] );
16 unset( $configuration['class'] ); 16 unset( $configuration['class'] );
17 } else{ 17 } else{
18 - throw new \ErrorException('Error configuration - undefined class'); 18 + return null;
19 } 19 }
20 20
21 $object = new $class(); 21 $object = new $class();
@@ -10,8 +10,6 @@ namespace yii\multiparser; @@ -10,8 +10,6 @@ namespace yii\multiparser;
10 10
11 //@todo - заменить read на parse 11 //@todo - заменить read на parse
12 12
13 -use common\components\CustomVarDamp;  
14 -  
15 abstract class Parser 13 abstract class Parser
16 { 14 {
17 public $converter_conf = []; 15 public $converter_conf = [];
lib/XlsxParser.php
@@ -130,10 +130,14 @@ class XlsxParser extends TableParser @@ -130,10 +130,14 @@ class XlsxParser extends TableParser
130 130
131 protected function readStrings() 131 protected function readStrings()
132 { 132 {
133 - $xml = simplexml_load_file($this->path_for_extract_files . '/xl/sharedStrings.xml');  
134 - foreach ($xml->children() as $item) {  
135 - $this->strings_arr[] = (string)$item->t; 133 + $file_with_strings = $this->path_for_extract_files . '/xl/sharedStrings.xml';
  134 + if ( file_exists( $file_with_strings ) ) {
  135 + $xml = simplexml_load_file($file_with_strings);
  136 + foreach ($xml->children() as $item) {
  137 + $this->strings_arr[] = (string)$item->t;
  138 + }
136 } 139 }
  140 +
137 } 141 }
138 142
139 143
@@ -170,7 +174,12 @@ class XlsxParser extends TableParser @@ -170,7 +174,12 @@ class XlsxParser extends TableParser
170 174
171 if ( isset($attr['t']) ){ 175 if ( isset($attr['t']) ){
172 // it's not a value it's a string, so fetch it from string array 176 // it's not a value it's a string, so fetch it from string array
173 - $value = $this->strings_arr[$value]; 177 + if( empty( $this->strings_arr[$value] ) ){
  178 + $value = '';
  179 + } else {
  180 + $value = $this->strings_arr[$value];
  181 + }
  182 +
174 } else { 183 } else {
175 $value = (string)round( $value, $this->float_precision ); 184 $value = (string)round( $value, $this->float_precision );
176 } 185 }