Commit aa518ad34e489d4b9c6111c423aa9eda1cde5022
1 parent
74072a2a
finishing with converter and dynamic form
Showing
8 changed files
with
53 additions
and
31 deletions
Show diff stats
backend/components/parsers/CustomCsvParser.php
| ... | ... | @@ -17,14 +17,12 @@ class CustomCsvParser extends \yii\multiparser\CsvParser { |
| 17 | 17 | public function setupConverter() |
| 18 | 18 | { |
| 19 | 19 | |
| 20 | - | |
| 21 | 20 | if ($this->hasHeaderRow) { |
| 22 | 21 | // если у файла есть заголовок, то в результате имеем ассоциативный массив |
| 23 | 22 | $this->converter_conf['hasKey'] = 1; |
| 24 | 23 | } |
| 25 | 24 | |
| 26 | 25 | $this->converter = \Yii::createObject($this->converter_conf); |
| 27 | - \common\components\CustomVarDamp::dumpAndDie($this); | |
| 28 | 26 | |
| 29 | 27 | } |
| 30 | 28 | ... | ... |
backend/components/parsers/config.php
backend/controllers/ParserController.php
| ... | ... | @@ -120,7 +120,9 @@ public function actionWrite() |
| 120 | 120 | //CustomVarDamp::dumpAndDie($model); |
| 121 | 121 | if ($model->validate()) { |
| 122 | 122 | $arr = $model->toArray(); |
| 123 | - CustomVarDamp::dumpAndDie($arr); | |
| 123 | + $data = json_decode( Yii::$app->getCache()->get( 'parser_data' ),true ); | |
| 124 | + | |
| 125 | + CustomVarDamp::dumpAndDie(DynamicFormHelper::CreateAssocArray($data, $arr)); | |
| 124 | 126 | } |
| 125 | 127 | |
| 126 | 128 | ... | ... |
common/components/CustomVarDamp.php
| ... | ... | @@ -17,10 +17,16 @@ class CustomVarDamp extends BaseVarDumper { |
| 17 | 17 | echo "</pre>"; |
| 18 | 18 | die; |
| 19 | 19 | } |
| 20 | - public static function dump($var, $depth = 10, $highlight = false) | |
| 20 | + public static function dump($var, $step = '', $depth = 10, $highlight = false) | |
| 21 | 21 | { |
| 22 | 22 | echo "<pre>"; |
| 23 | + if ($step) { | |
| 24 | + echo "-------------- {$step} -------------"; | |
| 25 | + } | |
| 23 | 26 | echo static::dumpAsString($var, $depth, $highlight); |
| 27 | + if ($step) { | |
| 28 | + echo "-------------- {$step} -------------"; | |
| 29 | + } | |
| 24 | 30 | echo "</pre>"; |
| 25 | 31 | |
| 26 | 32 | } | ... | ... |
vendor/yiisoft/multiparser/CSVConverter.php
| ... | ... | @@ -12,9 +12,9 @@ namespace yii\multiparser; |
| 12 | 12 | class CSVConverter |
| 13 | 13 | { |
| 14 | 14 | |
| 15 | - protected static $method_prefix = 'convertTo'; | |
| 15 | + const METHOD_PREFIX = 'convertTo'; | |
| 16 | 16 | |
| 17 | - public static $configuration = []; | |
| 17 | + public $configuration = []; | |
| 18 | 18 | |
| 19 | 19 | protected static function convertToFloat($value) |
| 20 | 20 | { |
| ... | ... | @@ -50,6 +50,7 @@ class CSVConverter |
| 50 | 50 | |
| 51 | 51 | protected static function convertToString($value) |
| 52 | 52 | { |
| 53 | + | |
| 53 | 54 | $res = ''; |
| 54 | 55 | if (is_array($value)) { |
| 55 | 56 | |
| ... | ... | @@ -72,14 +73,14 @@ class CSVConverter |
| 72 | 73 | */ |
| 73 | 74 | public static function __callStatic( $name, $value ) |
| 74 | 75 | { |
| 75 | - $method_name = self::$method_prefix . $name; | |
| 76 | - if ( is_callable( $method_name ) ) { | |
| 76 | + $method_name = self::METHOD_PREFIX . $name; | |
| 77 | + if ( method_exists( self::class ,$method_name ) ) { | |
| 77 | 78 | |
| 78 | - self::$method_name( $value ); | |
| 79 | + return self::$method_name( $value[0] ); | |
| 79 | 80 | |
| 80 | 81 | } else{ |
| 81 | 82 | // если такого метода конвертации не предусмотрено, то возвращаем не конвертируя |
| 82 | - return $value; | |
| 83 | + return $value[0]; | |
| 83 | 84 | |
| 84 | 85 | } |
| 85 | 86 | } |
| ... | ... | @@ -90,13 +91,12 @@ class CSVConverter |
| 90 | 91 | * @return mixed |
| 91 | 92 | * конвертирует массив по полученным настройкам, вызывая последовательно функции конвертации (указанные в конфигурации) |
| 92 | 93 | */ |
| 93 | - public static function convertByConfiguration( $arr ) | |
| 94 | + public function convertByConfiguration( $arr ) | |
| 94 | 95 | { |
| 95 | 96 | $result = $arr; |
| 96 | 97 | |
| 97 | - $hasKey = isset( self::$configuration['hasKey'] ); | |
| 98 | - | |
| 99 | - foreach ( self::$configuration as $key => $value ) { | |
| 98 | + $hasKey = isset( $this->configuration['hasKey'] ); | |
| 99 | + foreach ( $this->configuration as $key => $value ) { | |
| 100 | 100 | |
| 101 | 101 | if ( $hasKey ){ |
| 102 | 102 | // у нас ассоциативный массив, и мы можем конвертировать каждое значение в отдельности |
| ... | ... | @@ -118,7 +118,7 @@ class CSVConverter |
| 118 | 118 | } |
| 119 | 119 | |
| 120 | 120 | } else { |
| 121 | - // нет заголовка - мы можем конвертировать только с року в целом | |
| 121 | + // нет заголовка - мы можем конвертировать только строку в целом | |
| 122 | 122 | $result = self::$key( $arr ); |
| 123 | 123 | } |
| 124 | 124 | ... | ... |
vendor/yiisoft/multiparser/CsvParser.php
| ... | ... | @@ -59,6 +59,7 @@ class CsvParser implements ParserInterface |
| 59 | 59 | $this->file->setCsvControl($this->delimiter); |
| 60 | 60 | $this->file->setFlags(\SplFileObject::READ_CSV); |
| 61 | 61 | $this->file->setFlags(\SplFileObject::SKIP_EMPTY); |
| 62 | + | |
| 62 | 63 | if ($this->auto_detect_first_line) { |
| 63 | 64 | $this->shiftToFirstValuableLine(); |
| 64 | 65 | } |
| ... | ... | @@ -78,6 +79,7 @@ class CsvParser implements ParserInterface |
| 78 | 79 | $this->converter_conf['hasKey'] = 1; |
| 79 | 80 | } |
| 80 | 81 | $this->converter->configuration = $this->converter_conf; |
| 82 | + | |
| 81 | 83 | } |
| 82 | 84 | } |
| 83 | 85 | |
| ... | ... | @@ -119,6 +121,7 @@ class CsvParser implements ParserInterface |
| 119 | 121 | */ |
| 120 | 122 | public function read() |
| 121 | 123 | { |
| 124 | + | |
| 122 | 125 | $return = []; |
| 123 | 126 | |
| 124 | 127 | $current_line = 0; |
| ... | ... | @@ -126,7 +129,6 @@ class CsvParser implements ParserInterface |
| 126 | 129 | |
| 127 | 130 | while (($row = $this->readRow()) !== FALSE) { |
| 128 | 131 | $current_line++; |
| 129 | - | |
| 130 | 132 | if ($this->hasHeaderRow) { |
| 131 | 133 | if ($this->keys === NULL) { |
| 132 | 134 | $this->keys = array_values($row); |
| ... | ... | @@ -168,8 +170,9 @@ class CsvParser implements ParserInterface |
| 168 | 170 | $row = $this->file->fgetcsv(); |
| 169 | 171 | if (is_array($row)) { |
| 170 | 172 | // попытаемся конвертировать прочитанные занчения согдасно конфигурации котнвертера значений |
| 173 | + // \common\components\CustomVarDamp::dump($row,1); | |
| 171 | 174 | $row = $this->convert($row); |
| 172 | - | |
| 175 | + // \common\components\CustomVarDamp::dump($row,2); | |
| 173 | 176 | if ($this->first_column) { |
| 174 | 177 | |
| 175 | 178 | $row = array_slice($row, $this->first_column); |
| ... | ... | @@ -193,11 +196,9 @@ class CsvParser implements ParserInterface |
| 193 | 196 | $result = $arr; |
| 194 | 197 | $converter = $this->converter; |
| 195 | 198 | |
| 196 | - //\common\components\CustomVarDamp::dumpAndDie($this); | |
| 197 | - | |
| 198 | 199 | if (!is_null($converter)) { |
| 199 | 200 | |
| 200 | - $result = $converter::convertByConfiguration($arr); | |
| 201 | + $result = $converter->convertByConfiguration( $arr ); | |
| 201 | 202 | |
| 202 | 203 | } |
| 203 | 204 | ... | ... |
vendor/yiisoft/multiparser/DynamicFormHelper.php
| ... | ... | @@ -11,6 +11,7 @@ namespace yii\multiparser; |
| 11 | 11 | use yii\base\DynamicModel; |
| 12 | 12 | use yii\grid\GridView; |
| 13 | 13 | use yii\grid\SerialColumn; |
| 14 | +use yii\helpers\ArrayHelper; | |
| 14 | 15 | |
| 15 | 16 | /** |
| 16 | 17 | * Class DynamicFormHelper |
| ... | ... | @@ -20,6 +21,8 @@ use yii\grid\SerialColumn; |
| 20 | 21 | class DynamicFormHelper |
| 21 | 22 | { |
| 22 | 23 | |
| 24 | + const KEY_PREFIX = 'attr_'; | |
| 25 | + private static $key_array; | |
| 23 | 26 | |
| 24 | 27 | /** |
| 25 | 28 | * @param $source - int or array |
| ... | ... | @@ -36,7 +39,7 @@ class DynamicFormHelper |
| 36 | 39 | |
| 37 | 40 | $i = 0; |
| 38 | 41 | while ($source > $i) { |
| 39 | - $arr_keys[] = "attr_{$i}"; | |
| 42 | + $arr_keys[] = self::KEY_PREFIX . $i; | |
| 40 | 43 | $i++; |
| 41 | 44 | } |
| 42 | 45 | array_flip($arr_keys); |
| ... | ... | @@ -58,7 +61,6 @@ class DynamicFormHelper |
| 58 | 61 | $columns_config[] = ['header' => $form->field($header_model, $key, ['inputOptions' => ['label' => '']])->dropDownList($arr_header_values), 'attribute' => $i]; |
| 59 | 62 | $i++; |
| 60 | 63 | } |
| 61 | - | |
| 62 | 64 | $dynamic_grid_view = GridView::widget( ['dataProvider' => $dataProvider, |
| 63 | 65 | 'columns' => $columns_config ] ); |
| 64 | 66 | |
| ... | ... | @@ -66,6 +68,18 @@ class DynamicFormHelper |
| 66 | 68 | |
| 67 | 69 | } |
| 68 | 70 | |
| 71 | + public static function CreateAssocArray ($value_arr, $key_array) | |
| 72 | + { | |
| 73 | + | |
| 74 | + self::$key_array = $key_array; | |
| 75 | + $result = array_map( | |
| 76 | + function ($value) { | |
| 69 | 77 | |
| 78 | + return array_combine( self::$key_array, $value ); | |
| 79 | + | |
| 80 | + }, | |
| 81 | + $value_arr); | |
| 82 | + return $result; | |
| 83 | + } | |
| 70 | 84 | |
| 71 | 85 | } |
| 72 | 86 | \ No newline at end of file | ... | ... |
vendor/yiisoft/multiparser/Encoder.php
| ... | ... | @@ -20,7 +20,7 @@ class Encoder |
| 20 | 20 | { |
| 21 | 21 | |
| 22 | 22 | $old_content = file_get_contents($filePath); |
| 23 | - $encode_content = self::encode( $in_charset, $out_charset, $old_content ); | |
| 23 | + $encode_content = self::encodeString( $old_content, $in_charset, $out_charset ); | |
| 24 | 24 | $file = @fopen($filePath, "w"); |
| 25 | 25 | fwrite($file, $encode_content); |
| 26 | 26 | @fclose($file); |
| ... | ... | @@ -28,16 +28,16 @@ class Encoder |
| 28 | 28 | |
| 29 | 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 | + if ($in_charset) | |
| 32 | + self::$in_charset = $in_charset; | |
| 33 | 33 | |
| 34 | - if (isset($out_charset)) | |
| 34 | + if ($out_charset) | |
| 35 | 35 | self::$out_charset = $out_charset; |
| 36 | 36 | |
| 37 | 37 | $result = array_map( |
| 38 | - function ( $array) { | |
| 38 | + function ($value) { | |
| 39 | 39 | |
| 40 | - return self::encodeString( $array, self::$in_charset, self::$out_charset ); | |
| 40 | + return self::encodeString( $value, self::$in_charset, self::$out_charset ); | |
| 41 | 41 | |
| 42 | 42 | }, |
| 43 | 43 | $array); |
| ... | ... | @@ -47,10 +47,10 @@ class Encoder |
| 47 | 47 | |
| 48 | 48 | public static function encodeString( $source, $in_charset = '', $out_charset = '' ){ |
| 49 | 49 | |
| 50 | - if (isset($in_charset)) | |
| 51 | - self::$in_charset = isset($in_charset); | |
| 50 | + if ($in_charset) | |
| 51 | + self::$in_charset = $in_charset; | |
| 52 | 52 | |
| 53 | - if (isset($out_charset)) | |
| 53 | + if ($out_charset) | |
| 54 | 54 | self::$out_charset = $out_charset; |
| 55 | 55 | |
| 56 | 56 | return iconv( self::$in_charset, self::$out_charset, $source ); | ... | ... |