5 # Part of the Collection Workflow Integration System (CWIS) 6 # Copyright 2002-2013 Edward Almasy and Internet Scout Research Group 7 # http://scout.wisc.edu/cwis/ 22 $this->Data = array();
23 $this->Warnings = array();
33 $this->Data[$Key] = $Value;
45 $this->Warnings[] = strval($Message);
57 $this->SendResult($this->GenerateResult(
"ERROR", $Message));
67 $this->SendResult($this->GenerateResult(
"OK", $Message));
78 private function SendResult(array $Result)
81 header(
"Content-Type: application/json; charset=" 82 .$SysConfig->DefaultCharacterSet(), TRUE);
83 $this->PrintArrayToJson($Result);
94 private function GenerateResult($State, $Message)
97 "data" => $this->Data,
99 "state" => strval($State),
100 "message" => strval($Message),
101 "numWarnings" => count($this->Warnings),
102 "warnings" => $this->Warnings));
109 private function PrintArrayToJson(array $Array)
111 # variables needed for printing commas if necessary 113 $Count = count($Array);
115 # determine whether or not we have a true array or a hash map 117 $ArrayCount = count($Array);
118 for ($i = 0, reset($Array); $i < $ArrayCount; $i++, next($Array))
120 if (key($Array) !== $i)
128 print ($TrueArray) ?
"[" :
"{";
131 foreach ($Array as $key => $value)
133 # replacements so we can escape strings and replace smart quotes 134 static $Replace = array(
135 array(
"\\",
"/",
"\n",
"\t",
"\r",
"\b",
"\f",
'"',
"",
137 array(
'\\\\',
'\\/',
'\\n',
'\\t',
'\\r',
'\\b',
'\\f',
'\"',
138 "'",
"'",
'\"',
'\"',
'-'));
140 # print key if a hash map 143 # escape, remove smart quotes, and print the key 144 print
'"'.str_replace($Replace[0], $Replace[1], $key).
'":';
147 # scalar values (int, float, string, or boolean) 148 if (is_scalar($value))
150 # numeric (i.e., float, int, or float/int string) 151 if (is_numeric($value))
157 else if (is_string($value))
159 # escape, remove smart quotes, and print the value 160 print
'"'.str_replace($Replace[0], $Replace[1], $value).
'"';
164 else if ($value === TRUE)
170 else if ($value === FALSE)
176 # recur if the value is an array 177 else if (is_array($value))
179 $this->PrintArrayToJson($value);
183 else if (is_null($value))
188 # object, just print the name and don't possibly expose secret details 189 else if (is_object($value))
191 print
'"object('.get_class($value).
')"';
194 # resource, just print the name and don't possibly expose secret details 197 print
'"resource('.get_resource_type($value).
')"';
200 # print comma if necessary 201 if (++$Offset < $Count) { print
","; }
205 print ($TrueArray) ?
"]" :
"}";
AddWarning($Message)
Add a warning message to export in the JSON response.
Success($Message="")
Signal that the callback was successful and optionally set a message.
AddDatum($Key, $Value)
Add a datum identified by a key to export in the JSON response.
Convenience class for standardizing JSON responses, making it easier to export primitive data types t...
__construct()
Object constructor.
Error($Message)
Add an error message to export in the JSON response and then send the response.