0
1
Fork 0
hedera-web-mindshore/usr/share/hedera-web/php/web/json.php

83 lines
1.3 KiB
PHP
Raw Normal View History

2014-05-09 12:19:53 +00:00
<?php
require_once ('php/vn/error.php');
class JsonError
{
var $domain;
var $code;
var $message;
}
class JsonData
{
var $data;
}
class JsonReply
{
var $error = NULL;
var $warnings = array ();
var $data = NULL;
}
class JsonLib
{
const ENCODING_JSON = 0;
const ENCODING_HTML = 1;
private static $reply = NULL;
static $encoding = self::ENCODING_JSON;
static function init ()
{
self::$reply = new JsonReply ();
Error::setHandler ('JsonLib::errorHandler');
}
static function setData ($data)
{
self::$reply->data = $data;
}
static function setError ($domain, $code, $message)
{
if (!isset (self::$reply->error))
{
$error = new JsonError ();
$error->domain = $domain;
$error->code = $code;
$error->message = $message;
self::$reply->error = $error;
}
}
static function addWarning ($str)
{
self::$reply->warnings[] = $str;
}
static function errorHandler ($domain, $code, $message)
{
self::setError ($domain, $code, $message);
}
static function sendReply ()
{
switch (self::$encoding)
{
case self::ENCODING_HTML:
if (self::$reply->error != NULL)
print (self::$reply->error->message);
else
print (self::$reply->data);
break;
default:
header ('Content-Type: application/json;charset=utf-8');
print ('(' . json_encode (self::$reply) . ')');
}
}
}
?>