CWIS Developer Documentation

- Home
- Main Page
- Classes
- Files

- File List
- File Members

JsonHelper.php
Go to the documentation of this file.
1 <?PHP
2 #
3 # FILE: JsonHelper.php
4 #
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/
8 #
9
14 class JsonHelper
15 {
16
20 public function __construct()
21 {
22 $this->Data = array();
23 $this->Warnings = array();
24 }
25
31 public function AddDatum($Key, $Value)
32 {
33 $this->Data[$Key] = $Value;
34 }
35
43 public function AddWarning($Message)
44 {
45 $this->Warnings[] = strval($Message);
46 }
47
55 public function Error($Message)
56 {
57 $this->SendResult($this->GenerateResult("ERROR", $Message));
58 }
59
65 public function Success($Message="")
66 {
67 $this->SendResult($this->GenerateResult("OK", $Message));
68 }
69
70 private $Data;
71 private $Warnings;
72
78 private function SendResult(array $Result)
79 {
80 global $SysConfig;
81 header("Content-Type: application/json; charset="
82 .$SysConfig->DefaultCharacterSet(), TRUE);
83 $this->PrintArrayToJson($Result);
84 }
85
94 private function GenerateResult($State, $Message)
95 {
96 return array(
97 "data" => $this->Data,
98 "status" => array(
99 "state" => strval($State),
100 "message" => strval($Message),
101 "numWarnings" => count($this->Warnings),
102 "warnings" => $this->Warnings));
103 }
104
109 private function PrintArrayToJson(array $Array)
110 {
111 # variables needed for printing commas if necessary
112 $Offset = 0;
113 $Count = count($Array);
114
115 # determine whether or not we have a true array or a hash map
116 $TrueArray = TRUE;
117 for ($i = 0, reset($Array); $i < count($Array); $i++, next($Array))
118 {
119 if (key($Array) !== $i)
120 {
121 $TrueArray = FALSE;
122 break;
123 }
124 }
125
126 # opening bracket
127 print ($TrueArray) ? "[" : "{";
128
129 # print each member
130 foreach ($Array as $key => $value)
131 {
132 # replacements so we can escape strings and replace smart quotes
133 static $Replace = array(
134 array("\\", "/", "\n", "\t", "\r", "\b", "\f", '"', "", "", "", "", ""),
135 array('\\\\', '\\/', '\\n', '\\t', '\\r', '\\b', '\\f', '\"', "'", "'", '\"', '\"', '-'));
136
137 # print key if a hash map
138 if (!$TrueArray)
139 {
140 # escape, remove smart quotes, and print the key
141 print '"'.str_replace($Replace[0], $Replace[1], $key).'":';
142 }
143
144 # scalar values (int, float, string, or boolean)
145 if (is_scalar($value))
146 {
147 # numeric (i.e., float, int, or float/int string)
148 if (is_numeric($value))
149 {
150 print $value;
151 }
152
153 # string
154 else if (is_string($value))
155 {
156 # escape, remove smart quotes, and print the value
157 print '"'.str_replace($Replace[0], $Replace[1], $value).'"';
158 }
159
160 # boolean true
161 else if ($value === TRUE)
162 {
163 print "true";
164 }
165
166 # boolean false
167 else if ($value === FALSE)
168 {
169 print "false";
170 }
171 }
172
173 # recur if the value is an array
174 else if (is_array($value))
175 {
176 $this->PrintArrayToJson($value);
177 }
178
179 # null
180 else if (is_null($value))
181 {
182 print "null";
183 }
184
185 # object, just print the name and don't possibly expose secret details
186 else if (is_object($value))
187 {
188 print '"object('.get_class($value).')"';
189 }
190
191 # resource, just print the name and don't possibly expose secret details
192 else
193 {
194 print '"resource('.get_resource_type($value).')"';
195 }
196
197 # print comma if necessary
198 if (++$Offset < $Count) { print ","; }
199 }
200
201 # closing bracket
202 print ($TrueArray) ? "]" : "}";
203 }
204
205 }
JsonHelper\AddWarning
AddWarning($Message)
Add a warning message to export in the JSON response.
Definition: JsonHelper.php:43
JsonHelper\Success
Success($Message="")
Signal that the callback was successful and optionally set a message.
Definition: JsonHelper.php:65
JsonHelper\AddDatum
AddDatum($Key, $Value)
Add a datum identified by a key to export in the JSON response.
Definition: JsonHelper.php:31
PHP
PHP
Definition: OAIClient.php:39
JsonHelper
Convenience class for standardizing JSON responses, making it easier to export primitive data types t...
Definition: JsonHelper.php:14
JsonHelper\__construct
__construct()
Object constructor.
Definition: JsonHelper.php:20
JsonHelper\Error
Error($Message)
Add an error message to export in the JSON response and then send the response.
Definition: JsonHelper.php:55