Commit 74072a2ae8b9c25878eb6433f4833efeb7cbcec4
1 parent
474f35bf
add first version XML parser, add CSVconverter
Showing
16 changed files
with
451 additions
and
116 deletions
Show diff stats
backend/components/parsers/CustomCsvParser.php
@@ -14,20 +14,19 @@ class CustomCsvParser extends \yii\multiparser\CsvParser { | @@ -14,20 +14,19 @@ class CustomCsvParser extends \yii\multiparser\CsvParser { | ||
14 | //public $last_line = 10; | 14 | //public $last_line = 10; |
15 | //public $hasHeaderRow = true; | 15 | //public $hasHeaderRow = true; |
16 | // public $keys = ['first','second', 'third', 'forth', 'fifth']; | 16 | // public $keys = ['first','second', 'third', 'forth', 'fifth']; |
17 | - | ||
18 | - | ||
19 | - protected function readRow() | 17 | + public function setupConverter() |
20 | { | 18 | { |
21 | 19 | ||
22 | - $row = parent::readRow(); | ||
23 | 20 | ||
24 | - if (is_array($row)) { | ||
25 | - | ||
26 | - $row = Encoder::encodeArray( Encoder::$in_charset, Encoder::$out_charset, $row ); | 21 | + if ($this->hasHeaderRow) { |
22 | + // если у файла есть заголовок, то в результате имеем ассоциативный массив | ||
23 | + $this->converter_conf['hasKey'] = 1; | ||
27 | } | 24 | } |
28 | 25 | ||
29 | - return $row; | 26 | + $this->converter = \Yii::createObject($this->converter_conf); |
27 | + \common\components\CustomVarDamp::dumpAndDie($this); | ||
30 | 28 | ||
31 | } | 29 | } |
32 | 30 | ||
31 | + | ||
33 | } | 32 | } |
34 | \ No newline at end of file | 33 | \ No newline at end of file |
backend/components/parsers/CustomParserConfigurator.php deleted
1 | -<?php | ||
2 | - | ||
3 | -namespace backend\components\parsers; | ||
4 | -use yii\multiparser\ParserConfigurator; | ||
5 | - | ||
6 | -class CustomParserConfigurator extends ParserConfigurator | ||
7 | -{ | ||
8 | - | ||
9 | - protected static $configuration = [ | ||
10 | - 'csv' => | ||
11 | - ['web' => | ||
12 | - ['class' => 'backend\components\parsers\CustomCsvParser', | ||
13 | - 'auto_detect_first_line' => true,]]]; | ||
14 | - | ||
15 | - | ||
16 | - | ||
17 | - public static $basic_column = [ | ||
18 | - "BRAND" => 'Бренд', | ||
19 | - "ARTICLE"=> 'Артикул', | ||
20 | - "PRICE" => 'Цена', | ||
21 | - "DESCR" => 'Наименование', | ||
22 | - "BOX" => 'Колво', | ||
23 | - "ADD_BOX"=> 'В пути', | ||
24 | - "GROUP" => 'Группа RG' | ||
25 | - ]; | ||
26 | - | ||
27 | - | ||
28 | -} | ||
29 | \ No newline at end of file | 0 | \ No newline at end of file |
backend/components/parsers/ValueFilter.php deleted
1 | -<?php | ||
2 | -/** | ||
3 | - * Created by PhpStorm. | ||
4 | - * User: Cibermag | ||
5 | - * Date: 31.08.2015 | ||
6 | - * Time: 12:50 | ||
7 | - */ | ||
8 | - | ||
9 | -namespace backend\components\parsers; | ||
10 | - | ||
11 | -// класс который содержит преобразователи значений (фильтры) используемые при парсинге | ||
12 | -class ValueFilter | ||
13 | -{ | ||
14 | - | ||
15 | - public static function pricefilter($str) | ||
16 | - { | ||
17 | - if ($str == '') { | ||
18 | - $str = 0; | ||
19 | - } | ||
20 | - $str = trim(str_replace(",", ".", $str)); | ||
21 | - $str = preg_replace("/[^0-9.]+/", "", strtoupper($str)); | ||
22 | - | ||
23 | - if ($str == '') { | ||
24 | - return ''; | ||
25 | - } | ||
26 | - $str = round( (float)$str, 2 ); | ||
27 | - | ||
28 | - return $str; | ||
29 | - } | ||
30 | - | ||
31 | - public static function boxfilter($str) | ||
32 | - { | ||
33 | - if ($str == '') { | ||
34 | - $str = 0; | ||
35 | - } | ||
36 | - $str = trim(str_replace(",", ".", $str)); | ||
37 | - $str = preg_replace("/[^0-9.]+/", "", strtoupper($str)); | ||
38 | - if ($str == '') { | ||
39 | - return ''; | ||
40 | - } | ||
41 | - $str = round((int)$str, 2); | ||
42 | - | ||
43 | - return $str; | ||
44 | - } | ||
45 | -} | ||
46 | \ No newline at end of file | 0 | \ No newline at end of file |
1 | +<?php | ||
2 | + return [ | ||
3 | + 'csv' => | ||
4 | + ['web' => | ||
5 | + ['class' => 'backend\components\parsers\CustomCsvParser', | ||
6 | + 'auto_detect_first_line' => true, | ||
7 | + 'converter_conf' => ['class' => 'yii\multiparser\CsvConverter', | ||
8 | + 'configuration' => [ | ||
9 | + "string" => 'DESCR' | ||
10 | + ] | ||
11 | + ,]], | ||
12 | + | ||
13 | + 'basic_column' => [ | ||
14 | + "BRAND" => 'Бренд', | ||
15 | + "ARTICLE"=> 'Артикул', | ||
16 | + "PRICE" => 'Цена', | ||
17 | + "DESCR" => 'Наименование', | ||
18 | + "BOX" => 'Колво', | ||
19 | + "ADD_BOX"=> 'В пути', | ||
20 | + "GROUP" => 'Группа RG' | ||
21 | + ], | ||
22 | + ]]; | ||
23 | + | ||
24 | + | ||
25 | +//[ | ||
26 | +// "float" => 'PRICE', | ||
27 | +// "integer" => ['BOX' , 'ADD_BOX' ], | ||
28 | +// "prefix" => 'ARTICLE' | ||
29 | +//] | ||
0 | \ No newline at end of file | 30 | \ No newline at end of file |
backend/config/main.php
@@ -5,6 +5,7 @@ $params = array_merge( | @@ -5,6 +5,7 @@ $params = array_merge( | ||
5 | require(__DIR__ . '/params.php'), | 5 | require(__DIR__ . '/params.php'), |
6 | require(__DIR__ . '/params-local.php') | 6 | require(__DIR__ . '/params-local.php') |
7 | ); | 7 | ); |
8 | +$mp_configuration = require( __DIR__ . '/../components/parsers/config.php'); | ||
8 | 9 | ||
9 | return [ | 10 | return [ |
10 | 'id' => 'app-backend', | 11 | 'id' => 'app-backend', |
@@ -41,6 +42,7 @@ return [ | @@ -41,6 +42,7 @@ return [ | ||
41 | 'multiparser'=>[ | 42 | 'multiparser'=>[ |
42 | 43 | ||
43 | 'class' => 'yii\multiparser\YiiMultiparser', | 44 | 'class' => 'yii\multiparser\YiiMultiparser', |
45 | + 'configuration' => $mp_configuration, | ||
44 | 46 | ||
45 | ], | 47 | ], |
46 | ], | 48 | ], |
backend/controllers/ParserController.php
@@ -8,7 +8,7 @@ use yii\filters\VerbFilter; | @@ -8,7 +8,7 @@ use yii\filters\VerbFilter; | ||
8 | use backend\models\UploadFileParsingForm; | 8 | use backend\models\UploadFileParsingForm; |
9 | use yii\web\UploadedFile; | 9 | use yii\web\UploadedFile; |
10 | use yii\data\ArrayDataProvider; | 10 | use yii\data\ArrayDataProvider; |
11 | -use backend\components\parsers\DynamicFormHelper; | 11 | +use yii\multiparser\DynamicFormHelper; |
12 | use backend\components\parsers\CustomParserConfigurator; | 12 | use backend\components\parsers\CustomParserConfigurator; |
13 | 13 | ||
14 | use common\components\CustomVarDamp; | 14 | use common\components\CustomVarDamp; |
@@ -96,13 +96,14 @@ class ParserController extends BaseController | @@ -96,13 +96,14 @@ class ParserController extends BaseController | ||
96 | ], | 96 | ], |
97 | ]); | 97 | ]); |
98 | 98 | ||
99 | + // CustomVarDamp::dumpAndDie($data); | ||
99 | $header_model = DynamicFormHelper::CreateDynamicModel( count( $data[0] ) ); | 100 | $header_model = DynamicFormHelper::CreateDynamicModel( count( $data[0] ) ); |
100 | 101 | ||
101 | - //CustomVarDamp::dumpAndDie($header_model); | 102 | + // CustomVarDamp::dumpAndDie(Yii::$app->multiparser->getConfiguration('csv','basic_column')); |
102 | return $this->render('results', | 103 | return $this->render('results', |
103 | ['model' => $data, | 104 | ['model' => $data, |
104 | 'header_model' => $header_model, | 105 | 'header_model' => $header_model, |
105 | - 'basic_column' => CustomParserConfigurator::$basic_column, | 106 | + 'basic_column' => Yii::$app->multiparser->getConfiguration('csv','basic_column'), |
106 | 'dataProvider' => $provider]); | 107 | 'dataProvider' => $provider]); |
107 | } | 108 | } |
108 | 109 | ||
@@ -113,7 +114,7 @@ public function actionWrite() | @@ -113,7 +114,7 @@ public function actionWrite() | ||
113 | $arr_attributes = Yii::$app->request->post()['DynamicModel']; | 114 | $arr_attributes = Yii::$app->request->post()['DynamicModel']; |
114 | $model = DynamicFormHelper::CreateDynamicModel( $arr_attributes ); | 115 | $model = DynamicFormHelper::CreateDynamicModel( $arr_attributes ); |
115 | foreach ($arr_attributes as $key => $value) { | 116 | foreach ($arr_attributes as $key => $value) { |
116 | - $model->addRule($key, 'in', ['range' => array_keys( CustomParserConfigurator::$basic_column )]); | 117 | + $model->addRule($key, 'in', ['range' => array_keys( Yii::$app->multiparser->getConfiguration('csv','basic_column') )]); |
117 | } | 118 | } |
118 | 119 | ||
119 | //CustomVarDamp::dumpAndDie($model); | 120 | //CustomVarDamp::dumpAndDie($model); |
backend/views/parser/results.php
1 | <?php | 1 | <?php |
2 | 2 | ||
3 | use yii\helpers\Html; | 3 | use yii\helpers\Html; |
4 | -use backend\components\parsers\DynamicFormHelper; | 4 | +use yii\multiparser\DynamicFormHelper; |
5 | use yii\widgets\ActiveForm; | 5 | use yii\widgets\ActiveForm; |
6 | 6 | ||
7 | 7 | ||
@@ -20,7 +20,7 @@ $this->params['breadcrumbs'][] = $this->title; | @@ -20,7 +20,7 @@ $this->params['breadcrumbs'][] = $this->title; | ||
20 | 20 | ||
21 | $form = ActiveForm::begin(['action' => 'write']); | 21 | $form = ActiveForm::begin(['action' => 'write']); |
22 | ?> | 22 | ?> |
23 | - <?= DynamicFormHelper::CreateDynamicGridViewWithDropDownListHeader( $dataProvider, $form, $header_model, $basic_column )?> | 23 | + <?= DynamicFormHelper::CreateGridWithDropDownListHeader( $dataProvider, $form, $header_model, $basic_column )?> |
24 | 24 | ||
25 | <div class="form-group"> | 25 | <div class="form-group"> |
26 | <?= Html::submitButton(Yii::t('app', 'Записать в БД'), ['class' => 'btn btn-primary']) ?> | 26 | <?= Html::submitButton(Yii::t('app', 'Записать в БД'), ['class' => 'btn btn-primary']) ?> |
1 | +<?php | ||
2 | +/** | ||
3 | +* Created by PhpStorm. | ||
4 | + * User: Cibermag | ||
5 | +* Date: 31.08.2015 | ||
6 | +* Time: 12:50 | ||
7 | +*/ | ||
8 | + | ||
9 | +namespace yii\multiparser; | ||
10 | + | ||
11 | +// класс который содержит преобразователи значений (фильтры) используемые при парсинге | ||
12 | +class CSVConverter | ||
13 | +{ | ||
14 | + | ||
15 | + protected static $method_prefix = 'convertTo'; | ||
16 | + | ||
17 | + public static $configuration = []; | ||
18 | + | ||
19 | + protected static function convertToFloat($value) | ||
20 | + { | ||
21 | + | ||
22 | + if ($value == '') { | ||
23 | + $value = 0; | ||
24 | + } | ||
25 | + $value = trim(str_replace(",", ".", $value)); | ||
26 | + $value = preg_replace("/[^0-9.]+/", "", strtoupper($value)); | ||
27 | + | ||
28 | + if ($value == '') { | ||
29 | + return ''; | ||
30 | + } | ||
31 | + $value = round( (float)$value, 2 ); | ||
32 | + | ||
33 | + return $value; | ||
34 | + } | ||
35 | + | ||
36 | + protected static function convertToInteger($value) | ||
37 | + { | ||
38 | + if ($value == '') { | ||
39 | + $value = 0; | ||
40 | + } | ||
41 | + $value = trim(str_replace(",", ".", $value)); | ||
42 | + $value = preg_replace("/[^0-9.]+/", "", strtoupper($value)); | ||
43 | + if ($value == '') { | ||
44 | + return ''; | ||
45 | + } | ||
46 | + $value = round((int)$value, 2); | ||
47 | + | ||
48 | + return $value; | ||
49 | + } | ||
50 | + | ||
51 | + protected static function convertToString($value) | ||
52 | + { | ||
53 | + $res = ''; | ||
54 | + if (is_array($value)) { | ||
55 | + | ||
56 | + $res = Encoder::encodeArray($value); | ||
57 | + | ||
58 | + }elseif ( is_string($value) ) { | ||
59 | + | ||
60 | + $res = Encoder::encodeString($value); | ||
61 | + | ||
62 | + } | ||
63 | + | ||
64 | + return $res; | ||
65 | + } | ||
66 | + | ||
67 | + | ||
68 | + /** | ||
69 | + * @param $name - имя метода конвертации | ||
70 | + * @param $value - значение на конвертацию | ||
71 | + * @return mixed | ||
72 | + */ | ||
73 | + public static function __callStatic( $name, $value ) | ||
74 | + { | ||
75 | + $method_name = self::$method_prefix . $name; | ||
76 | + if ( is_callable( $method_name ) ) { | ||
77 | + | ||
78 | + self::$method_name( $value ); | ||
79 | + | ||
80 | + } else{ | ||
81 | + // если такого метода конвертации не предусмотрено, то возвращаем не конвертируя | ||
82 | + return $value; | ||
83 | + | ||
84 | + } | ||
85 | + } | ||
86 | + | ||
87 | + | ||
88 | + /** | ||
89 | + * @param $arr - массив | ||
90 | + * @return mixed | ||
91 | + * конвертирует массив по полученным настройкам, вызывая последовательно функции конвертации (указанные в конфигурации) | ||
92 | + */ | ||
93 | + public static function convertByConfiguration( $arr ) | ||
94 | + { | ||
95 | + $result = $arr; | ||
96 | + | ||
97 | + $hasKey = isset( self::$configuration['hasKey'] ); | ||
98 | + | ||
99 | + foreach ( self::$configuration as $key => $value ) { | ||
100 | + | ||
101 | + if ( $hasKey ){ | ||
102 | + // у нас ассоциативный массив, и мы можем конвертировать каждое значение в отдельности | ||
103 | + if ( is_array( $value ) ) { | ||
104 | + foreach ($value as $sub_value) { | ||
105 | + if (isset($arr[$sub_value])) { | ||
106 | + // конвертируем только те ячейки которые сопоставлены в прочитанном массиве с колонками в конфигурационном файле | ||
107 | + $result[$arr[$sub_value]] = self::$key( $arr[$sub_value] ); | ||
108 | + } | ||
109 | + | ||
110 | + } | ||
111 | + } else { | ||
112 | + | ||
113 | + if (isset($arr[$value])) { | ||
114 | + // конвертируем только те ячейки которые сопоставлены в прочитанном массиве с колонками в конфигурационном файле | ||
115 | + $result[$arr[$value]] = self::$key( $arr[$value] ); | ||
116 | + } | ||
117 | + | ||
118 | + } | ||
119 | + | ||
120 | + } else { | ||
121 | + // нет заголовка - мы можем конвертировать только с року в целом | ||
122 | + $result = self::$key( $arr ); | ||
123 | + } | ||
124 | + | ||
125 | + } | ||
126 | + | ||
127 | + return $result; | ||
128 | + } | ||
129 | + | ||
130 | + | ||
131 | +} | ||
0 | \ No newline at end of file | 132 | \ No newline at end of file |
vendor/yiisoft/multiparser/CsvParser.php
@@ -45,10 +45,14 @@ class CsvParser implements ParserInterface | @@ -45,10 +45,14 @@ class CsvParser implements ParserInterface | ||
45 | * используется при автоопределении первой строки*/ | 45 | * используется при автоопределении первой строки*/ |
46 | public $min_column_quantity = 5; | 46 | public $min_column_quantity = 5; |
47 | 47 | ||
48 | + /** @var array - конфигурация конвертера значений */ | ||
49 | + public $converter_conf = []; | ||
50 | + /** @var array - конвертер созданный по конфигурации */ | ||
51 | + public $converter = NULL; | ||
48 | 52 | ||
49 | 53 | ||
50 | /** | 54 | /** |
51 | - метод устанвливает нужные настройки объекта SplFileObject, для работы с csv | 55 | + * метод устанвливает нужные настройки объекта SplFileObject, для работы с csv |
52 | */ | 56 | */ |
53 | public function setup() | 57 | public function setup() |
54 | { | 58 | { |
@@ -58,9 +62,27 @@ class CsvParser implements ParserInterface | @@ -58,9 +62,27 @@ class CsvParser implements ParserInterface | ||
58 | if ($this->auto_detect_first_line) { | 62 | if ($this->auto_detect_first_line) { |
59 | $this->shiftToFirstValuableLine(); | 63 | $this->shiftToFirstValuableLine(); |
60 | } | 64 | } |
65 | + $this->setupConverter(); | ||
66 | + | ||
61 | } | 67 | } |
62 | 68 | ||
63 | /** | 69 | /** |
70 | + * устанавливает конвертер значений согласно конфигурационным настройкам | ||
71 | + */ | ||
72 | + public function setupConverter() | ||
73 | + { | ||
74 | + if (!count($this->converter_conf)) { | ||
75 | + $this->converter = new CSVConverter(); | ||
76 | + if ($this->hasHeaderRow) { | ||
77 | + // если у файла есть заголовок, то в результате имеем ассоциативный массив | ||
78 | + $this->converter_conf['hasKey'] = 1; | ||
79 | + } | ||
80 | + $this->converter->configuration = $this->converter_conf; | ||
81 | + } | ||
82 | + } | ||
83 | + | ||
84 | + | ||
85 | + /** | ||
64 | * определяет первую значимую строку, | 86 | * определяет первую значимую строку, |
65 | * считывается файл пока в нем не встретится строка с непустыми колонками | 87 | * считывается файл пока в нем не встретится строка с непустыми колонками |
66 | * в количестве указанном в атрибуте min_column_quantity | 88 | * в количестве указанном в атрибуте min_column_quantity |
@@ -112,14 +134,12 @@ class CsvParser implements ParserInterface | @@ -112,14 +134,12 @@ class CsvParser implements ParserInterface | ||
112 | 134 | ||
113 | if (count($this->keys) !== count($row)) { | 135 | if (count($this->keys) !== count($row)) { |
114 | // | 136 | // |
115 | - throw new \ErrorException( "Invalid columns detected on line # {$current_line}", 0, 1, $this->file->getBasename(), $current_line); | 137 | + throw new \ErrorException("Invalid columns detected on line # {$current_line}", 0, 1, $this->file->getBasename(), $current_line); |
116 | } | 138 | } |
117 | 139 | ||
118 | $return[] = array_combine($this->keys, $row); | 140 | $return[] = array_combine($this->keys, $row); |
119 | } | 141 | } |
120 | - } | ||
121 | - else | ||
122 | - { | 142 | + } else { |
123 | $return[] = $row; | 143 | $return[] = $row; |
124 | } | 144 | } |
125 | // если у нас установлен лимит, при его достижении прекращаем парсинг | 145 | // если у нас установлен лимит, при его достижении прекращаем парсинг |
@@ -134,7 +154,6 @@ class CsvParser implements ParserInterface | @@ -134,7 +154,6 @@ class CsvParser implements ParserInterface | ||
134 | } | 154 | } |
135 | 155 | ||
136 | 156 | ||
137 | - | ||
138 | protected function closeHandler() | 157 | protected function closeHandler() |
139 | { | 158 | { |
140 | $this->file = NULL; | 159 | $this->file = NULL; |
@@ -147,10 +166,15 @@ class CsvParser implements ParserInterface | @@ -147,10 +166,15 @@ class CsvParser implements ParserInterface | ||
147 | { | 166 | { |
148 | 167 | ||
149 | $row = $this->file->fgetcsv(); | 168 | $row = $this->file->fgetcsv(); |
150 | - if (is_array($row) && $this->first_column) { | 169 | + if (is_array($row)) { |
170 | + // попытаемся конвертировать прочитанные занчения согдасно конфигурации котнвертера значений | ||
171 | + $row = $this->convert($row); | ||
172 | + | ||
173 | + if ($this->first_column) { | ||
151 | 174 | ||
152 | - $row = array_slice($row, $this->first_column); | 175 | + $row = array_slice($row, $this->first_column); |
153 | 176 | ||
177 | + } | ||
154 | } | 178 | } |
155 | if (is_null($row)) | 179 | if (is_null($row)) |
156 | $row = false; | 180 | $row = false; |
@@ -159,5 +183,27 @@ class CsvParser implements ParserInterface | @@ -159,5 +183,27 @@ class CsvParser implements ParserInterface | ||
159 | 183 | ||
160 | } | 184 | } |
161 | 185 | ||
186 | + /** | ||
187 | + * @param $arr | ||
188 | + * @return mixed | ||
189 | + * преобразовует значения прочитанного массива в нужные типы, согласно конфигурации конвертера | ||
190 | + */ | ||
191 | + protected function convert($arr) | ||
192 | + { | ||
193 | + $result = $arr; | ||
194 | + $converter = $this->converter; | ||
195 | + | ||
196 | + //\common\components\CustomVarDamp::dumpAndDie($this); | ||
197 | + | ||
198 | + if (!is_null($converter)) { | ||
199 | + | ||
200 | + $result = $converter::convertByConfiguration($arr); | ||
201 | + | ||
202 | + } | ||
203 | + | ||
204 | + return $result; | ||
205 | + | ||
206 | + } | ||
207 | + | ||
162 | 208 | ||
163 | } | 209 | } |
164 | \ No newline at end of file | 210 | \ No newline at end of file |
backend/components/parsers/DynamicFormHelper.php renamed to vendor/yiisoft/multiparser/DynamicFormHelper.php
@@ -6,7 +6,7 @@ | @@ -6,7 +6,7 @@ | ||
6 | * Time: 14:50 | 6 | * Time: 14:50 |
7 | */ | 7 | */ |
8 | 8 | ||
9 | -namespace backend\components\parsers; | 9 | +namespace yii\multiparser; |
10 | 10 | ||
11 | use yii\base\DynamicModel; | 11 | use yii\base\DynamicModel; |
12 | use yii\grid\GridView; | 12 | use yii\grid\GridView; |
@@ -48,8 +48,8 @@ class DynamicFormHelper | @@ -48,8 +48,8 @@ class DynamicFormHelper | ||
48 | return $model; | 48 | return $model; |
49 | } | 49 | } |
50 | 50 | ||
51 | - // @todo - rewrite on two functions, add comments | ||
52 | - public static function CreateDynamicGridViewWithDropDownListHeader( $dataProvider, $form, $header_model, $arr_header_values ) | 51 | + // @todo add comments |
52 | + public static function CreateGridWithDropDownListHeader( $dataProvider, $form, $header_model, $arr_header_values ) | ||
53 | { | 53 | { |
54 | $columns_config = [['class' => SerialColumn::className()]]; | 54 | $columns_config = [['class' => SerialColumn::className()]]; |
55 | $i = 0; | 55 | $i = 0; |
@@ -59,8 +59,6 @@ class DynamicFormHelper | @@ -59,8 +59,6 @@ class DynamicFormHelper | ||
59 | $i++; | 59 | $i++; |
60 | } | 60 | } |
61 | 61 | ||
62 | - //\common\components\CustomVarDamp::dumpAndDie($columns_config); | ||
63 | - | ||
64 | $dynamic_grid_view = GridView::widget( ['dataProvider' => $dataProvider, | 62 | $dynamic_grid_view = GridView::widget( ['dataProvider' => $dataProvider, |
65 | 'columns' => $columns_config ] ); | 63 | 'columns' => $columns_config ] ); |
66 | 64 |
backend/components/parsers/Encoder.php renamed to vendor/yiisoft/multiparser/Encoder.php
@@ -6,7 +6,7 @@ | @@ -6,7 +6,7 @@ | ||
6 | * Time: 13:36 | 6 | * Time: 13:36 |
7 | */ | 7 | */ |
8 | 8 | ||
9 | -namespace backend\components\parsers; | 9 | +namespace yii\multiparser; |
10 | 10 | ||
11 | // @todo add comments | 11 | // @todo add comments |
12 | class Encoder | 12 | class Encoder |
@@ -26,16 +26,18 @@ class Encoder | @@ -26,16 +26,18 @@ class Encoder | ||
26 | @fclose($file); | 26 | @fclose($file); |
27 | } | 27 | } |
28 | 28 | ||
29 | - public static function encodeArray($in_charset, $out_charset, $array) | 29 | + public static function encodeArray( $array, $in_charset = '', $out_charset = '') |
30 | { | 30 | { |
31 | + if (isset($in_charset)) | ||
32 | + self::$in_charset = isset($in_charset); | ||
31 | 33 | ||
32 | - self::$in_charset = $in_charset; | 34 | + if (isset($out_charset)) |
33 | self::$out_charset = $out_charset; | 35 | self::$out_charset = $out_charset; |
34 | 36 | ||
35 | $result = array_map( | 37 | $result = array_map( |
36 | function ( $array) { | 38 | function ( $array) { |
37 | 39 | ||
38 | - return self::encode( self::$in_charset, self::$out_charset, $array ); | 40 | + return self::encodeString( $array, self::$in_charset, self::$out_charset ); |
39 | 41 | ||
40 | }, | 42 | }, |
41 | $array); | 43 | $array); |
@@ -43,9 +45,15 @@ class Encoder | @@ -43,9 +45,15 @@ class Encoder | ||
43 | return $result; | 45 | return $result; |
44 | } | 46 | } |
45 | 47 | ||
46 | - private static function encode( $in_charset, $out_charset, $source ){ | 48 | + public static function encodeString( $source, $in_charset = '', $out_charset = '' ){ |
47 | 49 | ||
48 | - return iconv($in_charset, $out_charset, $source); | 50 | + if (isset($in_charset)) |
51 | + self::$in_charset = isset($in_charset); | ||
52 | + | ||
53 | + if (isset($out_charset)) | ||
54 | + self::$out_charset = $out_charset; | ||
55 | + | ||
56 | + return iconv( self::$in_charset, self::$out_charset, $source ); | ||
49 | 57 | ||
50 | } | 58 | } |
51 | } | 59 | } |
52 | \ No newline at end of file | 60 | \ No newline at end of file |
vendor/yiisoft/multiparser/ParserConfigurator.php
@@ -4,23 +4,29 @@ namespace yii\multiparser; | @@ -4,23 +4,29 @@ namespace yii\multiparser; | ||
4 | class ParserConfigurator | 4 | class ParserConfigurator |
5 | { | 5 | { |
6 | 6 | ||
7 | - protected static $configuration = [ | ||
8 | - 'csv' => | ||
9 | - ['web' => | ||
10 | - ['class' => 'backend\components\parsers\CustomCsvParser', | ||
11 | - 'auto_detect_first_line' => true,]]]; | 7 | + protected static $configuration; |
12 | 8 | ||
13 | - | ||
14 | - public static function getConfiguration($extension, $mode) | 9 | + public static function getConfiguration($extension, $parameter) |
15 | { | 10 | { |
11 | + self::setConfiguration(); | ||
12 | + | ||
16 | if (!isset( self::$configuration[$extension] )){ | 13 | if (!isset( self::$configuration[$extension] )){ |
17 | throw new \ErrorException( "Parser do not maintain file with extension {$extension}"); | 14 | throw new \ErrorException( "Parser do not maintain file with extension {$extension}"); |
18 | } | 15 | } |
19 | - if (!isset( self::$configuration[$extension][$mode] )){ | ||
20 | - throw new \ErrorException( "Parser configurator do not have settings for {$mode} mode"); | 16 | + if (!isset( self::$configuration[$extension][$parameter] )){ |
17 | + throw new \ErrorException( "Parser configurator do not have settings for {$parameter} parameter"); | ||
21 | } | 18 | } |
22 | 19 | ||
23 | - return self::$configuration[$extension][$mode]; | 20 | + return self::$configuration[$extension][$parameter]; |
24 | } | 21 | } |
25 | 22 | ||
23 | + protected static function setConfiguration() | ||
24 | + { | ||
25 | + | ||
26 | + self::$configuration = require(__DIR__ . '/config.php'); | ||
27 | + | ||
28 | + } | ||
29 | + | ||
30 | + | ||
31 | + | ||
26 | } | 32 | } |
27 | \ No newline at end of file | 33 | \ No newline at end of file |
1 | +<?php | ||
2 | +/** | ||
3 | + * Created by PhpStorm. | ||
4 | + * User: Cibermag | ||
5 | + * Date: 10.09.2015 | ||
6 | + * Time: 17:47 | ||
7 | + */ | ||
8 | + | ||
9 | +namespace yii\multiparser; | ||
10 | + | ||
11 | + | ||
12 | +class XmlParser implements ParserInterface{ | ||
13 | + public function read() | ||
14 | + { | ||
15 | + // TODO: Implement read() method. | ||
16 | + } | ||
17 | + | ||
18 | + public function setup() | ||
19 | + { | ||
20 | + // TODO: Implement setup() method. | ||
21 | + } | ||
22 | + | ||
23 | + | ||
24 | + /** | ||
25 | + * Converts an XML string to a PHP array | ||
26 | + * See http://phpsecurity.readthedocs.org/en/latest/Injection-Attacks.html#xml-external-entity-injection | ||
27 | + * | ||
28 | + * @uses recursiveXMLToArray() | ||
29 | + * @param string $val | ||
30 | + * @param boolean $disableDoctypes Disables the use of DOCTYPE, and will trigger an error if encountered. | ||
31 | + * false by default. | ||
32 | + * @param boolean $disableExternals Disables the loading of external entities. false by default. | ||
33 | + * @return array | ||
34 | + */ | ||
35 | + public static function xml2array($val, $disableDoctypes = false, $disableExternals = false) { | ||
36 | + // Check doctype | ||
37 | + if($disableDoctypes && preg_match('/\<\!DOCTYPE.+]\>/', $val)) { | ||
38 | + throw new InvalidArgumentException('XML Doctype parsing disabled'); | ||
39 | + } | ||
40 | + | ||
41 | + // Disable external entity loading | ||
42 | + if($disableExternals) $oldVal = libxml_disable_entity_loader($disableExternals); | ||
43 | + try { | ||
44 | + $xml = new SimpleXMLElement($val); | ||
45 | + $result = self::recursiveXMLToArray($xml); | ||
46 | + } catch(Exception $ex) { | ||
47 | + if($disableExternals) libxml_disable_entity_loader($oldVal); | ||
48 | + throw $ex; | ||
49 | + } | ||
50 | + if($disableExternals) libxml_disable_entity_loader($oldVal); | ||
51 | + return $result; | ||
52 | + } | ||
53 | + | ||
54 | + /** | ||
55 | + * Convert a XML string to a PHP array recursively. Do not | ||
56 | + * call this function directly, Please use {@link Convert::xml2array()} | ||
57 | + * | ||
58 | + * @param SimpleXMLElement | ||
59 | + * | ||
60 | + * @return mixed | ||
61 | + */ | ||
62 | + protected static function recursiveXMLToArray($xml) { | ||
63 | + if(is_object($xml) && get_class($xml) == 'SimpleXMLElement') { | ||
64 | + $attributes = $xml->attributes(); | ||
65 | + foreach($attributes as $k => $v) { | ||
66 | + if($v) $a[$k] = (string) $v; | ||
67 | + } | ||
68 | + $x = $xml; | ||
69 | + $xml = get_object_vars($xml); | ||
70 | + } | ||
71 | + if(is_array($xml)) { | ||
72 | + if(count($xml) == 0) return (string) $x; // for CDATA | ||
73 | + foreach($xml as $key => $value) { | ||
74 | + $r[$key] = self::recursiveXMLToArray($value); | ||
75 | + } | ||
76 | + if(isset($a)) $r['@'] = $a; // Attributes | ||
77 | + return $r; | ||
78 | + } | ||
79 | + | ||
80 | + return (string) $xml; | ||
81 | + } | ||
82 | +} | ||
0 | \ No newline at end of file | 83 | \ No newline at end of file |
vendor/yiisoft/multiparser/YiiMultiparser.php
@@ -11,8 +11,26 @@ namespace yii\multiparser; | @@ -11,8 +11,26 @@ namespace yii\multiparser; | ||
11 | use yii\base\Component; | 11 | use yii\base\Component; |
12 | 12 | ||
13 | 13 | ||
14 | + | ||
15 | + | ||
14 | class YiiMultiparser extends Component{ | 16 | class YiiMultiparser extends Component{ |
15 | 17 | ||
18 | +public $configuration; | ||
19 | + | ||
20 | + public function getConfiguration($extension, $conf_parameter){ | ||
21 | + | ||
22 | + if (!isset( $this->configuration[$extension] )){ | ||
23 | + throw new \ErrorException( "Parser do not maintain file with extension {$extension}"); | ||
24 | + } | ||
25 | + if (!isset( $this->configuration[$extension][$conf_parameter] )){ | ||
26 | + throw new \ErrorException( "Parser configurator do not have settings for {$conf_parameter} parameter"); | ||
27 | + } | ||
28 | + | ||
29 | + return $this->configuration[$extension][$conf_parameter]; | ||
30 | + | ||
31 | + } | ||
32 | + | ||
33 | + | ||
16 | public function parse( $filePath, $options = [] ){ | 34 | public function parse( $filePath, $options = [] ){ |
17 | 35 | ||
18 | $parser = new YiiParserHandler( $filePath, $options ); | 36 | $parser = new YiiParserHandler( $filePath, $options ); |
vendor/yiisoft/multiparser/YiiParserHandler.php
@@ -12,4 +12,72 @@ namespace yii\multiparser; | @@ -12,4 +12,72 @@ namespace yii\multiparser; | ||
12 | class YiiParserHandler extends ParserHandler{ | 12 | class YiiParserHandler extends ParserHandler{ |
13 | 13 | ||
14 | 14 | ||
15 | + /** | ||
16 | + * @param $filePath | ||
17 | + * @param array $options | ||
18 | + * проверяет читабенльность переданного файла, а также наличие настроек парсера в конфигурационном файле для данного типа файла | ||
19 | + */ | ||
20 | + public function __construct($filePath, $options = []) | ||
21 | + { | ||
22 | + $this->filePath = $filePath; | ||
23 | + if (isset($options['mode'])) { | ||
24 | + | ||
25 | + $this->mode = $options['mode']; | ||
26 | + unset($options['mode']); | ||
27 | + | ||
28 | + } else { | ||
29 | + | ||
30 | + $this->mode = self::DEFAULT_MODE; | ||
31 | + | ||
32 | + } | ||
33 | + | ||
34 | + $this->options = $options; | ||
35 | + | ||
36 | + try { | ||
37 | + $this->fileObject = new \SplFileObject($this->filePath, 'r'); | ||
38 | + } catch (\ErrorException $e) { | ||
39 | + // Yii::warning("Ошибка открытия файла {$this->filePath}"); | ||
40 | + echo "Ошибка открытия файла {$this->filePath}"; | ||
41 | + return []; | ||
42 | + } | ||
43 | + | ||
44 | + $options['file'] = $this->fileObject; | ||
45 | + $this->extension = $this->fileObject->getExtension(); | ||
46 | + | ||
47 | + try { | ||
48 | + $this->configuration = \Yii::$app->multiparser->getConfiguration($this->extension, $this->mode); | ||
49 | + $this->configuration = array_merge($this->configuration, $options); | ||
50 | + | ||
51 | + } catch (\ErrorException $e) { | ||
52 | + echo $e->getMessage(); | ||
53 | + return []; | ||
54 | + } | ||
55 | + | ||
56 | + } | ||
57 | + | ||
58 | + public function run() | ||
59 | + { | ||
60 | + | ||
61 | + $result = []; | ||
62 | + | ||
63 | + // \common\components\CustomVarDamp::dumpAndDie($this); | ||
64 | + if (count($this->configuration)) { | ||
65 | + $parser = \Yii::createObject($this->configuration); | ||
66 | + | ||
67 | + try { | ||
68 | + | ||
69 | + $parser->setup(); | ||
70 | + $result = $parser->read(); | ||
71 | + | ||
72 | + } catch (\ErrorException $e) { | ||
73 | + | ||
74 | + echo $e->getMessage(); | ||
75 | + | ||
76 | + } | ||
77 | + | ||
78 | + } | ||
79 | + | ||
80 | + return $result; | ||
81 | + } | ||
82 | + | ||
15 | } | 83 | } |
16 | \ No newline at end of file | 84 | \ No newline at end of file |
1 | +<?php | ||
2 | +return | ||
3 | + [ | ||
4 | + 'csv' => | ||
5 | + ['web' => | ||
6 | + ['class' => 'yii\multiparser\CsvParser', | ||
7 | + 'auto_detect_first_line' => true, | ||
8 | + 'converter_conf' => [ | ||
9 | + "float" => 'PRICE', | ||
10 | + "integer" => 'QUANTITY', | ||
11 | + "string" => 'DESCR' | ||
12 | + ]], | ||
13 | + 'basic_column' => [ | ||
14 | + "ARTICLE" => 'Артикул', | ||
15 | + "PRICE" => 'Цена', | ||
16 | + "DESCR" => 'Наименование', | ||
17 | + "QUANTITY" => 'Колво' | ||
18 | + | ||
19 | + ], | ||
20 | + ]]; |