工具类更新

This commit is contained in:
IT小强xqitw.cn 2018-06-07 13:42:07 +08:00
parent 826ab94aaa
commit e75e80b391

View File

@ -47,4 +47,36 @@ class Tools
$str = preg_replace('/^\_/', '', $str); $str = preg_replace('/^\_/', '', $str);
return $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();
}
} }