|
/** Return Data Formatting class
* DATE:2011-08-15
* Author:fdipzone
*/
Class datareturn{//Class start
Private $type;
Private $xmlroot;
Private $callback;
Private $returnData;
Public function __construct ($param =array ()) {
$this->type = $this->exists ($param, ' type ')? Strtoupper ($param [' type ']): ' JSON '; Type Json,xml,callback,array
$this->xmlroot = $this->exists ($param, ' XmlRoot ')? $param [' XmlRoot ']: ' XmlRoot '; XML root DOM Name
$this->callback = $this->exists ($param, ' callback ')? $param [' Callback ']: '; JS callback Function name
$format = Array ();
$format [' retcode '] = $this->exists ($param, ' Format.retcode ')? $param [' Format '] [' retcode ']: ' Retcode ';//retcode corresponding name
$format [' msg '] = $this->exists ($param, ' format.msg ')? $param [' Format '] [' msg ']: ' msg '; MSG corresponding Name
$format [' data '] = $this->exists ($param, ' Format.data ')? $param [' Format '] [' data ']: ' Data '; Data corresponding name
$result = Array ();
$result [$format [' retcode ']] = $this->exists ($param, ' Retcode ')? $param [' Retcode ']: 0;
$result [$format [' msg ']] = $this->exists ($param, ' msg ')? $param [' msg ']: ';
$result [$format [' data '] = $this->exists ($param, ' data ')? $param [' Data ']: ';
$this->returndata = $result;
}
Output data
Public Function Data_return () {
Ob_clean ();
Switch ($this->type) {
Case ' JSON ':
$this->json_return ();
Break
Case ' XML ':
$this->xml_return ();
Break
Case ' CALLBACK ':
$this->callback_return ();
Break
Case ' ARRAY ':
$this->array_return ();
Break
Default
$this->json_return ();
}
Exit ();
}
Output JSON format data, return JSONP format if callback parameter is available
Private Function Json_return () {
Header (' Content-type:text/html;charset=utf-8 ');
if (Empty ($this->callback)) {
Echo Json_encode ($this->returndata);
}else{
echo $this->callback. ' ('. Json_encode ($this->returndata). ';
}
}
Output XML Format data
Private Function Xml_return () {
Header (' Content-type:text/xml;charset=utf-8 ');
echo $this->xml_encode ($this->returndata, $this->xmlroot);
}
Output JSON format data and call the callback method
Private Function Callback_return () {
Header (' Content-type:text/html;charset=utf-8 ');
$this->callback = Empty ($this->callback)? ' Callback ': $this->callback;
echo "";
}
Output Array Format data
Private Function Array_return () {
Header (' Content-type:text/html;charset=utf-8 ');
Echo '
Echo '
';
}
XML encoding
Private Function Xml_encode ($data, $root = ' XmlRoot ', $encoding = ' utf-8 ') {
$xml = " \ n";
$xml. = "<". $root. ">\n";
$xml. = $this->data_to_xml ($data);
$xml. = " ";
return $xml;
}
Array to XML format
Private Function Data_to_xml ($data) {
if (Is_object ($data)) {
$data = Get_object_vars ($data);
}
$xml = ";
foreach ($data as $key = = $val) {
Is_numeric ($key) && $key = "Item id=\" $key \ "";
$xml. = "< $key >";
$xml. = (Is_array ($val) | | is_object ($val))? $this->data_to_xml ($val): $this->cdata ($val);
List ($key,) = Explode ("', $key);
$xml. = " \ n";
}
return $xml;
}
Determine if the data exists
Private function exists ($obj, $key = ") {
if ($key = = ") {
return Isset ($obj) &&!empty ($obj);
}else{
$keys = Explode ('. ', $key);
For ($i =0, $max =count ($keys), $i < $max; $i + +) {
if (Isset ($obj [$keys [$i]]) {
$obj = $obj [$keys [$i]];
}else{
return false;
}
}
return Isset ($obj) &&!empty ($obj);
}
}
Determine if you need to add a tag
Private Function CDATA ($val) {
if (!empty ($val) &&!preg_match ('/^[a-za-z0-9+$]/', $val)) {
$val = " '.$val.' ;
}
return $val;
}
}//Class end
?>
|