1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* pretty prints a json string |
5
|
|
|
* |
6
|
|
|
* @param string $json |
7
|
|
|
*/ |
8
|
|
|
function indent($json) |
9
|
|
|
{ |
10
|
|
|
$result = ''; |
11
|
|
|
$pos = 0; |
12
|
|
|
$strLen = strlen($json); |
13
|
|
|
$indentStr = ' '; |
14
|
|
|
$newLine = "\n"; |
15
|
|
|
$prevChar = ''; |
16
|
|
|
$outOfQuotes = true; |
17
|
|
|
|
18
|
|
|
for ($i=0; $i<=$strLen; $i++) { |
19
|
|
|
|
20
|
|
|
// Grab the next character in the string. |
21
|
|
|
$char = substr($json, $i, 1); |
22
|
|
|
|
23
|
|
|
// Are we inside a quoted string? |
24
|
|
|
if ($char == '"' && $prevChar != '\\') { |
25
|
|
|
$outOfQuotes = !$outOfQuotes; |
26
|
|
|
|
27
|
|
|
// If this character is the end of an element, |
28
|
|
|
// output a new line and indent the next line. |
29
|
|
|
} elseif (($char == '}' || $char == ']') && $outOfQuotes) { |
30
|
|
|
$result .= $newLine; |
31
|
|
|
$pos --; |
32
|
|
|
for ($j=0; $j<$pos; $j++) { |
33
|
|
|
$result .= $indentStr; |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
// Add the character to the result string. |
38
|
|
|
$result .= $char; |
39
|
|
|
|
40
|
|
|
// If the last character was the beginning of an element, |
41
|
|
|
// output a new line and indent the next line. |
42
|
|
|
if (($char == ',' || $char == '{' || $char == '[') && $outOfQuotes) { |
43
|
|
|
$result .= $newLine; |
44
|
|
|
if ($char == '{' || $char == '[') { |
45
|
|
|
$pos ++; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
for ($j = 0; $j < $pos; $j++) { |
49
|
|
|
$result .= $indentStr; |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$prevChar = $char; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return $result; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* converts a DOMElement to an array representation |
61
|
|
|
* |
62
|
|
|
* @param \DOMElement $root |
63
|
|
|
*/ |
64
|
|
|
function dom_to_array($root) |
65
|
|
|
{ |
66
|
|
|
// if the node has only a single text node |
67
|
|
|
if (!$root->hasAttributes() && $root->childNodes->length==1 |
68
|
|
|
&& $root->childNodes->item(0)->nodeType == XML_TEXT_NODE) { |
69
|
|
|
return $root->childNodes->item(0)->nodeValue; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$result = array(); |
73
|
|
|
|
74
|
|
|
if ($root->hasAttributes()) { |
75
|
|
|
$attrs = $root->attributes; |
76
|
|
|
|
77
|
|
|
foreach ($attrs as $i => $attr) { |
78
|
|
|
$result["_" . $attr->name] = $attr->value; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$children = $root->childNodes; |
83
|
|
|
|
84
|
|
|
$group = array(); |
85
|
|
|
|
86
|
|
|
$text = ""; |
87
|
|
|
|
88
|
|
|
for ($i = 0; $i < $children->length; $i++) { |
89
|
|
|
$child = $children->item($i); |
90
|
|
|
if ($child->nodeType == XML_TEXT_NODE) { |
91
|
|
|
$text = $text . $child->nodeValue; |
92
|
|
|
} else { |
93
|
|
|
if (!isset($result[$child->nodeName])) { |
94
|
|
|
$result[$child->nodeName] = dom_to_array($child); |
|
|
|
|
95
|
|
|
} else { |
96
|
|
|
if (!isset($group[$child->nodeName])) { |
97
|
|
|
$tmp = $result[$child->nodeName]; |
98
|
|
|
$result[$child->nodeName] = array($tmp); |
99
|
|
|
$group[$child->nodeName] = 1; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$result[$child->nodeName][] = dom_to_array($child); |
|
|
|
|
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
$trimmed = trim($text); |
107
|
|
|
if ($trimmed != "") { |
108
|
|
|
$result['#text'] = $text; |
109
|
|
|
} |
110
|
|
|
return $result; |
111
|
|
|
} |
112
|
|
|
/** |
113
|
|
|
* takes a file name of an xml document and returns an json representation |
114
|
|
|
* |
115
|
|
|
* @param string $fileName |
116
|
|
|
*/ |
117
|
|
|
function convert($fileName) |
118
|
|
|
{ |
119
|
|
|
$d = new DOMDocument(1, "UTF-8"); |
120
|
|
|
$d->load($fileName); |
121
|
|
|
$ret[$d->documentElement->nodeName] = dom_to_array($d->documentElement); |
|
|
|
|
122
|
|
|
return json_encode($ret); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
echo indent(str_replace("\/", "/", convert($argv[1]))); |
126
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.