|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
class Kint_Parser_Xml extends Kint_Parser_Plugin |
|
|
|
|
|
|
4
|
|
|
{ |
|
5
|
|
|
/** |
|
6
|
|
|
* Which method to parse the variable with. |
|
7
|
|
|
* |
|
8
|
|
|
* DOMDocument provides more information including the text between nodes, |
|
9
|
|
|
* however it's memory usage is very high and it takes longer to parse and |
|
10
|
|
|
* render. Plus it's a pain to work with. So SimpleXML is the default. |
|
11
|
|
|
* |
|
12
|
|
|
* @var string |
|
13
|
|
|
*/ |
|
14
|
|
|
public static $parse_method = 'SimpleXML'; |
|
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
public function getTypes() |
|
17
|
|
|
{ |
|
18
|
|
|
return array('string'); |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
public function getTriggers() |
|
|
|
|
|
|
22
|
|
|
{ |
|
23
|
|
|
return Kint_Parser::TRIGGER_SUCCESS; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function parse(&$var, Kint_Object &$o, $trigger) |
|
|
|
|
|
|
27
|
|
|
{ |
|
28
|
|
|
if (substr($var, 0, 5) !== '<?xml') { |
|
29
|
|
|
return; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
if (!method_exists(get_class($this), 'xmlTo'.self::$parse_method)) { |
|
33
|
|
|
return; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
$xml = call_user_func(array(get_class($this), 'xmlTo'.self::$parse_method), $var, $o->access_path); |
|
37
|
|
|
|
|
38
|
|
|
if (empty($xml)) { |
|
39
|
|
|
return; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
list($xml, $access_path, $name) = $xml; |
|
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
$base_obj = new Kint_Object(); |
|
|
|
|
|
|
45
|
|
|
$base_obj->depth = $o->depth + 1; |
|
|
|
|
|
|
46
|
|
|
$base_obj->name = $name; |
|
|
|
|
|
|
47
|
|
|
$base_obj->access_path = $access_path; |
|
|
|
|
|
|
48
|
|
|
|
|
49
|
|
|
$r = new Kint_Object_Representation('XML'); |
|
|
|
|
|
|
50
|
|
|
$r->contents = $this->parser->parse($xml, $base_obj); |
|
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
$o->addRepresentation($r, 0); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
protected static function xmlToSimpleXML($var, $parent_path) |
|
|
|
|
|
|
56
|
|
|
{ |
|
57
|
|
|
try { |
|
58
|
|
|
$errors = libxml_use_internal_errors(true); |
|
59
|
|
|
$xml = simplexml_load_string($var); |
|
60
|
|
|
libxml_use_internal_errors($errors); |
|
61
|
|
|
} catch (Exception $e) { |
|
62
|
|
|
if (isset($errors)) { |
|
63
|
|
|
libxml_use_internal_errors($errors); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
return; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
if (!$xml) { |
|
70
|
|
|
return; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
View Code Duplication |
if ($parent_path === null) { |
|
|
|
|
|
|
74
|
|
|
$access_path = null; |
|
|
|
|
|
|
75
|
|
|
} else { |
|
76
|
|
|
$access_path = 'simplexml_load_string('.$parent_path.')'; |
|
|
|
|
|
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$name = $xml->getName(); |
|
80
|
|
|
|
|
81
|
|
|
return array($xml, $access_path, $name); |
|
|
|
|
|
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Get the DOMDocument info. |
|
86
|
|
|
* |
|
87
|
|
|
* The documentation of DOMDocument::loadXML() states that while you can |
|
88
|
|
|
* call it statically, it will give an E_STRICT warning. On my system it |
|
89
|
|
|
* actually gives an E_DEPRECATED warning, but it works so we'll just add |
|
90
|
|
|
* an error-silencing '@' to the access path. |
|
91
|
|
|
* |
|
92
|
|
|
* If it errors loading then we wouldn't have gotten this far in the first place. |
|
93
|
|
|
* |
|
94
|
|
|
* @param string $var The XML string |
|
95
|
|
|
* @param string $parent_path The path to the parent, in this case the XML string |
|
96
|
|
|
* |
|
97
|
|
|
* @return array The root element DOMNode, the access path, and the root element name |
|
|
|
|
|
|
98
|
|
|
*/ |
|
99
|
|
|
protected static function xmlToDOMDocument($var, $parent_path) |
|
|
|
|
|
|
100
|
|
|
{ |
|
101
|
|
|
// There's no way to check validity in DOMDocument without making errors. For shame! |
|
102
|
|
|
if (!self::xmlToSimpleXML($var, $parent_path)) { |
|
|
|
|
|
|
103
|
|
|
return; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
$xml = new DOMDocument(); |
|
107
|
|
|
$xml->loadXML($var); |
|
108
|
|
|
$xml = $xml->firstChild; |
|
109
|
|
|
|
|
110
|
|
View Code Duplication |
if ($parent_path === null) { |
|
|
|
|
|
|
111
|
|
|
$access_path = null; |
|
|
|
|
|
|
112
|
|
|
} else { |
|
113
|
|
|
$access_path = '@DOMDocument::loadXML('.$parent_path.')->firstChild'; |
|
|
|
|
|
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
$name = $xml->nodeName; |
|
117
|
|
|
|
|
118
|
|
|
return array($xml, $access_path, $name); |
|
|
|
|
|
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
This check examines a number of code elements and verifies that they conform to the given naming conventions.
You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.