From e75e80b391cf5a2e26a18729ee19c45b97f5e187 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?IT=E5=B0=8F=E5=BC=BAxqitw=2Ecn?= <360237521@qq.com> Date: Thu, 7 Jun 2018 13:42:07 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B7=A5=E5=85=B7=E7=B1=BB=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lib/Tools.php | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) 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