diff --git a/src/lib/Tools.php b/src/lib/Tools.php index 4800f78..ebf46a9 100644 --- a/src/lib/Tools.php +++ b/src/lib/Tools.php @@ -47,4 +47,36 @@ class Tools $str = preg_replace('/^\_/', '', $str); return $str; } + + /** + * 获取数组、对象下标对应值,不存在时返回指定的默认值 + * @param string|integer $name - 下标(键名) + * @param array|object $data - 原始数组/对象 + * @param mixed $default - 指定默认值 + * @return mixed + */ + public static function getSubValue($name, $data, $default = '') { + if (is_object($data)) { + $value = isset($data->$name) ? $data->$name : $default; + } else if (is_array($data)) { + $value = isset($data[$name]) ? $data[$name] : $default; + } else { + $value = $default; + } + return $value; + } + + /** + * 文件下载 + * @param string - $docHtml - API文档HTML内容 + */ + public static function downloadFile($docHtml) { + set_time_limit(0); + //下载文件需要用到的头 + header('Content-type: application/octet-stream'); + header('Accept-Ranges: bytes'); + header('Content-Disposition: attachment; filename=api-doc_' . date('Y-m-d') . '.html'); + echo $docHtml; + exit(); + } } \ No newline at end of file