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         print $this->ArrayToJson($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 ArrayToJson(array $Array)
00055     {
00056         global $SysConfig;
00057 
00058         # initialize
00059         $JSON = "";
00060         $counter = 0;
00061 
00062         foreach ($Array as $key => $value)
00063         {
00064             # add key
00065             $JSON .= "'".$key."':";
00066 
00067             # recur if the value is an array
00068             if (is_array($value))
00069             {
00070                 $JSON .= $this->ArrayToJson($value);
00071             }
00072 
00073             # add the value
00074             else
00075             {
00076                 # escape
00077                 $value = str_replace("\r", "", $value);
00078                 $value = htmlentities($value, ENT_QUOTES, $SysConfig->DefaultCharacterSet());
00079 
00080                 # if the value is a number, parse it and don't put in quotes
00081                 if (is_numeric($value))
00082                 {
00083                     $JSON .= intval($value);
00084                 }
00085 
00086                 # for everything else, get the string value and put in quotes
00087                 else
00088                 {
00089                     $JSON .= "'".strval($value)."'";
00090                 }
00091             }
00092 
00093             # add comma if necessary
00094             $counter++;
00095             $JSON .= ($counter < count($Array)) ? "," : "" ;
00096         }
00097 
00098         # return JSON-ified array
00099         return "{".$JSON."}";
00100     }
00101 
00102 }
00103 
00104 ?>