コード例 #1
0
 /**
  * Convert this response into a valid HTML response
  */
 function output()
 {
     $type = null;
     foreach ($this->request->accept as $accept) {
         foreach ($accept as $a) {
             switch ($a) {
                 case "xml":
                     $type = "xml";
                     break;
                 case "json":
                 default:
                     $type = "json";
                     break;
             }
             if ($type != null) {
                 break;
             } else {
                 $type = "json";
                 break;
             }
         }
     }
     if ($this->error != null && $this->error_detail != null && is_array($this->error_detail)) {
         $this->body = array("error" => $this->error, "detail" => $this->error_detail);
     } else {
         if ($this->error != null && $this->error_detail != null && is_string($this->error_detail)) {
             $this->body = array("error" => $this->error, "detail" => array("code" => $this->error_detail));
         } else {
             if ($this->error != null) {
                 $this->body = array("error" => $this->error);
             }
         }
     }
     switch ($type) {
         case "json":
         default:
             $this->body = json_encode($this->body);
             $this->addHeader('Content-Type', 'application/json');
             break;
         case "xml":
             $xmlnode = new XMLNode("response");
             if (is_array($this->body)) {
                 foreach ($this->body as $k => $v) {
                     if (is_int($k)) {
                         $k = "item";
                     }
                     if (is_array($v)) {
                         $n = new XMLNode($k);
                         $n->addArray($v);
                         $xmlnode->addValue(null, $n);
                     } else {
                         $xmlnode->addValue($k, $v);
                     }
                 }
             } else {
                 if (is_object($this->body) && method_exists($this->body, "__toString")) {
                     $xmlnode->addValue(get_class($this->body), $this->body->__toString());
                 } else {
                     if (is_object($this->body)) {
                         $xmlnode->addValue(get_class($this->body), "Object");
                     } else {
                         $xmlnode->addValue(null, $this->body);
                     }
                 }
             }
             $this->body = $xmlnode->generate();
             $this->addHeader('Content-Type', 'application/xml');
             break;
     }
     parent::output();
 }