Return data formatting class instance implemented by php

  • 2021-07-18 07:31:59
  • OfStack

This paper describes the return data formatting class implemented by php and its usage, which is of great practical value in string processing. Share it for your reference. The specific methods are as follows:

The DataReturn. class. php class files are as follows:


<?php 
/**  Returns a 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 , If so callback Parameter returns the JSONP Format  
  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 "<script type=\"text/javascript\">\r\n"; 
    echo $this->callback."(".json_encode($this->returnData).");\r\n"; 
    echo "</script>"; 
  } 
 
  // Output array format data  
  private function array_return(){ 
    header('content-type:text/html;charset=utf-8'); 
    echo '<pre>'; 
    print_r($this->returnData); 
    echo '</pre>'; 
  } 
 
  //XML Code  
  private function xml_encode($data, $root='xmlroot', $encoding='utf-8') { 
    $xml = "<?xml version=\"1.0\" encoding=\"" . $encoding . "\"?>\n"; 
    $xml.= "<" . $root . ">\n"; 
    $xml.= $this->data_to_xml($data); 
    $xml.= "</" . $root . ">"; 
    return $xml; 
  } 
 
  // Array conversion 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.="</$key>\n"; 
    } 
    return $xml; 
  } 
 
  // Determine whether the data exists or not  
  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 whether you need to add <![CDATA[]]> Mark  
  private function cdata($val){ 
    if(!empty($val) && !preg_match('/^[A-Za-z0-9+$]/',$val)){ 
      $val = '<![CDATA['.$val.']]>'; 
    } 
    return $val; 
  } 
}  // class end 
?> 

The demo sample program is as follows:


<?php 
  require_once('DataReturn.class.php'); 
  $param = array( // DataReturn  Parameter  
          'type'  => 'JSON', //  Type of output  JSON,XML,CALLBACK,ARRAY  Default to  JSON 
          'retcode' => '1000', // retcode  The default value is 0 
          'msg'   => '',   // msg  The default value is empty  
          'data'  => array( //  Data to output  
                  'id'   => '100', 
                  'name'  => 'fdipzone', 
                  'gender' => 1, 
                  'age'  => 28 
                 ), 
          'format' => array( //  Output data key Format , Default to  retcode,msg,data 
                  'retcode' => 'status', 
                  'msg'   => 'info', 
                  'data'  => 'result' 
                 ), 
          'xmlroot' => 'xmlroot', //  When type=XML Hour ,XML Root node name, default to xmlroot 
          'callback' => 'callback' /*  Callback method name  
                          type=JSON Is null by default, if it is not null, the output is callback({data}); 
                          type=CALLBACK Hour , Default to callback Automatically invoke the page JS Callback method  
                       */ 
  ); 
  $obj = new DataReturn($param); //  Create DataReturn Class object  
  $obj->data_return();      //  Output data in format  
?>

I hope this article is helpful to everyone's study of php programming.


Related articles: