Commit d21c5c5fe8fbf85aebc8390c0ab2eead4a8c0cf1
1 parent
aa518ad3
XMLparser - read function
Showing
9 changed files
with
135 additions
and
82 deletions
Show diff stats
backend/components/parsers/CustomCsvParser.php
@@ -11,7 +11,7 @@ namespace backend\components\parsers; | @@ -11,7 +11,7 @@ namespace backend\components\parsers; | ||
11 | 11 | ||
12 | class CustomCsvParser extends \yii\multiparser\CsvParser { | 12 | class CustomCsvParser extends \yii\multiparser\CsvParser { |
13 | 13 | ||
14 | - //public $last_line = 10; | 14 | + public $last_line = 10; |
15 | //public $hasHeaderRow = true; | 15 | //public $hasHeaderRow = true; |
16 | // public $keys = ['first','second', 'third', 'forth', 'fifth']; | 16 | // public $keys = ['first','second', 'third', 'forth', 'fifth']; |
17 | public function setupConverter() | 17 | public function setupConverter() |
backend/components/parsers/config.php
@@ -4,7 +4,7 @@ | @@ -4,7 +4,7 @@ | ||
4 | ['web' => | 4 | ['web' => |
5 | ['class' => 'backend\components\parsers\CustomCsvParser', | 5 | ['class' => 'backend\components\parsers\CustomCsvParser', |
6 | 'auto_detect_first_line' => true, | 6 | 'auto_detect_first_line' => true, |
7 | - 'converter_conf' => ['class' => 'yii\multiparser\CsvConverter', | 7 | + 'converter_conf' => ['class' => 'yii\multiparser\Converter', |
8 | 'configuration' => [ | 8 | 'configuration' => [ |
9 | "string" => 'DESCR' | 9 | "string" => 'DESCR' |
10 | ] | 10 | ] |
@@ -20,7 +20,23 @@ | @@ -20,7 +20,23 @@ | ||
20 | "ADD_BOX"=> 'В пути', | 20 | "ADD_BOX"=> 'В пути', |
21 | "GROUP" => 'Группа RG' | 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,7 +122,8 @@ public function actionWrite() | ||
122 | $arr = $model->toArray(); | 122 | $arr = $model->toArray(); |
123 | $data = json_decode( Yii::$app->getCache()->get( 'parser_data' ),true ); | 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,7 +52,6 @@ class UploadFileParsingForm extends Model | ||
52 | public function readFile($filePath){ | 52 | public function readFile($filePath){ |
53 | 53 | ||
54 | $data = Yii::$app->multiparser->parse($filePath); | 54 | $data = Yii::$app->multiparser->parse($filePath); |
55 | - // \common\components\CustomVarDamp::dumpAndDie($data); | ||
56 | if( !is_array($data) ){ | 55 | if( !is_array($data) ){ |
57 | $data = ['No results']; | 56 | $data = ['No results']; |
58 | } | 57 | } |
framework/core/Convert.php
@@ -166,63 +166,63 @@ class Convert { | @@ -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 | * Create a link if the string is a valid URL | 228 | * Create a link if the string is a valid URL |
vendor/yiisoft/multiparser/CSVConverter.php renamed to vendor/yiisoft/multiparser/Converter.php
@@ -9,7 +9,7 @@ | @@ -9,7 +9,7 @@ | ||
9 | namespace yii\multiparser; | 9 | namespace yii\multiparser; |
10 | 10 | ||
11 | // класс который содержит преобразователи значений (фильтры) используемые при парсинге | 11 | // класс который содержит преобразователи значений (фильтры) используемые при парсинге |
12 | -class CSVConverter | 12 | +class Converter |
13 | { | 13 | { |
14 | 14 | ||
15 | const METHOD_PREFIX = 'convertTo'; | 15 | const METHOD_PREFIX = 'convertTo'; |
@@ -65,6 +65,22 @@ class CSVConverter | @@ -65,6 +65,22 @@ class CSVConverter | ||
65 | return $res; | 65 | return $res; |
66 | } | 66 | } |
67 | 67 | ||
68 | +// protected static function convertToAssocArray($arr) | ||
69 | +// { | ||
70 | +// | ||
71 | +// $res = ''; | ||
72 | +// if (is_array($value)) { | ||
73 | +// | ||
74 | +// $res = Encoder::encodeArray($value); | ||
75 | +// | ||
76 | +// }elseif ( is_string($value) ) { | ||
77 | +// | ||
78 | +// $res = Encoder::encodeString($value); | ||
79 | +// | ||
80 | +// } | ||
81 | +// | ||
82 | +// return $res; | ||
83 | +// } | ||
68 | 84 | ||
69 | /** | 85 | /** |
70 | * @param $name - имя метода конвертации | 86 | * @param $name - имя метода конвертации |
@@ -128,4 +144,5 @@ class CSVConverter | @@ -128,4 +144,5 @@ class CSVConverter | ||
128 | } | 144 | } |
129 | 145 | ||
130 | 146 | ||
147 | + | ||
131 | } | 148 | } |
132 | \ No newline at end of file | 149 | \ No newline at end of file |
vendor/yiisoft/multiparser/CsvParser.php
@@ -73,7 +73,7 @@ class CsvParser implements ParserInterface | @@ -73,7 +73,7 @@ class CsvParser implements ParserInterface | ||
73 | public function setupConverter() | 73 | public function setupConverter() |
74 | { | 74 | { |
75 | if (!count($this->converter_conf)) { | 75 | if (!count($this->converter_conf)) { |
76 | - $this->converter = new CSVConverter(); | 76 | + $this->converter = new Converter(); |
77 | if ($this->hasHeaderRow) { | 77 | if ($this->hasHeaderRow) { |
78 | // если у файла есть заголовок, то в результате имеем ассоциативный массив | 78 | // если у файла есть заголовок, то в результате имеем ассоциативный массив |
79 | $this->converter_conf['hasKey'] = 1; | 79 | $this->converter_conf['hasKey'] = 1; |
vendor/yiisoft/multiparser/DynamicFormHelper.php
@@ -75,11 +75,26 @@ class DynamicFormHelper | @@ -75,11 +75,26 @@ class DynamicFormHelper | ||
75 | $result = array_map( | 75 | $result = array_map( |
76 | function ($value) { | 76 | function ($value) { |
77 | 77 | ||
78 | - return array_combine( self::$key_array, $value ); | 78 | + foreach ($value as $key => $sub_value) { |
79 | + return self::change_key( $key, self::$key_array[$sub_value], $value ); | ||
80 | + } | ||
81 | + | ||
82 | + | ||
83 | + // array_combine( self::$key_array, $value ); | ||
79 | 84 | ||
80 | }, | 85 | }, |
81 | $value_arr); | 86 | $value_arr); |
82 | return $result; | 87 | return $result; |
83 | } | 88 | } |
84 | 89 | ||
90 | + protected static function change_key( $key, $new_key, $arr ){ | ||
91 | + $res = $arr; | ||
92 | + if(!array_key_exists($new_key,$arr)){ | ||
93 | + $arr[$new_key]=$arr[$key]; | ||
94 | + unset($arr[$key]); | ||
95 | + $res = $arr; | ||
96 | + } | ||
97 | + return $res; | ||
98 | + } | ||
99 | + | ||
85 | } | 100 | } |
86 | \ No newline at end of file | 101 | \ No newline at end of file |
vendor/yiisoft/multiparser/XmlParser.php
@@ -10,44 +10,47 @@ namespace yii\multiparser; | @@ -10,44 +10,47 @@ namespace yii\multiparser; | ||
10 | 10 | ||
11 | 11 | ||
12 | class XmlParser implements ParserInterface{ | 12 | class XmlParser implements ParserInterface{ |
13 | + /** @var экземляр SplFileObject читаемого файла */ | ||
14 | + public $file; | ||
15 | + public $node; | ||
16 | + | ||
13 | public function read() | 17 | public function read() |
14 | { | 18 | { |
15 | - // TODO: Implement read() method. | 19 | + $file = $this->file; |
20 | + $result = self::xml2array( $file->getPathname() ); | ||
21 | + if ( isset($this->node) ) { | ||
22 | + $result = $result[$this->node]; | ||
23 | + } | ||
24 | + | ||
25 | + DynamicFormHelper::CreateAssocArray($result, \Yii::$app->multiparser->getConfiguration('xml','basic_column')); | ||
26 | + \common\components\CustomVarDamp::dumpAndDie($result); | ||
16 | } | 27 | } |
17 | 28 | ||
18 | public function setup() | 29 | public function setup() |
19 | { | 30 | { |
20 | - // TODO: Implement setup() method. | 31 | + |
32 | + | ||
21 | } | 33 | } |
22 | 34 | ||
23 | 35 | ||
24 | /** | 36 | /** |
25 | * Converts an XML string to a PHP array | 37 | * Converts an XML string to a PHP array |
26 | - * See http://phpsecurity.readthedocs.org/en/latest/Injection-Attacks.html#xml-external-entity-injection | ||
27 | * | 38 | * |
28 | * @uses recursiveXMLToArray() | 39 | * @uses recursiveXMLToArray() |
29 | - * @param string $val | ||
30 | - * @param boolean $disableDoctypes Disables the use of DOCTYPE, and will trigger an error if encountered. | ||
31 | - * false by default. | ||
32 | - * @param boolean $disableExternals Disables the loading of external entities. false by default. | 40 | + * @param string $file_path |
33 | * @return array | 41 | * @return array |
34 | */ | 42 | */ |
35 | - public static function xml2array($val, $disableDoctypes = false, $disableExternals = false) { | ||
36 | - // Check doctype | ||
37 | - if($disableDoctypes && preg_match('/\<\!DOCTYPE.+]\>/', $val)) { | ||
38 | - throw new InvalidArgumentException('XML Doctype parsing disabled'); | ||
39 | - } | 43 | + protected static function xml2array( $file_path ) { |
40 | 44 | ||
41 | - // Disable external entity loading | ||
42 | - if($disableExternals) $oldVal = libxml_disable_entity_loader($disableExternals); | ||
43 | try { | 45 | try { |
44 | - $xml = new SimpleXMLElement($val); | ||
45 | - $result = self::recursiveXMLToArray($xml); | 46 | + $xml = new \SimpleXMLElement( $file_path, 0, true ); |
47 | + //\common\components\CustomVarDamp::dumpAndDie($xml->children()->children()); | ||
48 | + $result = self::recursiveXMLToArray( $xml ); | ||
46 | } catch(Exception $ex) { | 49 | } catch(Exception $ex) { |
47 | - if($disableExternals) libxml_disable_entity_loader($oldVal); | 50 | + |
48 | throw $ex; | 51 | throw $ex; |
49 | } | 52 | } |
50 | - if($disableExternals) libxml_disable_entity_loader($oldVal); | 53 | + |
51 | return $result; | 54 | return $result; |
52 | } | 55 | } |
53 | 56 | ||
@@ -68,6 +71,8 @@ class XmlParser implements ParserInterface{ | @@ -68,6 +71,8 @@ class XmlParser implements ParserInterface{ | ||
68 | $x = $xml; | 71 | $x = $xml; |
69 | $xml = get_object_vars($xml); | 72 | $xml = get_object_vars($xml); |
70 | } | 73 | } |
74 | + | ||
75 | + //\common\components\CustomVarDamp::dump($xml); | ||
71 | if(is_array($xml)) { | 76 | if(is_array($xml)) { |
72 | if(count($xml) == 0) return (string) $x; // for CDATA | 77 | if(count($xml) == 0) return (string) $x; // for CDATA |
73 | foreach($xml as $key => $value) { | 78 | foreach($xml as $key => $value) { |
@@ -76,7 +81,7 @@ class XmlParser implements ParserInterface{ | @@ -76,7 +81,7 @@ class XmlParser implements ParserInterface{ | ||
76 | if(isset($a)) $r['@'] = $a; // Attributes | 81 | if(isset($a)) $r['@'] = $a; // Attributes |
77 | return $r; | 82 | return $r; |
78 | } | 83 | } |
79 | - | 84 | + //\common\components\CustomVarDamp::dumpAndDie($xml); |
80 | return (string) $xml; | 85 | return (string) $xml; |
81 | } | 86 | } |
82 | } | 87 | } |
83 | \ No newline at end of file | 88 | \ No newline at end of file |