Commit 86295c156d9421249b8922303e20496d078777e1

Authored by Mihail
1 parent 492d8ac1

XMLparser - read function

backend/components/parsers/CustomCsvParser.php
... ... @@ -11,7 +11,7 @@ namespace backend\components\parsers;
11 11  
12 12 class CustomCsvParser extends \yii\multiparser\CsvParser {
13 13  
14   - //public $last_line = 10;
  14 + public $last_line = 10;
15 15 //public $hasHeaderRow = true;
16 16 // public $keys = ['first','second', 'third', 'forth', 'fifth'];
17 17 public function setupConverter()
... ...
backend/components/parsers/config.php
... ... @@ -4,7 +4,7 @@
4 4 ['web' =>
5 5 ['class' => 'backend\components\parsers\CustomCsvParser',
6 6 'auto_detect_first_line' => true,
7   - 'converter_conf' => ['class' => 'yii\multiparser\CsvConverter',
  7 + 'converter_conf' => ['class' => 'yii\multiparser\Converter',
8 8 'configuration' => [
9 9 "string" => 'DESCR'
10 10 ]
... ... @@ -20,7 +20,23 @@
20 20 "ADD_BOX"=> 'В пути',
21 21 "GROUP" => 'Группа RG'
22 22 ],
23   - ]];
  23 + ],
  24 + 'xml' =>
  25 + ['web' =>
  26 + ['class' => 'yii\multiparser\XmlParser',
  27 + 'node' => 'Товар',],
  28 +
  29 + 'basic_column' => [
  30 + "BRAND" => 'Производитель',
  31 + "ARTICLE"=> 'Код',
  32 + "PRICE" => 'Розница',
  33 + "DESCR" => 'Наименование',
  34 + "BOX" => 'Колво',
  35 + "ADD_BOX"=> 'Ожидаемое',
  36 + "GROUP" => 'Группа'
  37 + ],
  38 + ]
  39 + ];
24 40  
25 41  
26 42 //[
... ...
backend/controllers/ParserController.php
... ... @@ -122,7 +122,8 @@ public function actionWrite()
122 122 $arr = $model->toArray();
123 123 $data = json_decode( Yii::$app->getCache()->get( 'parser_data' ),true );
124 124  
125   - CustomVarDamp::dumpAndDie(DynamicFormHelper::CreateAssocArray($data, $arr));
  125 + // CustomVarDamp::dumpAndDie(DynamicFormHelper::CreateAssocArray($data, $arr));
  126 + CustomVarDamp::dumpAndDie($arr);
126 127 }
127 128  
128 129  
... ...
backend/models/UploadFileParsingForm.php
... ... @@ -52,7 +52,6 @@ class UploadFileParsingForm extends Model
52 52 public function readFile($filePath){
53 53  
54 54 $data = Yii::$app->multiparser->parse($filePath);
55   - // \common\components\CustomVarDamp::dumpAndDie($data);
56 55 if( !is_array($data) ){
57 56 $data = ['No results'];
58 57 }
... ...
framework/core/Convert.php
... ... @@ -166,63 +166,63 @@ class Convert {
166 166 }
167 167  
168 168 /**
169   - * Converts an XML string to a PHP array
170   - * See http://phpsecurity.readthedocs.org/en/latest/Injection-Attacks.html#xml-external-entity-injection
171   - *
172   - * @uses recursiveXMLToArray()
173   - * @param string $val
174   - * @param boolean $disableDoctypes Disables the use of DOCTYPE, and will trigger an error if encountered.
175   - * false by default.
176   - * @param boolean $disableExternals Disables the loading of external entities. false by default.
177   - * @return array
178   - */
179   - public static function xml2array($val, $disableDoctypes = false, $disableExternals = false) {
180   - // Check doctype
181   - if($disableDoctypes && preg_match('/\<\!DOCTYPE.+]\>/', $val)) {
182   - throw new InvalidArgumentException('XML Doctype parsing disabled');
183   - }
  169 + * Converts an XML string to a PHP array
  170 + * See http://phpsecurity.readthedocs.org/en/latest/Injection-Attacks.html#xml-external-entity-injection
  171 + *
  172 + * @uses recursiveXMLToArray()
  173 + * @param string $val
  174 + * @param boolean $disableDoctypes Disables the use of DOCTYPE, and will trigger an error if encountered.
  175 + * false by default.
  176 + * @param boolean $disableExternals Disables the loading of external entities. false by default.
  177 + * @return array
  178 + */
  179 + public static function xml2array($val, $disableDoctypes = false, $disableExternals = false) {
  180 + // Check doctype
  181 + if($disableDoctypes && preg_match('/\<\!DOCTYPE.+]\>/', $val)) {
  182 + throw new InvalidArgumentException('XML Doctype parsing disabled');
  183 + }
184 184  
185   - // Disable external entity loading
186   - if($disableExternals) $oldVal = libxml_disable_entity_loader($disableExternals);
187   - try {
188   - $xml = new SimpleXMLElement($val);
189   - $result = self::recursiveXMLToArray($xml);
190   - } catch(Exception $ex) {
191   - if($disableExternals) libxml_disable_entity_loader($oldVal);
192   - throw $ex;
193   - }
194   - if($disableExternals) libxml_disable_entity_loader($oldVal);
195   - return $result;
196   - }
  185 + // Disable external entity loading
  186 + if($disableExternals) $oldVal = libxml_disable_entity_loader($disableExternals);
  187 + try {
  188 + $xml = new SimpleXMLElement($val);
  189 + $result = self::recursiveXMLToArray($xml);
  190 + } catch(Exception $ex) {
  191 + if($disableExternals) libxml_disable_entity_loader($oldVal);
  192 + throw $ex;
  193 + }
  194 + if($disableExternals) libxml_disable_entity_loader($oldVal);
  195 + return $result;
  196 + }
197 197  
198   - /**
199   - * Convert a XML string to a PHP array recursively. Do not
200   - * call this function directly, Please use {@link Convert::xml2array()}
201   - *
202   - * @param SimpleXMLElement
203   - *
204   - * @return mixed
205   - */
206   - protected static function recursiveXMLToArray($xml) {
207   - if(is_object($xml) && get_class($xml) == 'SimpleXMLElement') {
208   - $attributes = $xml->attributes();
209   - foreach($attributes as $k => $v) {
210   - if($v) $a[$k] = (string) $v;
211   - }
212   - $x = $xml;
213   - $xml = get_object_vars($xml);
214   - }
215   - if(is_array($xml)) {
216   - if(count($xml) == 0) return (string) $x; // for CDATA
217   - foreach($xml as $key => $value) {
218   - $r[$key] = self::recursiveXMLToArray($value);
219   - }
220   - if(isset($a)) $r['@'] = $a; // Attributes
221   - return $r;
222   - }
223   -
224   - return (string) $xml;
225   - }
  198 + /**
  199 + * Convert a XML string to a PHP array recursively. Do not
  200 + * call this function directly, Please use {@link Convert::xml2array()}
  201 + *
  202 + * @param SimpleXMLElement
  203 + *
  204 + * @return mixed
  205 + */
  206 + protected static function recursiveXMLToArray($xml) {
  207 + if(is_object($xml) && get_class($xml) == 'SimpleXMLElement') {
  208 + $attributes = $xml->attributes();
  209 + foreach($attributes as $k => $v) {
  210 + if($v) $a[$k] = (string) $v;
  211 + }
  212 + $x = $xml;
  213 + $xml = get_object_vars($xml);
  214 + }
  215 + if(is_array($xml)) {
  216 + if(count($xml) == 0) return (string) $x; // for CDATA
  217 + foreach($xml as $key => $value) {
  218 + $r[$key] = self::recursiveXMLToArray($value);
  219 + }
  220 + if(isset($a)) $r['@'] = $a; // Attributes
  221 + return $r;
  222 + }
  223 +
  224 + return (string) $xml;
  225 + }
226 226  
227 227 /**
228 228 * Create a link if the string is a valid URL
... ...