From b46a9c6c9fd8ba483df3182a94ebd8332fa86f40 Mon Sep 17 00:00:00 2001 From: Mihail Date: Tue, 15 Dec 2015 16:22:40 +0200 Subject: [PATCH] add examples for xml and data files --- examples/ParserController.php | 9 +++++++++ examples/_data/template.csv | 17 +++++++++++++++++ examples/_data/template.xlsx | Bin 0 -> 7989 bytes examples/_data/template.xml | 2 ++ examples/config.php | 37 ++++++++++++++++++++++--------------- lib/ObjectCreator.php | 2 +- lib/Parser.php | 2 -- lib/XlsxParser.php | 17 +++++++++++++---- 8 files changed, 64 insertions(+), 22 deletions(-) create mode 100644 examples/_data/template.csv create mode 100644 examples/_data/template.xlsx create mode 100644 examples/_data/template.xml diff --git a/examples/ParserController.php b/examples/ParserController.php index 5a2114d..e78d9da 100644 --- a/examples/ParserController.php +++ b/examples/ParserController.php @@ -102,7 +102,16 @@ class ParserController extends Controller 'pageSize' => 10, ], ]); + + if ( empty( $data[0] ) ) { + // если нет первого ряла - это xml custom-файл с вложенными узлами, массив ассоциативный (дерево), + // такой массив нет возможности вывести с помощью GridView + // просто выведем его как есть + echo "
";
+            return print_r($data);
+        }
         // если отпарсенные данные - ассоциативный массив, то пользователю нечего выбирать
+        // но выведем его в GridView
         $assoc_data_arr = $this->is_assoc($data[0]);
 
         if ( $assoc_data_arr ) {
diff --git a/examples/_data/template.csv b/examples/_data/template.csv
new file mode 100644
index 0000000..a6a6258
--- /dev/null
+++ b/examples/_data/template.csv
@@ -0,0 +1,17 @@
+name;art;price;brand;count
+some name 1;94783;03.;Autofrance;298.00
+some name 2;BS-82001;23.31;Optimal;2.00
+some name 3;94782;.27;Dayco;3.00
+some name 4;941019;13.88;Dayco;4.00
+some name 5;94554;.37;Dayco;2.00
+some name 6;94785;.46;Dayco;17.00
+some name 7;10233;.18;Quick Brake;2.00
+some name 8;94863;.54;Dayco;y
+some name 9;94331;.80;Dayco;2.00
+some name 10;94862;09.;Dayco;6.00
+some name 11;94656;.72;Dayco;2.00
+some name 12;94785;.21;Total;10.00
+some name 13;94786;.36;Total;15.00
+some name 14;94789;19.71;Total;6.00
+some name 15;94754;24.21;Total;5.00
+some name 16;94780;20.93;Total;13.00
diff --git a/examples/_data/template.xlsx b/examples/_data/template.xlsx
new file mode 100644
index 0000000..3b31744
Binary files /dev/null and b/examples/_data/template.xlsx differ
diff --git a/examples/_data/template.xml b/examples/_data/template.xml
new file mode 100644
index 0000000..3b6b788
--- /dev/null
+++ b/examples/_data/template.xml
@@ -0,0 +1,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
\ No newline at end of file
diff --git a/examples/config.php b/examples/config.php
index a8e0bff..a00116c 100644
--- a/examples/config.php
+++ b/examples/config.php
@@ -46,7 +46,6 @@ return [
             'template' =>
                 ['class' => 'yii\multiparser\XmlParser',
                     'node' => 'Товар',
-                    'has_header_row' => false,
                     'keys' => [
                         "BRAND" => 'Производитель',
                         "ARTICLE" => 'Код',
@@ -59,14 +58,9 @@ return [
                     'converter_conf' => [
                         'class' => 'yii\multiparser\Converter',
                         'configuration' => [
-                            'converter_conf' => [
-                            'class' => 'yii\multiparser\Converter',
-                            'configuration' => ["encode" => 'DESCR',
                                 "string" => ['DESCR', 'BRAND'],
                                 "float" => 'PRICE',
                                 "integer" => ['BOX', 'ADD_BOX'],
-                            ],
-                        ],
                         ],
                     ],
                 ],
@@ -82,17 +76,30 @@ return [
             ],
         ],
     'xlsx' =>
-        ['web' =>
-            ['class' => 'common\components\parsers\XlsxParser',
-                //         'path_for_extract_files' => \Yii::getAlias('@temp_upload') . '/xlsx/',
-                //'auto_detect_first_line' => true,
-                //'has_header_row' => true,
-                'active_sheet' => 1,
+        ['custom' =>
+            ['class' => 'yii\multiparser\XlsxParser',
+                'path_for_extract_files' => $_SERVER["DOCUMENT_ROOT"] . '/tests/_data/xlsx_tmp/',
                 'converter_conf' => [
-                    'class' => 'common\components\parsers\CustomConverter',
-                    'configuration' => ["string" => []],
+                    'class' => 'yii\multiparser\Converter',
+                    'configuration' => ["encode" => []],
                 ]
             ],
-        ]
+            'template' =>
+                ['class' => 'yii\multiparser\XlsxParser',
+                    'path_for_extract_files' => $_SERVER["DOCUMENT_ROOT"] . 'tests/_data/xlsx_tmp',
+                    'keys' => [
+                        0 => 'Original',
+                        1 => 'Replacement',
+                    ],
+                 ],
+            'basic_column' => [
+                Null => 'null',
+                "Description" => 'Название',
+                "Article" => 'Артикул',
+                "Price" => 'Цена',
+                "Brand" => 'Производитель',
+                "Count" => 'Количество',
+            ],
+        ],
 ];
 
diff --git a/lib/ObjectCreator.php b/lib/ObjectCreator.php
index d87eb50..a34b5aa 100644
--- a/lib/ObjectCreator.php
+++ b/lib/ObjectCreator.php
@@ -15,7 +15,7 @@ class ObjectCreator {
             $class =  trim( $configuration['class'] );
             unset( $configuration['class'] );
         } else{
-            throw new \ErrorException('Error configuration - undefined class');
+            return null;
         }
 
         $object = new $class();
diff --git a/lib/Parser.php b/lib/Parser.php
index 27e35c1..3e0cbae 100644
--- a/lib/Parser.php
+++ b/lib/Parser.php
@@ -10,8 +10,6 @@ namespace yii\multiparser;
 
 //@todo - заменить read на parse
 
-use common\components\CustomVarDamp;
-
 abstract class Parser
 {
     public $converter_conf  = [];
diff --git a/lib/XlsxParser.php b/lib/XlsxParser.php
index 820779c..599182f 100644
--- a/lib/XlsxParser.php
+++ b/lib/XlsxParser.php
@@ -130,10 +130,14 @@ class XlsxParser extends TableParser
 
     protected function readStrings()
     {
-        $xml = simplexml_load_file($this->path_for_extract_files . '/xl/sharedStrings.xml');
-        foreach ($xml->children() as $item) {
-            $this->strings_arr[] = (string)$item->t;
+        $file_with_strings = $this->path_for_extract_files . '/xl/sharedStrings.xml';
+        if ( file_exists( $file_with_strings ) ) {
+            $xml = simplexml_load_file($file_with_strings);
+            foreach ($xml->children() as $item) {
+                $this->strings_arr[] = (string)$item->t;
+            }
         }
+
     }
 
 
@@ -170,7 +174,12 @@ class XlsxParser extends TableParser
 
                 if ( isset($attr['t']) ){
                     // it's not a value it's a string, so fetch it from string array
-                    $value = $this->strings_arr[$value];
+                    if( empty( $this->strings_arr[$value] ) ){
+                        $value = '';
+                    } else {
+                        $value = $this->strings_arr[$value];
+                    }
+
                 } else {
                     $value = (string)round( $value, $this->float_precision );
                 }
--
libgit2 0.21.4