Skip to content

Commit c6a323d

Browse files
author
Ethan Jinks O'Sullivan
committed
Update PHP formate
1 parent db04a9e commit c6a323d

File tree

1 file changed

+42
-50
lines changed

1 file changed

+42
-50
lines changed

index.php

+42-50
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
2222
$response = curl_exec($handle);
2323
$statusCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
24-
if($statusCode == 200) {
24+
25+
if ($statusCode == 200) {
2526
$path = $xmlQueryString;
2627
$xml = simplexml_load_file($path);
2728
$json = xmlToArray($xml);
@@ -49,8 +50,8 @@
4950
// Show all XML validation errors
5051
$statusCode = 400;
5152
$title = "Failed Loading XML";
52-
$detail = array();
53-
foreach(libxml_get_errors() as $error) {
53+
$detail = [];
54+
foreach (libxml_get_errors() as $error) {
5455
$detail[] = str_replace("\n", "", $error->message);
5556
}
5657
$json = constructErrorResponse($statusCode, $title, $detail);
@@ -68,42 +69,42 @@
6869
return;
6970
}
7071

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+
];
8486
$options = array_merge($defaults, $options);
8587
$namespaces = $xml->getDocNamespaces($options['namespaceRecursive']);
8688
$namespaces[''] = null; // Add empty base namespace
87-
89+
8890
// Get attributes from all namespaces
89-
$attributesArray = array();
91+
$attributesArray = [];
9092
foreach ($namespaces as $prefix => $namespace) {
9193
if ($options['removeNamespace']) {
9294
$prefix = '';
9395
}
9496
foreach ($xml->attributes($namespace) as $attributeName => $attribute) {
9597
// (Optional) replace characters in attribute name
9698
if ($options['keySearch']) {
97-
$attributeName =
98-
str_replace($options['keySearch'], $options['keyReplace'], $attributeName);
99+
$attributeName = str_replace($options['keySearch'], $options['keyReplace'], $attributeName);
99100
}
100101
$attributeKey = $options['attributePrefix'] . ($prefix ? $prefix . $options['namespaceSeparator'] : '') . $attributeName;
101-
$attributesArray[$attributeKey] = (string)$attribute;
102+
$attributesArray[$attributeKey] = (string) $attribute;
102103
}
103104
}
104-
105+
105106
// Get child nodes from all namespaces
106-
$tagsArray = array();
107+
$tagsArray = [];
107108
foreach ($namespaces as $prefix => $namespace) {
108109
if ($options['removeNamespace']) {
109110
$prefix = '';
@@ -114,52 +115,45 @@ function xmlToArray($xml, $options = array()) {
114115
$childArray = xmlToArray($childXml, $options);
115116
$childTagName = key($childArray);
116117
$childProperties = current($childArray);
117-
118+
118119
// Replace characters in tag name
119120
if ($options['keySearch']) {
120-
$childTagName =
121-
str_replace($options['keySearch'], $options['keyReplace'], $childTagName);
121+
$childTagName = str_replace($options['keySearch'], $options['keyReplace'], $childTagName);
122122
}
123123

124124
// Add namespace prefix, if any
125125
if ($prefix) {
126126
$childTagName = $prefix . $options['namespaceSeparator'] . $childTagName;
127127
}
128-
128+
129129
if (!isset($tagsArray[$childTagName])) {
130130
// Only entry with this key
131131
// 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)) {
139134
// Key already exists and is integer indexed array
140135
$tagsArray[$childTagName][] = $childProperties;
141136
} else {
142137
// 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];
144139
}
145140
}
146141
}
147-
142+
148143
// Get text content of node
149-
$textContentArray = array();
150-
$plainText = trim((string)$xml);
144+
$textContentArray = [];
145+
$plainText = trim((string) $xml);
151146
if ($plainText !== '') {
152147
$textContentArray[$options['textContent']] = $plainText;
153148
}
154-
149+
155150
// 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+
159153
// Return node as array
160-
return array(
161-
$xml->getName() => $propertiesArray
162-
);
154+
return [
155+
$xml->getName() => $propertiesArray,
156+
];
163157
}
164158

165159
/**
@@ -169,7 +163,8 @@ function xmlToArray($xml, $options = array()) {
169163
*
170164
* @return array The response
171165
*/
172-
function constructErrorResponse($statusCode, $title, $detail) {
166+
function constructErrorResponse($statusCode, $title, $detail)
167+
{
173168
// Set timestamp to New York
174169
$timestamp = (new DateTime("America/New_York"))->format("Y-m-d h:i:s ") . "EST";
175170

@@ -185,10 +180,7 @@ function constructErrorResponse($statusCode, $title, $detail) {
185180
"version" => "2.0.0",
186181
"copyright" => "Copyright 2011-" . date("Y") . " Fact Maven",
187182
"link" => "https://factmaven.com/",
188-
"authors" => [
189-
"Ethan O'Sullivan",
190-
"Edward Bebbington",
191-
],
183+
"authors" => ["Ethan O'Sullivan", "Edward Bebbington"],
192184
],
193185
];
194186
return $json;

0 commit comments

Comments
 (0)