class-wpml-xml2array.php
2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
class WPML_XML2Array implements WPML_XML_Transform {
private $contents;
private $get_attributes;
public function get( $contents, $get_attributes = true ) {
$this->contents = $contents;
$this->get_attributes = (bool) $get_attributes;
$xml_values = array();
if ( $this->contents && function_exists( 'xml_parser_create' ) ) {
$parser = xml_parser_create();
xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, 0 );
xml_parser_set_option( $parser, XML_OPTION_SKIP_WHITE, 1 );
xml_parse_into_struct( $parser, $this->contents, $xml_values );
xml_parser_free( $parser );
}
// Initializations
$xml_array = array();
$current = &$xml_array;
// Go through the tags.
foreach ( $xml_values as $data ) {
unset( $attributes, $value );// Remove existing values, or there will be trouble
$tag = $data['tag'];
$type = $data['type'];
$level = $data['level'];
$value = isset( $data['value'] ) ? $data['value'] : '';
$attributes = isset( $data['attributes'] ) ? $data['attributes'] : array();
$item = $this->get_item( $value, $attributes );
// See tag status and do the needed.
if ( 'open' === $type ) {// The starting of the tag '<tag>'
$parent[ $level - 1 ] = &$current;
if ( ! is_array( $current ) || ( ! isset( $current[ $tag ] ) ) ) { // Insert New tag
$current[ $tag ] = $item;
$current = &$current[ $tag ];
} else { // There was another element with the same tag name
if ( isset( $current[ $tag ][0] ) ) {
$current[ $tag ][] = $item;
} else {
$current[ $tag ] = array( $current[ $tag ], $item );
}
$last = count( $current[ $tag ] ) - 1;
$current = &$current[ $tag ][ $last ];
}
} elseif ( 'complete' === $type ) { // Tags that ends in 1 line '<tag />'
// See if the key is already taken.
if ( ! isset( $current[ $tag ] ) ) { // New Key
$current[ $tag ] = $item;
} else { // If taken, put all things inside a list(array)
if ( ( is_array( $current[ $tag ] ) && ! $this->get_attributes )// If it is already an array...
|| ( isset( $current[ $tag ][0] ) && is_array( $current[ $tag ][0] ) && $this->get_attributes )
) {
$current[ $tag ][] = $item;
} else { // If it is not an array...
$current[ $tag ] = array( $current[ $tag ], $item );
}
}
} elseif ( 'close' === $type && isset( $parent ) ) { // End of tag '</tag>'
$current = &$parent[ $level - 1 ];
}
}
return $xml_array;
}
/**
* @param mixed|null $value
* @param array $attributes
*
* @return array
*/
private function get_item( $value, array $attributes ) {
$item = array();
if ( $this->get_attributes ) {// The second argument of the function decides this.
if ( null !== $value ) {
$item['value'] = $value;
}
if ( null !== $attributes ) {
foreach ( $attributes as $attr => $val ) {
$item['attr'][ $attr ] = $val;
}
}
} else {
$item = $value;
}
return $item;
}
}