Commit 282531690a747620ec70fe555c589a30fbd3edb9
1 parent
d21c5c5f
add converter as behaviour, add table - Details by migration
Showing
10 changed files
with
161 additions
and
64 deletions
Show diff stats
1 | +<?php | |
2 | +namespace backend\components\parsers; | |
3 | +use yii\multiparser\Converter; | |
4 | + | |
5 | +class CustomConverter extends Converter { | |
6 | + | |
7 | + /** | |
8 | + * @param $value_arr - двумерный массив значений, которому нужно присвоить ключи | |
9 | + * @param $key_array - ключи для вложенного массива | |
10 | + * @return array - таблица с проименованными колонками | |
11 | + */ | |
12 | + public static function convertToAssocArray ($value_arr, $key_array, $key_prefix = '') | |
13 | + { | |
14 | + if ($key_prefix) { | |
15 | + $parametrs_array = array_fill( 0, count($key_array), $key_prefix ); | |
16 | + $key_array = array_map( function ($value, $key_prefix){ return str_replace( $key_prefix, '',$value ); }, $key_array, $parametrs_array ); | |
17 | + } | |
18 | + | |
19 | + // преобразуем массив ключей (обернем в массив), для передачи его в качестве параметра в анонимную функцию для array_map | |
20 | + // для этого увеличим размерность массива, что бы при каждом обходе массива $value_arr , функции был доступен исходный массив ключей | |
21 | + $key_array = array_fill( 0, count($value_arr), $key_array ); | |
22 | + //\common\components\CustomVarDamp::dumpAndDie($key_array); | |
23 | + $result = array_map( | |
24 | + function ($value, $key_array) { | |
25 | + $res = $value; | |
26 | + foreach ($value as $key => $sub_value) { | |
27 | + if (isset($key_array[$key])) { | |
28 | + // если такой ключ в базовом массиве (массиве ключей) есть, то заменим новым, иначе просто удалим | |
29 | + $new_key = $key_array[$key]; | |
30 | + if( !array_key_exists( $new_key , $res ) ){ | |
31 | + $res[ $new_key ] = $res[$key]; | |
32 | + } | |
33 | + } | |
34 | + unset( $res[$key] ); | |
35 | + } | |
36 | + | |
37 | + return $res; | |
38 | + }, | |
39 | + $value_arr, $key_array); | |
40 | + return $result; | |
41 | + } | |
42 | + | |
43 | +} | |
0 | 44 | \ No newline at end of file | ... | ... |
backend/components/parsers/CustomCsvParser.php
... | ... | @@ -16,13 +16,17 @@ class CustomCsvParser extends \yii\multiparser\CsvParser { |
16 | 16 | // public $keys = ['first','second', 'third', 'forth', 'fifth']; |
17 | 17 | public function setupConverter() |
18 | 18 | { |
19 | + if ( count($this->converter_conf) ) { | |
20 | + | |
21 | + if ($this->hasHeaderRow) { | |
22 | + // если у файла есть заголовок, то в результате имеем ассоциативный массив | |
23 | + $this->converter_conf['hasKey'] = 1; | |
24 | + } | |
25 | + | |
26 | + $this->converter = \Yii::createObject($this->converter_conf); | |
19 | 27 | |
20 | - if ($this->hasHeaderRow) { | |
21 | - // если у файла есть заголовок, то в результате имеем ассоциативный массив | |
22 | - $this->converter_conf['hasKey'] = 1; | |
23 | 28 | } |
24 | 29 | |
25 | - $this->converter = \Yii::createObject($this->converter_conf); | |
26 | 30 | |
27 | 31 | } |
28 | 32 | ... | ... |
backend/components/parsers/config.php
... | ... | @@ -4,11 +4,9 @@ |
4 | 4 | ['web' => |
5 | 5 | ['class' => 'backend\components\parsers\CustomCsvParser', |
6 | 6 | 'auto_detect_first_line' => true, |
7 | - 'converter_conf' => ['class' => 'yii\multiparser\Converter', | |
8 | - 'configuration' => [ | |
9 | - "string" => 'DESCR' | |
10 | - ] | |
11 | - ,]], | |
7 | + 'converter_conf' => ['class' => ' backend\components\parsers\CustomConverter', | |
8 | + 'configuration' => ["string" => 'DESCR'],] | |
9 | + ], | |
12 | 10 | |
13 | 11 | 'basic_column' => [ |
14 | 12 | Null => 'Пусто', | ... | ... |
backend/config/main.php
backend/controllers/ParserController.php
... | ... | @@ -96,7 +96,7 @@ class ParserController extends BaseController |
96 | 96 | ], |
97 | 97 | ]); |
98 | 98 | |
99 | - // CustomVarDamp::dumpAndDie($data); | |
99 | + //CustomVarDamp::dumpAndDie($data); | |
100 | 100 | $header_model = DynamicFormHelper::CreateDynamicModel( count( $data[0] ) ); |
101 | 101 | |
102 | 102 | // CustomVarDamp::dumpAndDie(Yii::$app->multiparser->getConfiguration('csv','basic_column')); |
... | ... | @@ -117,13 +117,15 @@ public function actionWrite() |
117 | 117 | $model->addRule($key, 'in', ['range' => array_keys( Yii::$app->multiparser->getConfiguration('csv','basic_column') )]); |
118 | 118 | } |
119 | 119 | |
120 | + | |
120 | 121 | //CustomVarDamp::dumpAndDie($model); |
121 | 122 | if ($model->validate()) { |
122 | 123 | $arr = $model->toArray(); |
123 | 124 | $data = json_decode( Yii::$app->getCache()->get( 'parser_data' ),true ); |
124 | 125 | |
125 | - // CustomVarDamp::dumpAndDie(DynamicFormHelper::CreateAssocArray($data, $arr)); | |
126 | - CustomVarDamp::dumpAndDie($arr); | |
126 | + $data = \Yii::$app->multiparser->convertToAssocArray( $data, $arr, 'attr_' ); | |
127 | + $data[1]['BOX'] = \Yii::$app->multiparser->convertToFloat( $data[1]['BOX'] ); | |
128 | + CustomVarDamp::dumpAndDie($data); | |
127 | 129 | } |
128 | 130 | |
129 | 131 | ... | ... |
1 | +<?php | |
2 | +/** | |
3 | + * Created by PhpStorm. | |
4 | + * User: Cibermag | |
5 | + * Date: 15.09.2015 | |
6 | + * Time: 16:49 | |
7 | + */ | |
8 | + | |
9 | +namespace backend\models; | |
10 | + | |
11 | +use yii\base\Model; | |
12 | + | |
13 | +class Details extends Model{ | |
14 | + | |
15 | + public function save ($arr_value) | |
16 | + { | |
17 | + | |
18 | + } | |
19 | +} | |
0 | 20 | \ No newline at end of file | ... | ... |
1 | +<?php | |
2 | + | |
3 | +use yii\db\Schema; | |
4 | +use yii\db\Migration; | |
5 | + | |
6 | +class m150915_125129_addDetails extends Migration | |
7 | +{ | |
8 | + public function up() | |
9 | + { | |
10 | + $tableOptions = null; | |
11 | + if ($this->db->driverName === 'mysql') { | |
12 | + $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB'; | |
13 | + } | |
14 | + | |
15 | + $this->createTable('{{%details}}', [ | |
16 | + 'ID' => 'int(10) UNSIGNED NOT NULL', | |
17 | + 'IMPORT_ID' => 'int(6) unsigned NOT NULL', | |
18 | + 'BRAND' => 'varchar(100) NOT NULL', | |
19 | + 'ARTICLE' => 'varchar(100) NOT NULL', | |
20 | + 'FULL_ARTICLE' => 'varchar(150) NOT NULL', | |
21 | + 'PRICE' => 'float(15,2) unsigned NOT NULL', | |
22 | + 'DESCR' => 'varchar(200) NOT NULL', | |
23 | + 'BOX' => 'int(6) unsigned NOT NULL', | |
24 | + 'ADD_BOX' => 'int(6) unsigned NOT NULL DEFAULT 0', | |
25 | + 'GROUP' => 'varchar(200) NOT NULL', | |
26 | + 'timestamp' => 'timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP',], $tableOptions); | |
27 | + | |
28 | + //$this->dropPrimaryKey('ID','{{%details}}'); | |
29 | + $this->createIndex('ID_delete', '{{%details}}', 'ID', true); | |
30 | + $this->execute('ALTER TABLE details | |
31 | + CHANGE COLUMN ID ID INT(10) UNSIGNED NOT NULL AUTO_INCREMENT'); | |
32 | + $this->addPrimaryKey('importer_id', '{{%details}}', 'import_id, brand, ARTICLE'); | |
33 | + $this->createIndex('timestamp', '{{%details}}', 'timestamp', false); | |
34 | + $this->createIndex('ARTICLE', '{{%details}}', 'ARTICLE', 'BRAND', 'ADD_BOX', false); | |
35 | + $this->createIndex('IMPORT_ID', '{{%details}}', 'ARTICLE', false); | |
36 | + $this->createIndex('IMPORT_ID_2', '{{%details}}', 'IMPORT_ID', 'timestamp', false); | |
37 | + | |
38 | + | |
39 | + | |
40 | + | |
41 | +// PRIMARY KEY ('ARTICLE','BRAND','IMPORT_ID'), | |
42 | +// UNIQUE KEY 'ID_delete' ('ID'), | |
43 | +// KEY 'timestamp' ('timestamp'), | |
44 | +// KEY 'ARTICLE' ('ARTICLE','BRAND','BOX'), | |
45 | +// KEY 'BRAND' ('BRAND','ARTICLE'), | |
46 | +// KEY 'ARTICLE_2' ('ARTICLE','BRAND','ADD_BOX'), | |
47 | +// KEY 'IMPORT_ID' ('IMPORT_ID','ARTICLE'), | |
48 | +// KEY 'IMPORT_ID_2' ('IMPORT_ID','timestamp | |
49 | + | |
50 | + } | |
51 | + | |
52 | + public function down() | |
53 | + { | |
54 | + $this->dropTable('{{%details}}'); | |
55 | + | |
56 | + } | |
57 | + | |
58 | + /* | |
59 | + // Use safeUp/safeDown to run migration code within a transaction | |
60 | + public function safeUp() | |
61 | + { | |
62 | + } | |
63 | + | |
64 | + public function safeDown() | |
65 | + { | |
66 | + } | |
67 | + */ | |
68 | +} | ... | ... |
vendor/yiisoft/multiparser/Converter.php
... | ... | @@ -7,9 +7,10 @@ |
7 | 7 | */ |
8 | 8 | |
9 | 9 | namespace yii\multiparser; |
10 | +use yii\base\Behavior; | |
10 | 11 | |
11 | 12 | // класс который содержит преобразователи значений (фильтры) используемые при парсинге |
12 | -class Converter | |
13 | +class Converter extends Behavior | |
13 | 14 | { |
14 | 15 | |
15 | 16 | const METHOD_PREFIX = 'convertTo'; |
... | ... | @@ -18,7 +19,7 @@ class Converter |
18 | 19 | |
19 | 20 | protected static function convertToFloat($value) |
20 | 21 | { |
21 | - | |
22 | + echo 1; | |
22 | 23 | if ($value == '') { |
23 | 24 | $value = 0; |
24 | 25 | } |
... | ... | @@ -51,6 +52,7 @@ class Converter |
51 | 52 | protected static function convertToString($value) |
52 | 53 | { |
53 | 54 | |
55 | + | |
54 | 56 | $res = ''; |
55 | 57 | if (is_array($value)) { |
56 | 58 | |
... | ... | @@ -61,26 +63,9 @@ class Converter |
61 | 63 | $res = Encoder::encodeString($value); |
62 | 64 | |
63 | 65 | } |
64 | - | |
65 | 66 | return $res; |
66 | 67 | } |
67 | 68 | |
68 | -// protected static function convertToAssocArray($arr) | |
69 | -// { | |
70 | -// | |
71 | -// $res = ''; | |
72 | -// if (is_array($value)) { | |
73 | -// | |
74 | -// $res = Encoder::encodeArray($value); | |
75 | -// | |
76 | -// }elseif ( is_string($value) ) { | |
77 | -// | |
78 | -// $res = Encoder::encodeString($value); | |
79 | -// | |
80 | -// } | |
81 | -// | |
82 | -// return $res; | |
83 | -// } | |
84 | 69 | |
85 | 70 | /** |
86 | 71 | * @param $name - имя метода конвертации |
... | ... | @@ -90,7 +75,7 @@ class Converter |
90 | 75 | public static function __callStatic( $name, $value ) |
91 | 76 | { |
92 | 77 | $method_name = self::METHOD_PREFIX . $name; |
93 | - if ( method_exists( self::class ,$method_name ) ) { | |
78 | + if ( method_exists( self::class, $method_name ) ) { | |
94 | 79 | |
95 | 80 | return self::$method_name( $value[0] ); |
96 | 81 | |
... | ... | @@ -101,6 +86,11 @@ class Converter |
101 | 86 | } |
102 | 87 | } |
103 | 88 | |
89 | + public function __call($name, $params) | |
90 | + { | |
91 | + return self::__callStatic( $name, $params ); | |
92 | + } | |
93 | + | |
104 | 94 | |
105 | 95 | /** |
106 | 96 | * @param $arr - массив | ... | ... |
vendor/yiisoft/multiparser/CsvParser.php
... | ... | @@ -168,11 +168,11 @@ class CsvParser implements ParserInterface |
168 | 168 | { |
169 | 169 | |
170 | 170 | $row = $this->file->fgetcsv(); |
171 | + | |
171 | 172 | if (is_array($row)) { |
172 | 173 | // попытаемся конвертировать прочитанные занчения согдасно конфигурации котнвертера значений |
173 | - // \common\components\CustomVarDamp::dump($row,1); | |
174 | 174 | $row = $this->convert($row); |
175 | - // \common\components\CustomVarDamp::dump($row,2); | |
175 | + // \common\components\CustomVarDamp::dump($row); | |
176 | 176 | if ($this->first_column) { |
177 | 177 | |
178 | 178 | $row = array_slice($row, $this->first_column); | ... | ... |
vendor/yiisoft/multiparser/DynamicFormHelper.php
... | ... | @@ -22,7 +22,6 @@ class DynamicFormHelper |
22 | 22 | { |
23 | 23 | |
24 | 24 | const KEY_PREFIX = 'attr_'; |
25 | - private static $key_array; | |
26 | 25 | |
27 | 26 | /** |
28 | 27 | * @param $source - int or array |
... | ... | @@ -68,33 +67,4 @@ class DynamicFormHelper |
68 | 67 | |
69 | 68 | } |
70 | 69 | |
71 | - public static function CreateAssocArray ($value_arr, $key_array) | |
72 | - { | |
73 | - | |
74 | - self::$key_array = $key_array; | |
75 | - $result = array_map( | |
76 | - function ($value) { | |
77 | - | |
78 | - foreach ($value as $key => $sub_value) { | |
79 | - return self::change_key( $key, self::$key_array[$sub_value], $value ); | |
80 | - } | |
81 | - | |
82 | - | |
83 | - // array_combine( self::$key_array, $value ); | |
84 | - | |
85 | - }, | |
86 | - $value_arr); | |
87 | - return $result; | |
88 | - } | |
89 | - | |
90 | - protected static function change_key( $key, $new_key, $arr ){ | |
91 | - $res = $arr; | |
92 | - if(!array_key_exists($new_key,$arr)){ | |
93 | - $arr[$new_key]=$arr[$key]; | |
94 | - unset($arr[$key]); | |
95 | - $res = $arr; | |
96 | - } | |
97 | - return $res; | |
98 | - } | |
99 | - | |
100 | 70 | } |
101 | 71 | \ No newline at end of file | ... | ... |