diff --git a/backend/components/parsers/CustomCsvParser.php b/backend/components/parsers/CustomCsvParser.php index 55f3153..5674511 100644 --- a/backend/components/parsers/CustomCsvParser.php +++ b/backend/components/parsers/CustomCsvParser.php @@ -11,7 +11,7 @@ namespace backend\components\parsers; class CustomCsvParser extends \yii\multiparser\CsvParser { - //public $last_line = 10; + public $last_line = 10; //public $hasHeaderRow = true; // public $keys = ['first','second', 'third', 'forth', 'fifth']; public function setupConverter() diff --git a/backend/components/parsers/config.php b/backend/components/parsers/config.php index a4b5009..6e14618 100644 --- a/backend/components/parsers/config.php +++ b/backend/components/parsers/config.php @@ -4,7 +4,7 @@ ['web' => ['class' => 'backend\components\parsers\CustomCsvParser', 'auto_detect_first_line' => true, - 'converter_conf' => ['class' => 'yii\multiparser\CsvConverter', + 'converter_conf' => ['class' => 'yii\multiparser\Converter', 'configuration' => [ "string" => 'DESCR' ] @@ -20,7 +20,23 @@ "ADD_BOX"=> 'В пути', "GROUP" => 'Группа RG' ], - ]]; + ], + 'xml' => + ['web' => + ['class' => 'yii\multiparser\XmlParser', + 'node' => 'Товар',], + + 'basic_column' => [ + "BRAND" => 'Производитель', + "ARTICLE"=> 'Код', + "PRICE" => 'Розница', + "DESCR" => 'Наименование', + "BOX" => 'Колво', + "ADD_BOX"=> 'Ожидаемое', + "GROUP" => 'Группа' + ], + ] + ]; //[ diff --git a/backend/controllers/ParserController.php b/backend/controllers/ParserController.php index 633d432..eb6dab9 100644 --- a/backend/controllers/ParserController.php +++ b/backend/controllers/ParserController.php @@ -122,7 +122,8 @@ public function actionWrite() $arr = $model->toArray(); $data = json_decode( Yii::$app->getCache()->get( 'parser_data' ),true ); - CustomVarDamp::dumpAndDie(DynamicFormHelper::CreateAssocArray($data, $arr)); + // CustomVarDamp::dumpAndDie(DynamicFormHelper::CreateAssocArray($data, $arr)); + CustomVarDamp::dumpAndDie($arr); } diff --git a/backend/models/UploadFileParsingForm.php b/backend/models/UploadFileParsingForm.php index 9377a59..ef94bc4 100644 --- a/backend/models/UploadFileParsingForm.php +++ b/backend/models/UploadFileParsingForm.php @@ -52,7 +52,6 @@ class UploadFileParsingForm extends Model public function readFile($filePath){ $data = Yii::$app->multiparser->parse($filePath); - // \common\components\CustomVarDamp::dumpAndDie($data); if( !is_array($data) ){ $data = ['No results']; } diff --git a/framework/core/Convert.php b/framework/core/Convert.php index 89a4367..bd1f645 100644 --- a/framework/core/Convert.php +++ b/framework/core/Convert.php @@ -166,63 +166,63 @@ class Convert { } /** - * Converts an XML string to a PHP array - * See http://phpsecurity.readthedocs.org/en/latest/Injection-Attacks.html#xml-external-entity-injection - * - * @uses recursiveXMLToArray() - * @param string $val - * @param boolean $disableDoctypes Disables the use of DOCTYPE, and will trigger an error if encountered. - * false by default. - * @param boolean $disableExternals Disables the loading of external entities. false by default. - * @return array - */ - public static function xml2array($val, $disableDoctypes = false, $disableExternals = false) { - // Check doctype - if($disableDoctypes && preg_match('/\<\!DOCTYPE.+]\>/', $val)) { - throw new InvalidArgumentException('XML Doctype parsing disabled'); - } + * Converts an XML string to a PHP array + * See http://phpsecurity.readthedocs.org/en/latest/Injection-Attacks.html#xml-external-entity-injection + * + * @uses recursiveXMLToArray() + * @param string $val + * @param boolean $disableDoctypes Disables the use of DOCTYPE, and will trigger an error if encountered. + * false by default. + * @param boolean $disableExternals Disables the loading of external entities. false by default. + * @return array + */ + public static function xml2array($val, $disableDoctypes = false, $disableExternals = false) { + // Check doctype + if($disableDoctypes && preg_match('/\<\!DOCTYPE.+]\>/', $val)) { + throw new InvalidArgumentException('XML Doctype parsing disabled'); + } - // Disable external entity loading - if($disableExternals) $oldVal = libxml_disable_entity_loader($disableExternals); - try { - $xml = new SimpleXMLElement($val); - $result = self::recursiveXMLToArray($xml); - } catch(Exception $ex) { - if($disableExternals) libxml_disable_entity_loader($oldVal); - throw $ex; - } - if($disableExternals) libxml_disable_entity_loader($oldVal); - return $result; - } + // Disable external entity loading + if($disableExternals) $oldVal = libxml_disable_entity_loader($disableExternals); + try { + $xml = new SimpleXMLElement($val); + $result = self::recursiveXMLToArray($xml); + } catch(Exception $ex) { + if($disableExternals) libxml_disable_entity_loader($oldVal); + throw $ex; + } + if($disableExternals) libxml_disable_entity_loader($oldVal); + return $result; + } - /** - * Convert a XML string to a PHP array recursively. Do not - * call this function directly, Please use {@link Convert::xml2array()} - * - * @param SimpleXMLElement - * - * @return mixed - */ - protected static function recursiveXMLToArray($xml) { - if(is_object($xml) && get_class($xml) == 'SimpleXMLElement') { - $attributes = $xml->attributes(); - foreach($attributes as $k => $v) { - if($v) $a[$k] = (string) $v; - } - $x = $xml; - $xml = get_object_vars($xml); - } - if(is_array($xml)) { - if(count($xml) == 0) return (string) $x; // for CDATA - foreach($xml as $key => $value) { - $r[$key] = self::recursiveXMLToArray($value); - } - if(isset($a)) $r['@'] = $a; // Attributes - return $r; - } - - return (string) $xml; - } + /** + * Convert a XML string to a PHP array recursively. Do not + * call this function directly, Please use {@link Convert::xml2array()} + * + * @param SimpleXMLElement + * + * @return mixed + */ + protected static function recursiveXMLToArray($xml) { + if(is_object($xml) && get_class($xml) == 'SimpleXMLElement') { + $attributes = $xml->attributes(); + foreach($attributes as $k => $v) { + if($v) $a[$k] = (string) $v; + } + $x = $xml; + $xml = get_object_vars($xml); + } + if(is_array($xml)) { + if(count($xml) == 0) return (string) $x; // for CDATA + foreach($xml as $key => $value) { + $r[$key] = self::recursiveXMLToArray($value); + } + if(isset($a)) $r['@'] = $a; // Attributes + return $r; + } + + return (string) $xml; + } /** * Create a link if the string is a valid URL diff --git a/vendor/yiisoft/multiparser/CSVConverter.php b/vendor/yiisoft/multiparser/CSVConverter.php deleted file mode 100644 index 8f535ff..0000000 --- a/vendor/yiisoft/multiparser/CSVConverter.php +++ /dev/null @@ -1,131 +0,0 @@ -configuration['hasKey'] ); - foreach ( $this->configuration as $key => $value ) { - - if ( $hasKey ){ - // у нас ассоциативный массив, и мы можем конвертировать каждое значение в отдельности - if ( is_array( $value ) ) { - foreach ($value as $sub_value) { - if (isset($arr[$sub_value])) { - // конвертируем только те ячейки которые сопоставлены в прочитанном массиве с колонками в конфигурационном файле - $result[$arr[$sub_value]] = self::$key( $arr[$sub_value] ); - } - - } - } else { - - if (isset($arr[$value])) { - // конвертируем только те ячейки которые сопоставлены в прочитанном массиве с колонками в конфигурационном файле - $result[$arr[$value]] = self::$key( $arr[$value] ); - } - - } - - } else { - // нет заголовка - мы можем конвертировать только строку в целом - $result = self::$key( $arr ); - } - - } - - return $result; - } - - -} \ No newline at end of file diff --git a/vendor/yiisoft/multiparser/Converter.php b/vendor/yiisoft/multiparser/Converter.php new file mode 100644 index 0000000..360e695 --- /dev/null +++ b/vendor/yiisoft/multiparser/Converter.php @@ -0,0 +1,148 @@ +configuration['hasKey'] ); + foreach ( $this->configuration as $key => $value ) { + + if ( $hasKey ){ + // у нас ассоциативный массив, и мы можем конвертировать каждое значение в отдельности + if ( is_array( $value ) ) { + foreach ($value as $sub_value) { + if (isset($arr[$sub_value])) { + // конвертируем только те ячейки которые сопоставлены в прочитанном массиве с колонками в конфигурационном файле + $result[$arr[$sub_value]] = self::$key( $arr[$sub_value] ); + } + + } + } else { + + if (isset($arr[$value])) { + // конвертируем только те ячейки которые сопоставлены в прочитанном массиве с колонками в конфигурационном файле + $result[$arr[$value]] = self::$key( $arr[$value] ); + } + + } + + } else { + // нет заголовка - мы можем конвертировать только строку в целом + $result = self::$key( $arr ); + } + + } + + return $result; + } + + + +} \ No newline at end of file diff --git a/vendor/yiisoft/multiparser/CsvParser.php b/vendor/yiisoft/multiparser/CsvParser.php index bb70d41..51f284a 100644 --- a/vendor/yiisoft/multiparser/CsvParser.php +++ b/vendor/yiisoft/multiparser/CsvParser.php @@ -73,7 +73,7 @@ class CsvParser implements ParserInterface public function setupConverter() { if (!count($this->converter_conf)) { - $this->converter = new CSVConverter(); + $this->converter = new Converter(); if ($this->hasHeaderRow) { // если у файла есть заголовок, то в результате имеем ассоциативный массив $this->converter_conf['hasKey'] = 1; diff --git a/vendor/yiisoft/multiparser/DynamicFormHelper.php b/vendor/yiisoft/multiparser/DynamicFormHelper.php index 786a59e..515fc29 100644 --- a/vendor/yiisoft/multiparser/DynamicFormHelper.php +++ b/vendor/yiisoft/multiparser/DynamicFormHelper.php @@ -75,11 +75,26 @@ class DynamicFormHelper $result = array_map( function ($value) { - return array_combine( self::$key_array, $value ); + foreach ($value as $key => $sub_value) { + return self::change_key( $key, self::$key_array[$sub_value], $value ); + } + + + // array_combine( self::$key_array, $value ); }, $value_arr); return $result; } + protected static function change_key( $key, $new_key, $arr ){ + $res = $arr; + if(!array_key_exists($new_key,$arr)){ + $arr[$new_key]=$arr[$key]; + unset($arr[$key]); + $res = $arr; + } + return $res; + } + } \ No newline at end of file diff --git a/vendor/yiisoft/multiparser/XmlParser.php b/vendor/yiisoft/multiparser/XmlParser.php index 77cd03c..d376a4a 100644 --- a/vendor/yiisoft/multiparser/XmlParser.php +++ b/vendor/yiisoft/multiparser/XmlParser.php @@ -10,44 +10,47 @@ namespace yii\multiparser; class XmlParser implements ParserInterface{ + /** @var экземляр SplFileObject читаемого файла */ + public $file; + public $node; + public function read() { - // TODO: Implement read() method. + $file = $this->file; + $result = self::xml2array( $file->getPathname() ); + if ( isset($this->node) ) { + $result = $result[$this->node]; + } + + DynamicFormHelper::CreateAssocArray($result, \Yii::$app->multiparser->getConfiguration('xml','basic_column')); + \common\components\CustomVarDamp::dumpAndDie($result); } public function setup() { - // TODO: Implement setup() method. + + } /** * Converts an XML string to a PHP array - * See http://phpsecurity.readthedocs.org/en/latest/Injection-Attacks.html#xml-external-entity-injection * * @uses recursiveXMLToArray() - * @param string $val - * @param boolean $disableDoctypes Disables the use of DOCTYPE, and will trigger an error if encountered. - * false by default. - * @param boolean $disableExternals Disables the loading of external entities. false by default. + * @param string $file_path * @return array */ - public static function xml2array($val, $disableDoctypes = false, $disableExternals = false) { - // Check doctype - if($disableDoctypes && preg_match('/\<\!DOCTYPE.+]\>/', $val)) { - throw new InvalidArgumentException('XML Doctype parsing disabled'); - } + protected static function xml2array( $file_path ) { - // Disable external entity loading - if($disableExternals) $oldVal = libxml_disable_entity_loader($disableExternals); try { - $xml = new SimpleXMLElement($val); - $result = self::recursiveXMLToArray($xml); + $xml = new \SimpleXMLElement( $file_path, 0, true ); + //\common\components\CustomVarDamp::dumpAndDie($xml->children()->children()); + $result = self::recursiveXMLToArray( $xml ); } catch(Exception $ex) { - if($disableExternals) libxml_disable_entity_loader($oldVal); + throw $ex; } - if($disableExternals) libxml_disable_entity_loader($oldVal); + return $result; } @@ -68,6 +71,8 @@ class XmlParser implements ParserInterface{ $x = $xml; $xml = get_object_vars($xml); } + + //\common\components\CustomVarDamp::dump($xml); if(is_array($xml)) { if(count($xml) == 0) return (string) $x; // for CDATA foreach($xml as $key => $value) { @@ -76,7 +81,7 @@ class XmlParser implements ParserInterface{ if(isset($a)) $r['@'] = $a; // Attributes return $r; } - + //\common\components\CustomVarDamp::dumpAndDie($xml); return (string) $xml; } } \ No newline at end of file -- libgit2 0.21.4