CWIS Developer Documentation

- Home
- Main Page
- Namespaces
- 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 $ArrayCount = count($Array);
118 for ($i = 0, reset($Array); $i < $ArrayCount; $i++, next($Array))
119 {
120 if (key($Array) !== $i)
121 {
122 $TrueArray = FALSE;
123 break;
124 }
125 }
126
127 # opening bracket
128 print ($TrueArray) ? "[" : "{";
129
130 # print each member
131 foreach ($Array as $key => $value)
132 {
133 # replacements so we can escape strings and replace smart quotes
134 static $Replace = array(
135 array("\\", "/", "\n", "\t", "\r", "\b", "\f", '"', "",
136 "", "", "", ""),
137 array('\\\\', '\\/', '\\n', '\\t', '\\r', '\\b', '\\f', '\"',
138 "'", "'", '\"', '\"', '-'));
139
140 # print key if a hash map
141 if (!$TrueArray)
142 {
143 # escape, remove smart quotes, and print the key
144 print '"'.str_replace($Replace[0], $Replace[1], $key).'":';
145 }
146
147 # scalar values (int, float, string, or boolean)
148 if (is_scalar($value))
149 {
150 # numeric (i.e., float, int, or float/int string)
151 if (is_numeric($value))
152 {
153 print $value;
154 }
155
156 # string
157 else if (is_string($value))
158 {
159 # escape, remove smart quotes, and print the value
160 print '"'.str_replace($Replace[0], $Replace[1], $value).'"';
161 }
162
163 # boolean true
164 else if ($value === TRUE)
165 {
166 print "true";
167 }
168
169 # boolean false
170 else if ($value === FALSE)
171 {
172 print "false";
173 }
174 }
175
176 # recur if the value is an array
177 else if (is_array($value))
178 {
179 $this->PrintArrayToJson($value);
180 }
181
182 # null
183 else if (is_null($value))
184 {
185 print "null";
186 }
187
188 # object, just print the name and don't possibly expose secret details
189 else if (is_object($value))
190 {
191 print '"object('.get_class($value).')"';
192 }
193
194 # resource, just print the name and don't possibly expose secret details
195 else
196 {
197 print '"resource('.get_resource_type($value).')"';
198 }
199
200 # print comma if necessary
201 if (++$Offset < $Count) { print ","; }
202 }
203
204 # closing bracket
205 print ($TrueArray) ? "]" : "}";
206 }
207 }
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: RSSClient--Test.php:12
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