Commit 67adc7885f3d5afe0b8340909d5bf0cdd18affea
1 parent
741ecd87
add converter interface, object creator, abstract class Parser
Showing
9 changed files
with
112 additions
and
118 deletions
Show diff stats
backend/controllers/ParserController.php
| ... | ... | @@ -15,6 +15,7 @@ use backend\models\Importers; |
| 15 | 15 | use yii\base\ErrorException; |
| 16 | 16 | use common\components\PriceWriter; |
| 17 | 17 | use common\components\CustomVarDamp; |
| 18 | +use common\components\CustomArrayHelper; | |
| 18 | 19 | |
| 19 | 20 | /** |
| 20 | 21 | * Parser controller |
| ... | ... | @@ -54,7 +55,6 @@ class ParserController extends BaseController |
| 54 | 55 | $model = new UploadFileParsingForm(); |
| 55 | 56 | // установим режим, 0 - ручная загрузка, 1 - автозагрузка |
| 56 | 57 | $model->mode = $mode; |
| 57 | - //CustomVarDamp::dumpAndDie(phpinfo()); | |
| 58 | 58 | return $this->render('index', ['model' => $model]); |
| 59 | 59 | } |
| 60 | 60 | |
| ... | ... | @@ -186,7 +186,7 @@ class ParserController extends BaseController |
| 186 | 186 | |
| 187 | 187 | // соотнесем отпарсенные данные с соответсивем полученным от пользователя |
| 188 | 188 | // для этого преобразуем массив отпарсенных данных - назначим ключи согласно соответствию |
| 189 | - $data = \Yii::$app->multiparser->convertToAssocArray($data, $arr , 'attr_'); | |
| 189 | + $data = CustomArrayHelper::createAssocArray( $data, $arr , 'attr_' ); | |
| 190 | 190 | |
| 191 | 191 | // запустим специальный класс который запишет данные в таблицы связанные с прайсами |
| 192 | 192 | $writer = new PriceWriter(); | ... | ... |
backend/models/UploadFileParsingForm.php
| ... | ... | @@ -75,8 +75,8 @@ class UploadFileParsingForm extends Model |
| 75 | 75 | throw new ErrorException("Ошибка чтения из файла прайса {$this->file_path}"); |
| 76 | 76 | } |
| 77 | 77 | // файл больше не нужен - данные прочитаны и сохранены в кеш |
| 78 | - if( file_exists($this->file_path) ) | |
| 79 | - unlink($this->file_path); | |
| 78 | +// if( file_exists($this->file_path) ) | |
| 79 | +// unlink($this->file_path); | |
| 80 | 80 | |
| 81 | 81 | return $data; |
| 82 | 82 | } | ... | ... |
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Created by PhpStorm. | |
| 4 | + * User: Tsurkanov | |
| 5 | + * Date: 20.10.2015 | |
| 6 | + * Time: 14:42 | |
| 7 | + */ | |
| 8 | + | |
| 9 | +namespace common\components; | |
| 10 | + | |
| 11 | + | |
| 12 | +class CustomArrayHelper extends \yii\helpers\ArrayHelper { | |
| 13 | + | |
| 14 | + /** | |
| 15 | + * @param $value_arr - двумерный массив значений, которому нужно присвоить ключи | |
| 16 | + * @param $key_array - ключи для вложенного массива | |
| 17 | + * @return array - таблица с проименованными колонками | |
| 18 | + */ | |
| 19 | + | |
| 20 | + public static function createAssocArray(array $value_arr, array $key_array, $key_prefix = '') | |
| 21 | + { | |
| 22 | + // очистка служебного префикса в массиве заголовков | |
| 23 | + if ($key_prefix) { | |
| 24 | + // @todo оптимизировать - два переворота массива - избыточно | |
| 25 | + $key_array = array_flip($key_array); | |
| 26 | + | |
| 27 | + array_walk($key_array, function (&$value, $key, $key_prefix) { | |
| 28 | + $value = str_replace($key_prefix, '', $value); | |
| 29 | + }, $key_prefix); | |
| 30 | + | |
| 31 | + $key_array = array_flip($key_array); | |
| 32 | + //уберем пустые элементы | |
| 33 | + $key_array = array_filter($key_array, function ($value) { | |
| 34 | + return $value !== ''; | |
| 35 | + }); | |
| 36 | + } | |
| 37 | + | |
| 38 | + array_walk($value_arr, | |
| 39 | + function (&$value, $key, $key_array) { | |
| 40 | + $res = $value; | |
| 41 | + foreach ($res as $sub_key => $sub_value) { | |
| 42 | + if (isset($key_array[$sub_key])) { | |
| 43 | + // если такой ключ в базовом массиве (массиве ключей) есть, то заменим новым, иначе просто удалим | |
| 44 | + $new_key = $key_array[$sub_key]; | |
| 45 | + if (!array_key_exists($new_key, $res)) { | |
| 46 | + $res[$new_key] = $value[$sub_key]; | |
| 47 | + } | |
| 48 | + } | |
| 49 | + unset($res[$sub_key]); | |
| 50 | + $value = $res; | |
| 51 | + } | |
| 52 | + | |
| 53 | + }, | |
| 54 | + $key_array); | |
| 55 | + | |
| 56 | + return $value_arr; | |
| 57 | + } | |
| 58 | + | |
| 59 | + /** | |
| 60 | + * @param $value_arr - двумерный массив к которому нужно добавить колонки | |
| 61 | + * @param $add_array - массив с колонками (ключи) и значениями колонок | |
| 62 | + * @return mixed | |
| 63 | + */ | |
| 64 | + public static function addColumns(array $value_arr, array $add_array) | |
| 65 | + { | |
| 66 | + $i = 0; | |
| 67 | + while ($i < count($value_arr)) { | |
| 68 | + foreach ($add_array as $add_key => $add_value) { | |
| 69 | + $value_arr[$i][$add_key] = $add_value; | |
| 70 | + } | |
| 71 | + $i++; | |
| 72 | + } | |
| 73 | + return $value_arr; | |
| 74 | + } | |
| 75 | +} | |
| 0 | 76 | \ No newline at end of file | ... | ... |
common/components/PriceWriter.php
| ... | ... | @@ -62,20 +62,20 @@ class PriceWriter |
| 62 | 62 | // преобразуем числовые значения |
| 63 | 63 | foreach ($this->data as &$row) { |
| 64 | 64 | |
| 65 | - $row['PRICE'] = \Yii::$app->multiparser->convertToFloat($row['PRICE']); | |
| 66 | - $row['BOX'] = \Yii::$app->multiparser->convertToInteger($row['BOX']); | |
| 65 | + $row['PRICE'] = \Yii::$app->converter->convertTo('float',$row['PRICE']); | |
| 66 | + $row['BOX'] = \Yii::$app->converter->convertTo('integer',$row['BOX']); | |
| 67 | 67 | // присвоим полный артикул |
| 68 | 68 | |
| 69 | 69 | $row['FULL_ARTICLE'] = $row['ARTICLE']; |
| 70 | 70 | if ((int)$this->configuration['delete_prefix']) { |
| 71 | - $row = \Yii::$app->multiparser->convertToArticle( $row, $this->configuration['importer_id'] ); | |
| 71 | + $row = \Yii::$app->converter->convertTo( 'Article', $row, ['importer_id' => $this->configuration['importer_id']] ); | |
| 72 | 72 | } else { |
| 73 | - $row['ARTICLE'] = \Yii::$app->multiparser->convertToArticle( $row['ARTICLE'] ); | |
| 73 | + $row['ARTICLE'] = \Yii::$app->converter->convertTo( 'Article', $row['ARTICLE'] ); | |
| 74 | 74 | } |
| 75 | 75 | |
| 76 | 76 | |
| 77 | 77 | if (isset($row['ADD_BOX'])) |
| 78 | - $row['ADD_BOX'] = \Yii::$app->multiparser->convertToInteger($row['ADD_BOX']); | |
| 78 | + $row['ADD_BOX'] = \Yii::$app->converter->convertTo( 'integer', $row['ADD_BOX'] ); | |
| 79 | 79 | |
| 80 | 80 | // проверим все ли обязательные колонки были указаны пользователем |
| 81 | 81 | $details_model->load(['Details' => $row]); |
| ... | ... | @@ -87,7 +87,7 @@ class PriceWriter |
| 87 | 87 | } |
| 88 | 88 | |
| 89 | 89 | // дополним данные значением импортера и даты обновления цены |
| 90 | - $this->data = \Yii::$app->multiparser->addColumns($this->data, ['IMPORT_ID' => $this->configuration['importer_id'], 'timestamp' => $update_date]); | |
| 90 | + $this->data = CustomArrayHelper::addColumns( $this->data, ['IMPORT_ID' => $this->configuration['importer_id'], 'timestamp' => $update_date] ); | |
| 91 | 91 | try { |
| 92 | 92 | //@todo add transaction |
| 93 | 93 | ... | ... |
common/components/parsers/CustomConverter.php
| ... | ... | @@ -10,71 +10,11 @@ use backend\models\ImportersPrefix; |
| 10 | 10 | class CustomConverter extends Converter |
| 11 | 11 | { |
| 12 | 12 | |
| 13 | - /** | |
| 14 | - * @param $value_arr - двумерный массив значений, которому нужно присвоить ключи | |
| 15 | - * @param $key_array - ключи для вложенного массива | |
| 16 | - * @return array - таблица с проименованными колонками | |
| 17 | - */ | |
| 18 | 13 | public static $sign; |
| 19 | 14 | public static $multiplier; |
| 20 | 15 | public static $importer_id; |
| 21 | 16 | public static $brand; |
| 22 | 17 | |
| 23 | - public static function convertToAssocArray(array $value_arr, array $key_array, $key_prefix = '') | |
| 24 | - { | |
| 25 | - // очистка служебного префикса в массиве заголовков | |
| 26 | - if ($key_prefix) { | |
| 27 | - // @todo оптимизировать - два переворота массива - избыточно | |
| 28 | - $key_array = array_flip($key_array); | |
| 29 | - | |
| 30 | - array_walk($key_array, function (&$value, $key, $key_prefix) { | |
| 31 | - $value = str_replace($key_prefix, '', $value); | |
| 32 | - }, $key_prefix); | |
| 33 | - | |
| 34 | - $key_array = array_flip($key_array); | |
| 35 | - //уберем пустые элементы | |
| 36 | - $key_array = array_filter($key_array, function ($value) { | |
| 37 | - return $value !== ''; | |
| 38 | - }); | |
| 39 | - } | |
| 40 | - | |
| 41 | - array_walk($value_arr, | |
| 42 | - function (&$value, $key, $key_array) { | |
| 43 | - $res = $value; | |
| 44 | - foreach ($res as $sub_key => $sub_value) { | |
| 45 | - if (isset($key_array[$sub_key])) { | |
| 46 | - // если такой ключ в базовом массиве (массиве ключей) есть, то заменим новым, иначе просто удалим | |
| 47 | - $new_key = $key_array[$sub_key]; | |
| 48 | - if (!array_key_exists($new_key, $res)) { | |
| 49 | - $res[$new_key] = $value[$sub_key]; | |
| 50 | - } | |
| 51 | - } | |
| 52 | - unset($res[$sub_key]); | |
| 53 | - $value = $res; | |
| 54 | - } | |
| 55 | - | |
| 56 | - }, | |
| 57 | - $key_array); | |
| 58 | - | |
| 59 | - return $value_arr; | |
| 60 | - } | |
| 61 | - | |
| 62 | - /** | |
| 63 | - * @param $value_arr - двумерный массив к которому нужно добавить колонки | |
| 64 | - * @param $add_array - массив с колонками (ключи) и значениями колонок | |
| 65 | - * @return mixed | |
| 66 | - */ | |
| 67 | - public function addColumns(array $value_arr, array $add_array) | |
| 68 | - { | |
| 69 | - $i = 0; | |
| 70 | - while ($i < count($value_arr)) { | |
| 71 | - foreach ($add_array as $add_key => $add_value) { | |
| 72 | - $value_arr[$i][$add_key] = $add_value; | |
| 73 | - } | |
| 74 | - $i++; | |
| 75 | - } | |
| 76 | - return $value_arr; | |
| 77 | - } | |
| 78 | 18 | |
| 79 | 19 | public static function convertToDetails(array $row) |
| 80 | 20 | { |
| ... | ... | @@ -143,11 +83,8 @@ class CustomConverter extends Converter |
| 143 | 83 | |
| 144 | 84 | } |
| 145 | 85 | |
| 146 | - public static function convertToArticle( $value, $importer_id = '' ) | |
| 86 | + public static function convertToArticle( $value ) | |
| 147 | 87 | { |
| 148 | - if(isset( $importer_id )){ | |
| 149 | - self::$importer_id = $importer_id; | |
| 150 | - } | |
| 151 | 88 | |
| 152 | 89 | if (is_array($value)) { |
| 153 | 90 | |
| ... | ... | @@ -189,7 +126,7 @@ class CustomConverter extends Converter |
| 189 | 126 | |
| 190 | 127 | public static function convertToBrand($value) |
| 191 | 128 | { |
| 192 | - $res = parent::convertToEncode($value);; | |
| 129 | + $res = self::convertToEncode($value);; | |
| 193 | 130 | $res = trim(strtoupper($res)); |
| 194 | 131 | $res = str_replace("Ä", "A", str_replace("Ö", "O", str_replace("Ü", "U", str_replace("Ë", "E", str_replace("Ò", "O", $res))))); |
| 195 | 132 | $res = str_replace(array('@', '#', '~', '"', "'", "?", "!"), '', $res); |
| ... | ... | @@ -197,12 +134,5 @@ class CustomConverter extends Converter |
| 197 | 134 | return $res; |
| 198 | 135 | } |
| 199 | 136 | |
| 200 | - public static function convertToString($value) | |
| 201 | - { | |
| 202 | - $value = parent::convertToEncode($value); | |
| 203 | - | |
| 204 | - return str_replace(array('!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', '=', '-', '~', '`', '"', "'", ' ', '№', '%', ';', ':', '[', ']', '{', '}', '*', '?', '/', '\'', '|', '.', ',', '<', '>', '\\'), '', $value); | |
| 205 | - } | |
| 206 | - | |
| 207 | 137 | |
| 208 | 138 | } |
| 209 | 139 | \ No newline at end of file | ... | ... |
common/components/parsers/CustomCsvParser.php
| ... | ... | @@ -19,31 +19,19 @@ class CustomCsvParser extends \yii\multiparser\CsvParser { |
| 19 | 19 | public $last_line = 100; |
| 20 | 20 | //public $hasHeaderRow = true; |
| 21 | 21 | // public $keys = ['first','second', 'third', 'forth', 'fifth']; |
| 22 | - public function setupConverter() | |
| 23 | - { | |
| 24 | - if (!count($this->converter_conf)) { | |
| 25 | - if ($this->hasHeaderRow) { | |
| 26 | - // если у файла есть заголовок, то в результате имеем ассоциативный массив | |
| 27 | - $this->converter_conf['hasKey'] = 1; | |
| 28 | - | |
| 29 | - } | |
| 30 | - | |
| 31 | - } | |
| 32 | -// $this->converter = \Yii::createObject( $this->converter_conf ); | |
| 33 | -// CustomVarDamp::dumpAndDie($this->converter); | |
| 34 | - } | |
| 35 | - | |
| 36 | - /** | |
| 37 | - * @param $arr | |
| 38 | - * @return mixed | |
| 39 | - * преобразовует значения прочитанного массива в нужные типы, согласно конфигурации конвертера | |
| 40 | - */ | |
| 41 | - protected function convert($arr) | |
| 42 | - { | |
| 43 | - $arr = \Yii::$app->multiparser->convertByConfiguration( $arr, $this->converter_conf ); | |
| 44 | - | |
| 45 | - return $arr; | |
| 46 | - | |
| 47 | - } | |
| 22 | +// public function setupConverter() | |
| 23 | +// { | |
| 24 | +// if (!count($this->converter_conf)) { | |
| 25 | +// if ($this->hasHeaderRow) { | |
| 26 | +// // если у файла есть заголовок, то в результате имеем ассоциативный массив | |
| 27 | +// $this->converter_conf['hasKey'] = 1; | |
| 28 | +// | |
| 29 | +// } | |
| 30 | +// | |
| 31 | +// } | |
| 32 | +//// $this->converter = \Yii::createObject( $this->converter_conf ); | |
| 33 | +// } | |
| 34 | + | |
| 35 | + | |
| 48 | 36 | |
| 49 | 37 | } |
| 50 | 38 | \ No newline at end of file | ... | ... |
common/components/parsers/config.php
| ... | ... | @@ -5,7 +5,7 @@ |
| 5 | 5 | ['class' => 'common\components\parsers\CustomCsvParser', |
| 6 | 6 | 'auto_detect_first_line' => true, |
| 7 | 7 | 'converter_conf' => [ |
| 8 | - //'class' => ' common\components\parsers\CustomConverter', // @todo переделать на компонент | |
| 8 | + 'class' => 'common\components\parsers\CustomConverter', | |
| 9 | 9 | 'configuration' => ["encode" => 'DESCR'],] |
| 10 | 10 | ], |
| 11 | 11 | 'console' => |
| ... | ... | @@ -13,7 +13,7 @@ |
| 13 | 13 | 'auto_detect_first_line' => true, |
| 14 | 14 | 'hasHeaderRow' => true, |
| 15 | 15 | 'converter_conf' => [ |
| 16 | - //'class' => ' common\components\parsers\CustomConverter', | |
| 16 | + 'class' => ' common\components\parsers\CustomConverter', | |
| 17 | 17 | 'hasKey' => 1, |
| 18 | 18 | 'configuration' => ["string" => 'DESCR', |
| 19 | 19 | "float" => 'PRICE', |
| ... | ... | @@ -42,7 +42,7 @@ |
| 42 | 42 | 'hasHeaderRow' => true, |
| 43 | 43 | 'keys' =>['ARTICLE', 'CROSS_ARTICLE', 'BRAND', 'CROSS_BRAND'], |
| 44 | 44 | 'converter_conf' => [ |
| 45 | - //'class' => ' common\components\parsers\CustomConverter', | |
| 45 | + 'class' => ' common\components\parsers\CustomConverter', | |
| 46 | 46 | 'hasKey' => 1, |
| 47 | 47 | 'configuration' => [ |
| 48 | 48 | "brand" => ['BRAND', 'CROSS_BRAND'], |
| ... | ... | @@ -55,7 +55,7 @@ |
| 55 | 55 | ['class' => 'yii\multiparser\XmlParser', |
| 56 | 56 | 'node' => 'Товар', |
| 57 | 57 | 'converter_conf' => [ |
| 58 | - //'class' => ' common\components\parsers\CustomConverter', | |
| 58 | + 'class' => ' common\components\parsers\CustomConverter', | |
| 59 | 59 | 'hasKey' => 1, |
| 60 | 60 | 'configuration' => ["details" => [] |
| 61 | 61 | ],], | ... | ... |
common/config/main.php
| ... | ... | @@ -15,14 +15,15 @@ return [ |
| 15 | 15 | ] |
| 16 | 16 | ], |
| 17 | 17 | 'multiparser'=>[ |
| 18 | - | |
| 19 | 18 | 'class' => 'yii\multiparser\YiiMultiparser', |
| 20 | 19 | 'configuration' => $mp_configuration, |
| 21 | - 'as behavior' => [ | |
| 22 | - 'class' => 'common\components\parsers\CustomConverter', | |
| 20 | + ], | |
| 21 | + 'converter'=>[ | |
| 22 | + 'class' => 'yii\multiparser\YiiConverter', | |
| 23 | + 'configuration' => [ | |
| 24 | + 'class' => 'common\components\parsers\CustomConverter' | |
| 25 | + ], | |
| 23 | 26 | ], |
| 24 | 27 | |
| 25 | 28 | ], |
| 26 | - ], | |
| 27 | - | |
| 28 | 29 | ]; | ... | ... |
common/models/DetailsCurrency.php
| ... | ... | @@ -6,7 +6,7 @@ use Yii; |
| 6 | 6 | |
| 7 | 7 | /** |
| 8 | 8 | * This is the model class for table "{{%details_currency}}". |
| 9 | - * | |
| 9 | + * w_details_currency - СПЕЦИАЛЬНО СОЗАДННАЯ ВЬЮШКА ДЛЯ ДОСТУПА К СПИСКУ ТОВАРОВ С ВАЛЮТАМИ И ИХ КУРСАМИ | |
| 10 | 10 | * @property string $ID |
| 11 | 11 | * @property string $IMPORT_ID |
| 12 | 12 | * @property string $BRAND | ... | ... |