在我的网站上,用户可以在标准文本输入字段中输入 HTTP 状态代码。即,200、400、404、403 等...
有没有办法检查HTTP状态代码是否有效。例如,如果用户输入“123”,它将返回 false。除了做一个大的丑陋的 if 或 switch 语句之外,我目前想不出或找到其他方法。
对于使用Symfony或Laravel 的用户,您可以执行以下操作:
use Illuminate\Http\Response;
function is_valid_http_status(int $code) : bool {
// thanks @James in the comments ^^
return array_key_exists($code, Response::$statusTexts);
}
Run Code Online (Sandbox Code Playgroud)
假设这Response
是其中之一:
Symfony\Component\HttpFoundation\Response
Illuminate\Http\Response