<?php

namespace Heanup\Frame;

class Response
{
    const HTTP_VERSION = "HTTP/1.1";

    //返回结果
    public static function sendResponse($code, $data = [], $msg = 'OK')
    {
        //获取数据
        if ($code == 200) {
            $status = 200;
            $message = 'OK';
        } elseif ($code == 404) {
            $status = 404;
            $data = array('error' => 'Not Found');
            $message = 'Not Found';
        } elseif ($code == 405) {
            $status = 405;
            $data = array('error' => 'Method not allowed');
            $message = 'Method not allowed';
        } else {
            $status = 200;
            $message = $msg;
        }

        //输出结果
        header(self::HTTP_VERSION . " " . $status . " " . $message);
        $output = array(
            'code' => (int)intval($code),
            'message' => $message,
        );
        $content_type = isset($_SERVER['CONTENT_TYPE']) ? $_SERVER['CONTENT_TYPE'] : $_SERVER['HTTP_ACCEPT'];
        if (strpos($content_type, 'application/json') !== false) {
            if (is_array($data) &&  $data['page_config']) {
                $output = array_merge($output, $data);
            } else {
                $output['data'] = $data;
            }
            header("Content-Type: application/json");
            echo self::encodeJson($output);
            die();
        } else if (strpos($content_type, 'application/xml') !== false) {
            header("Content-Type: application/xml");
            echo self::encodeXml($output);
            die();
        } else {
            if (is_array($data) && $data['page_config']) {
                $output = array_merge($output, $data);
            } else {
                $output['data'] = $data;
            }
            header("Content-Type: application/json");
            echo self::encodeJson($output);

            die();
        }
    }

    //json格式
    private static function encodeJson($responseData)
    {
        $str = json_encode($responseData);
        return $str;
    }

    //xml格式
    private static function encodeXml($responseData)
    {
        $xml = new \SimpleXMLElement('<?xml version="1.0"?><rest></rest>');
        foreach ($responseData as $key => $value) {
            if (is_array($value)) {
                foreach ($value as $k => $v) {
                    $xml->addChild($k, $v);
                }
            } else {
                $xml->addChild($key, $value);
            }
        }
        return $xml->asXML();
    }

    //html格式
    private static function encodeHtml($responseData)
    {
        $html = "<table border='1'>";
        foreach ($responseData as $key => $value) {
            $html .= "<tr>";
            if (is_array($value)) {
                foreach ($value as $k => $v) {
                    $html .= "<td>" . $k . "</td><td>" . $v . "</td>";
                }
            } else {
                $html .= "<td>" . $key . "</td><td>" . $value . "</td>";
            }
            $html .= "</tr>";
        }
        $html .= "</table>";
        return $html;
    }

}
