From 86295c156d9421249b8922303e20496d078777e1 Mon Sep 17 00:00:00 2001 From: Mihail Date: Fri, 11 Sep 2015 18:05:57 +0300 Subject: [PATCH] XMLparser - read function --- backend/components/parsers/CustomCsvParser.php | 2 +- backend/components/parsers/config.php | 20 ++++++++++++++++++-- backend/controllers/ParserController.php | 3 ++- backend/models/UploadFileParsingForm.php | 1 - framework/core/Convert.php | 110 +++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------------------------------------- 5 files changed, 76 insertions(+), 60 deletions(-) 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 -- libgit2 0.21.4