CWIS Developer Documentation
JsonHelper.php
Go to the documentation of this file.
1 <?PHP
2 
3 class JsonHelper
4 {
5 
6  private $Data;
7  private $Warnings;
8 
9  public function __construct()
10  {
11  $this->Data = array();
12  $this->Warnings = array();
13  }
14 
15  public function AddDatum($Key, $Value)
16  {
17  $this->Data[$Key] = $Value;
18  }
19 
20  public function AddWarning($Message)
21  {
22  $this->Warnings[] = strval($Message);
23  }
24 
25  public function Error($Message)
26  {
27  $this->SendResult($this->GenerateResult("ERROR", $Message));
28  }
29 
30  public function Success($Message="")
31  {
32  $this->SendResult($this->GenerateResult("OK", $Message));
33  }
34 
35  private function SendResult(array $Result)
36  {
37  global $SysConfig;
38  header("Content-Type: application/json; charset="
39  .$SysConfig->DefaultCharacterSet(), TRUE);
40  $this->PrintArrayToJson($Result);
41  }
42 
43  private function GenerateResult($State, $Message)
44  {
45  return array(
46  "data" => $this->Data,
47  "status" => array(
48  "state" => strval($State),
49  "message" => strval($Message),
50  "numWarnings" => count($this->Warnings),
51  "warnings" => $this->Warnings));
52  }
53 
54  private function PrintArrayToJson(array $Array)
55  {
56  # variables needed for printing commas if necessary
57  $Offset = 0;
58  $Count = count($Array);
59 
60  # determine whether or not we have a true array or a hash map
61  $TrueArray = TRUE;
62  for ($i = 0, reset($Array); $i < count($Array); $i++, next($Array))
63  {
64  if (key($Array) !== $i)
65  {
66  $TrueArray = FALSE;
67  break;
68  }
69  }
70 
71  # opening bracket
72  print ($TrueArray) ? "[" : "{";
73 
74  # print each member
75  foreach ($Array as $key => $value)
76  {
77  # replacements so we can escape strings and replace smart quotes
78  static $Replace = array(
79  array("\\", "/", "\n", "\t", "\r", "\b", "\f", '"', "", "", "", "", ""),
80  array('\\\\', '\\/', '\\n', '\\t', '\\r', '\\b', '\\f', '\"', "'", "'", '\"', '\"', '-'));
81 
82  # print key if a hash map
83  if (!$TrueArray)
84  {
85  # escape, remove smart quotes, and print the key
86  print '"'.str_replace($Replace[0], $Replace[1], $key).'":';
87  }
88 
89  # scalar values (int, float, string, or boolean)
90  if (is_scalar($value))
91  {
92  # numeric (i.e., float, int, or float/int string)
93  if (is_numeric($value))
94  {
95  print $value;
96  }
97 
98  # string
99  else if (is_string($value))
100  {
101  # escape, remove smart quotes, and print the value
102  print '"'.str_replace($Replace[0], $Replace[1], $value).'"';
103  }
104 
105  # boolean true
106  else if ($value === TRUE)
107  {
108  print "true";
109  }
110 
111  # boolean false
112  else if ($value === FALSE)
113  {
114  print "false";
115  }
116  }
117 
118  # recur if the value is an array
119  else if (is_array($value))
120  {
121  $this->PrintArrayToJson($value);
122  }
123 
124  # null
125  else if (is_null($value))
126  {
127  print "null";
128  }
129 
130  # object, just print the name and don't possibly expose secret details
131  else if (is_object($value))
132  {
133  print '"object('.get_class($value).')"';
134  }
135 
136  # resource, just print the name and don't possibly expose secret details
137  else
138  {
139  print '"resource('.get_resource_type($value).')"';
140  }
141 
142  # print comma if necessary
143  if (++$Offset < $Count) { print ","; }
144  }
145 
146  # closing bracket
147  print ($TrueArray) ? "]" : "}";
148  }
149 
150 }
151 
152 ?>