JsonHelper.php
Go to the documentation of this file.
00001 <?PHP 00002 00003 class JsonHelper 00004 { 00005 00006 private $Data; 00007 private $Warnings; 00008 00009 public function __construct() 00010 { 00011 $this->Data = array(); 00012 $this->Warnings = array(); 00013 } 00014 00015 public function AddDatum($Key, $Value) 00016 { 00017 $this->Data[$Key] = $Value; 00018 } 00019 00020 public function AddWarning($Message) 00021 { 00022 $this->Warnings[] = strval($Message); 00023 } 00024 00025 public function Error($Message) 00026 { 00027 $this->SendResult($this->GenerateResult("ERROR", $Message)); 00028 } 00029 00030 public function Success($Message="") 00031 { 00032 $this->SendResult($this->GenerateResult("OK", $Message)); 00033 } 00034 00035 private function SendResult(array $Result) 00036 { 00037 global $SysConfig; 00038 header("Content-Type: application/json; charset=" 00039 .$SysConfig->DefaultCharacterSet(), TRUE); 00040 $this->PrintArrayToJson($Result); 00041 } 00042 00043 private function GenerateResult($State, $Message) 00044 { 00045 return array( 00046 "data" => $this->Data, 00047 "status" => array( 00048 "state" => strval($State), 00049 "message" => strval($Message), 00050 "numWarnings" => count($this->Warnings), 00051 "warnings" => $this->Warnings)); 00052 } 00053 00054 private function PrintArrayToJson(array $Array) 00055 { 00056 # variables needed for printing commas if necessary 00057 $Offset = 0; 00058 $Count = count($Array); 00059 00060 # determine whether or not we have a true array or a hash map 00061 $TrueArray = TRUE; 00062 for ($i = 0, reset($Array); $i < count($Array); $i++, next($Array)) 00063 { 00064 if (key($Array) !== $i) 00065 { 00066 $TrueArray = FALSE; 00067 break; 00068 } 00069 } 00070 00071 # opening bracket 00072 print ($TrueArray) ? "[" : "{"; 00073 00074 # print each member 00075 foreach ($Array as $key => $value) 00076 { 00077 # replacements so we can escape strings and replace smart quotes 00078 static $Replace = array( 00079 array("\\", "/", "\n", "\t", "\r", "\b", "\f", '"', "", "", "", "", ""), 00080 array('\\\\', '\\/', '\\n', '\\t', '\\r', '\\b', '\\f', '\"', "'", "'", '\"', '\"', '-')); 00081 00082 # print key if a hash map 00083 if (!$TrueArray) 00084 { 00085 # escape, remove smart quotes, and print the key 00086 print '"'.str_replace($Replace[0], $Replace[1], $key).'":'; 00087 } 00088 00089 # scalar values (int, float, string, or boolean) 00090 if (is_scalar($value)) 00091 { 00092 # numeric (i.e., float, int, or float/int string) 00093 if (is_numeric($value)) 00094 { 00095 print $value; 00096 } 00097 00098 # string 00099 else if (is_string($value)) 00100 { 00101 # escape, remove smart quotes, and print the value 00102 print '"'.str_replace($Replace[0], $Replace[1], $value).'"'; 00103 } 00104 00105 # boolean true 00106 else if ($value === TRUE) 00107 { 00108 print "true"; 00109 } 00110 00111 # boolean false 00112 else if ($value === FALSE) 00113 { 00114 print "false"; 00115 } 00116 } 00117 00118 # recur if the value is an array 00119 else if (is_array($value)) 00120 { 00121 $this->PrintArrayToJson($value); 00122 } 00123 00124 # null 00125 else if (is_null($value)) 00126 { 00127 print "null"; 00128 } 00129 00130 # object, just print the name and don't possibly expose secret details 00131 else if (is_object($value)) 00132 { 00133 print '"object('.get_class($value).')"'; 00134 } 00135 00136 # resource, just print the name and don't possibly expose secret details 00137 else 00138 { 00139 print '"resource('.get_resource_type($value).')"'; 00140 } 00141 00142 # print comma if necessary 00143 if (++$Offset < $Count) { print ","; } 00144 } 00145 00146 # closing bracket 00147 print ($TrueArray) ? "]" : "}"; 00148 } 00149 00150 } 00151 00152 ?>