21
21
curl_setopt ($ handle , CURLOPT_RETURNTRANSFER , true );
22
22
$ response = curl_exec ($ handle );
23
23
$ statusCode = curl_getinfo ($ handle , CURLINFO_HTTP_CODE );
24
- if ($ statusCode == 200 ) {
24
+
25
+ if ($ statusCode == 200 ) {
25
26
$ path = $ xmlQueryString ;
26
27
$ xml = simplexml_load_file ($ path );
27
28
$ json = xmlToArray ($ xml );
49
50
// Show all XML validation errors
50
51
$ statusCode = 400 ;
51
52
$ title = "Failed Loading XML " ;
52
- $ detail = array () ;
53
- foreach (libxml_get_errors () as $ error ) {
53
+ $ detail = [] ;
54
+ foreach (libxml_get_errors () as $ error ) {
54
55
$ detail [] = str_replace ("\n" , "" , $ error ->message );
55
56
}
56
57
$ json = constructErrorResponse ($ statusCode , $ title , $ detail );
68
69
return ;
69
70
}
70
71
71
- function xmlToArray ($ xml , $ options = array ()) {
72
- $ defaults = array (
73
- 'namespaceRecursive ' => false , // Get XML doc namespaces recursively
74
- 'removeNamespace ' => false , // Remove namespace from resulting keys (recommend setting namespaceSeparator = '' when true)
75
- 'namespaceSeparator ' => ': ' , // Change separator to something other than a colon
76
- 'attributePrefix ' => '@ ' , // Distinguish between attributes and nodes with the same name
77
- 'alwaysArray ' => array (), // Array of XML tag names which should always become arrays
78
- 'autoArray ' => true , // Create arrays for tags which appear more than once
79
- 'textContent ' => '#text ' , // Key used for the text content of elements
80
- 'autoText ' => true , // Skip textContent key if node has no attributes or child nodes
81
- 'keySearch ' => false , // (Optional) search and replace on tag and attribute names
82
- 'keyReplace ' => false // (Optional) replace values for above search values
83
- );
72
+ function xmlToArray ($ xml , $ options = [])
73
+ {
74
+ $ defaults = [
75
+ 'namespaceRecursive ' => false , // Get XML doc namespaces recursively
76
+ 'removeNamespace ' => false , // Remove namespace from resulting keys (recommend setting namespaceSeparator = '' when true)
77
+ 'namespaceSeparator ' => ': ' , // Change separator to something other than a colon
78
+ 'attributePrefix ' => '@ ' , // Distinguish between attributes and nodes with the same name
79
+ 'alwaysArray ' => [], // Array of XML tag names which should always become arrays
80
+ 'autoArray ' => true , // Create arrays for tags which appear more than once
81
+ 'textContent ' => '#text ' , // Key used for the text content of elements
82
+ 'autoText ' => true , // Skip textContent key if node has no attributes or child nodes
83
+ 'keySearch ' => false , // (Optional) search and replace on tag and attribute names
84
+ 'keyReplace ' => false , // (Optional) replace values for above search values
85
+ ];
84
86
$ options = array_merge ($ defaults , $ options );
85
87
$ namespaces = $ xml ->getDocNamespaces ($ options ['namespaceRecursive ' ]);
86
88
$ namespaces ['' ] = null ; // Add empty base namespace
87
-
89
+
88
90
// Get attributes from all namespaces
89
- $ attributesArray = array () ;
91
+ $ attributesArray = [] ;
90
92
foreach ($ namespaces as $ prefix => $ namespace ) {
91
93
if ($ options ['removeNamespace ' ]) {
92
94
$ prefix = '' ;
93
95
}
94
96
foreach ($ xml ->attributes ($ namespace ) as $ attributeName => $ attribute ) {
95
97
// (Optional) replace characters in attribute name
96
98
if ($ options ['keySearch ' ]) {
97
- $ attributeName =
98
- str_replace ($ options ['keySearch ' ], $ options ['keyReplace ' ], $ attributeName );
99
+ $ attributeName = str_replace ($ options ['keySearch ' ], $ options ['keyReplace ' ], $ attributeName );
99
100
}
100
101
$ attributeKey = $ options ['attributePrefix ' ] . ($ prefix ? $ prefix . $ options ['namespaceSeparator ' ] : '' ) . $ attributeName ;
101
- $ attributesArray [$ attributeKey ] = (string )$ attribute ;
102
+ $ attributesArray [$ attributeKey ] = (string ) $ attribute ;
102
103
}
103
104
}
104
-
105
+
105
106
// Get child nodes from all namespaces
106
- $ tagsArray = array () ;
107
+ $ tagsArray = [] ;
107
108
foreach ($ namespaces as $ prefix => $ namespace ) {
108
109
if ($ options ['removeNamespace ' ]) {
109
110
$ prefix = '' ;
@@ -114,52 +115,45 @@ function xmlToArray($xml, $options = array()) {
114
115
$ childArray = xmlToArray ($ childXml , $ options );
115
116
$ childTagName = key ($ childArray );
116
117
$ childProperties = current ($ childArray );
117
-
118
+
118
119
// Replace characters in tag name
119
120
if ($ options ['keySearch ' ]) {
120
- $ childTagName =
121
- str_replace ($ options ['keySearch ' ], $ options ['keyReplace ' ], $ childTagName );
121
+ $ childTagName = str_replace ($ options ['keySearch ' ], $ options ['keyReplace ' ], $ childTagName );
122
122
}
123
123
124
124
// Add namespace prefix, if any
125
125
if ($ prefix ) {
126
126
$ childTagName = $ prefix . $ options ['namespaceSeparator ' ] . $ childTagName ;
127
127
}
128
-
128
+
129
129
if (!isset ($ tagsArray [$ childTagName ])) {
130
130
// Only entry with this key
131
131
// Test if tags of this type should always be arrays, no matter the element count
132
- $ tagsArray [$ childTagName ] =
133
- in_array ($ childTagName , $ options ['alwaysArray ' ], true ) || !$ options ['autoArray ' ]
134
- ? array ($ childProperties ) : $ childProperties ;
135
- } elseif (
136
- is_array ($ tagsArray [$ childTagName ]) && array_keys ($ tagsArray [$ childTagName ])
137
- === range (0 , count ($ tagsArray [$ childTagName ]) - 1 )
138
- ) {
132
+ $ tagsArray [$ childTagName ] = in_array ($ childTagName , $ options ['alwaysArray ' ], true ) || !$ options ['autoArray ' ] ? [$ childProperties ] : $ childProperties ;
133
+ } elseif (is_array ($ tagsArray [$ childTagName ]) && array_keys ($ tagsArray [$ childTagName ]) === range (0 , count ($ tagsArray [$ childTagName ]) - 1 )) {
139
134
// Key already exists and is integer indexed array
140
135
$ tagsArray [$ childTagName ][] = $ childProperties ;
141
136
} else {
142
137
// Key exists so convert to integer indexed array with previous value in position 0
143
- $ tagsArray [$ childTagName ] = array ( $ tagsArray [$ childTagName ], $ childProperties) ;
138
+ $ tagsArray [$ childTagName ] = [ $ tagsArray [$ childTagName ], $ childProperties] ;
144
139
}
145
140
}
146
141
}
147
-
142
+
148
143
// Get text content of node
149
- $ textContentArray = array () ;
150
- $ plainText = trim ((string )$ xml );
144
+ $ textContentArray = [] ;
145
+ $ plainText = trim ((string ) $ xml );
151
146
if ($ plainText !== '' ) {
152
147
$ textContentArray [$ options ['textContent ' ]] = $ plainText ;
153
148
}
154
-
149
+
155
150
// Stick it all together
156
- $ propertiesArray = !$ options ['autoText ' ] || $ attributesArray || $ tagsArray || ($ plainText === '' )
157
- ? array_merge ($ attributesArray , $ tagsArray , $ textContentArray ) : $ plainText ;
158
-
151
+ $ propertiesArray = !$ options ['autoText ' ] || $ attributesArray || $ tagsArray || $ plainText === '' ? array_merge ($ attributesArray , $ tagsArray , $ textContentArray ) : $ plainText ;
152
+
159
153
// Return node as array
160
- return array (
161
- $ xml ->getName () => $ propertiesArray
162
- ) ;
154
+ return [
155
+ $ xml ->getName () => $ propertiesArray,
156
+ ] ;
163
157
}
164
158
165
159
/**
@@ -169,7 +163,8 @@ function xmlToArray($xml, $options = array()) {
169
163
*
170
164
* @return array The response
171
165
*/
172
- function constructErrorResponse ($ statusCode , $ title , $ detail ) {
166
+ function constructErrorResponse ($ statusCode , $ title , $ detail )
167
+ {
173
168
// Set timestamp to New York
174
169
$ timestamp = (new DateTime ("America/New_York " ))->format ("Y-m-d h:i:s " ) . "EST " ;
175
170
@@ -185,10 +180,7 @@ function constructErrorResponse($statusCode, $title, $detail) {
185
180
"version " => "2.0.0 " ,
186
181
"copyright " => "Copyright 2011- " . date ("Y " ) . " Fact Maven " ,
187
182
"link " => "https://factmaven.com/ " ,
188
- "authors " => [
189
- "Ethan O'Sullivan " ,
190
- "Edward Bebbington " ,
191
- ],
183
+ "authors " => ["Ethan O'Sullivan " , "Edward Bebbington " ],
192
184
],
193
185
];
194
186
return $ json ;
0 commit comments