CWIS Developer Documentation
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 
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  header("Content-Type: application/json; charset="
81  .$GLOBALS["G_SysConfig"]->DefaultCharacterSet(), TRUE);
82  $this->PrintArrayToJson($Result);
83  }
84 
93  private function GenerateResult($State, $Message)
94  {
95  return array(
96  "data" => $this->Data,
97  "status" => array(
98  "state" => strval($State),
99  "message" => strval($Message),
100  "numWarnings" => count($this->Warnings),
101  "warnings" => $this->Warnings));
102  }
103 
108  private function PrintArrayToJson(array $Array)
109  {
110  # variables needed for printing commas if necessary
111  $Offset = 0;
112  $Count = count($Array);
113 
114  # determine whether or not we have a true array or a hash map
115  $TrueArray = TRUE;
116  $ArrayCount = count($Array);
117  for ($i = 0, reset($Array); $i < $ArrayCount; $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  "", "", "", ""),
136  array('\\\\', '\\/', '\\n', '\\t', '\\r', '\\b', '\\f', '\"',
137  "'", "'", '\"', '\"', '-'));
138 
139  # print key if a hash map
140  if (!$TrueArray)
141  {
142  # escape, remove smart quotes, and print the key
143  print '"'.str_replace($Replace[0], $Replace[1], $key).'":';
144  }
145 
146  # scalar values (int, float, string, or boolean)
147  if (is_scalar($value))
148  {
149  # numeric (i.e., float, int, or float/int string)
150  if (is_numeric($value))
151  {
152  print $value;
153  }
154 
155  # string
156  else if (is_string($value))
157  {
158  # escape, remove smart quotes, and print the value
159  print '"'.str_replace($Replace[0], $Replace[1], $value).'"';
160  }
161 
162  # boolean true
163  else if ($value === TRUE)
164  {
165  print "true";
166  }
167 
168  # boolean false
169  else if ($value === FALSE)
170  {
171  print "false";
172  }
173  }
174 
175  # recur if the value is an array
176  else if (is_array($value))
177  {
178  $this->PrintArrayToJson($value);
179  }
180 
181  # null
182  else if (is_null($value))
183  {
184  print "null";
185  }
186 
187  # object, just print the name and don't possibly expose secret details
188  else if (is_object($value))
189  {
190  print '"object('.get_class($value).')"';
191  }
192 
193  # resource, just print the name and don't possibly expose secret details
194  else
195  {
196  print '"resource('.get_resource_type($value).')"';
197  }
198 
199  # print comma if necessary
200  if (++$Offset < $Count) { print ","; }
201  }
202 
203  # closing bracket
204  print ($TrueArray) ? "]" : "}";
205  }
206 }
AddWarning($Message)
Add a warning message to export in the JSON response.
Definition: JsonHelper.php:43
Success($Message="")
Signal that the callback was successful and optionally set a message.
Definition: JsonHelper.php:65
AddDatum($Key, $Value)
Add a datum identified by a key to export in the JSON response.
Definition: JsonHelper.php:31
Convenience class for standardizing JSON responses, making it easier to export primitive data types t...
Definition: JsonHelper.php:14
__construct()
Object constructor.
Definition: JsonHelper.php:20
Error($Message)
Add an error message to export in the JSON response and then send the response.
Definition: JsonHelper.php:55